【问题标题】:JQUERY/ AJAX Request not going through and conflictsJQUERY/AJAX 请求未通过和冲突
【发布时间】:2014-10-10 12:16:15
【问题描述】:

我在我的页面中包含了一个联系表。在同一页面中,我有一个脚本,该脚本根据下拉列表的值获取价格。现在,当我尝试提交联系信息时,我与价格脚本发生冲突。基本上它试图运行它,我不知道为什么。提交时的联系表格也永远不会工作......我只是用URL打开一个新页面......?message = blablabla 知道出了什么问题吗?

我正在使用 Laravel 4.2,因此您看到的路由重定向到我的 php 函数。 这是JSfiddle,这是php代码:

    public function postSendMessage() {
    echo "<span class=\"alert alert-success\" >Your message has been received. Thanks!</span><br><br>";
}

【问题讨论】:

  • 你不要取消表单提交。

标签: javascript php jquery ajax laravel


【解决方案1】:

取消点击,表单将不会提交

$("button#send").click( function(evt){
    evt.preventDefault();

新错误,表单有一个联系人 id,而不是一个类

data: $('form.contact').serialize(),

需要

 data: $('form#contact').serialize(),

【讨论】:

  • 如果我是正确的按钮,请不要提交...仅输入具有提交类型的输入
  • @epascarello ,不确定你的意思是什么?表格必须在点击时提交。冲突在于提取名为retrievePrice 的价格的脚本。谢谢
  • 您在onclick 上进行了Ajax 调用,那么为什么需要提交表单?尝试添加代码。
  • @AndreasFurster &lt;button type="submit" class="btn btn-primary" id="send"&gt;Send&lt;/button&gt; 这是一个提交按钮。
  • 哦....我明白你的意思了。所以我可以删除按钮的提交类型吗?现在我也收到此错误,但名称正确:0 Uncaught SyntaxError: Unexpected identifier for data: $('form.contact').serialize(),
【解决方案2】:

这就是我在相同情况下所做的

    //For your drpbox use this code
    $(document).on("change", "#yorDropBoxId", function(){        
        dropBoxValue=$("#yorDropBoxId").val();
        var request = $.ajax({//http://api.jquery.com/jQuery.ajax/
                        url: "samePagePHPcript.php",
                        type: "POST",
                        data: { 
                              ObjEvn:"dropBoxEvent",
                              dropBoxValue: dropBoxValue //You will use $myVar=$_POST["dropBoxValue"] to retrieve the information from javascript                              
                      },
                        dataType: "json"
             });
             request.done(function(dataset){
             //If you want to retrieve information from PHP sent by JSON.  
                for (var index in dataset){ 
                    JsResponse=dataset[index].phpResponse;
                }

                 if(JsResponse test someting){
                 "do dometing"
                 control the beheaivor of your HTML elements
                 }
             }); 
             request.fail(function(jqXHR, textStatus) {
                  alert( "Request failed: " + textStatus );
             });

    });



    //To submit your form use this code. You must use Prevent default if you are using a button or using a <a> link tag to trigger the evenrmrnt
    $(document).on("click", "#btn_sendForm", function(e){
        e.preventDefault();    
        var dt={ 
                ObjEvn:"FormEvent",
                input1:$("#txt_input1").val(), 
                input2: $("#txt_input2").val(), 
                input3: $("#txt_input3").val() 
            };
        var request = $.ajax({//http://api.jquery.com/jQuery.ajax/
                            url: "samePagePHPcript.php",
                            type: "POST",
                            data: dt,
                            dataType: "json"
                 });
            request.done(function(dataset){
                 //If you want to retrieve information from PHP send by JSON.  
                    for (var index in dataset){ 
                        JsResponse=dataset[index].phpResponse;
                    }

                     if(JsResponse test someting){
                     "do dometing"
                     control the beheaivor of your HTML elements
                     }
            }); 
            request.fail(function(jqXHR, textStatus) {
                      alert( "Request failed: " + textStatus );
            });
    });  



    //In the samePagePHPcript.php you can do this:You will return your information from PHP using json like this

    $event = $_POST["ObjEvn"];

    if(event==="FormEvent"){//Event to insert in your form
    $arrToJSON = array(
            "phpResponse"=>"data you want to send to javascript",
            "asYouWant"=>"<div class=\".class1\">more data</div>"    
            );  
    echo json_encode(array($arrToJSON));

    }
    elseif(event==="dropBoxEvent"){//Event to your dropbox - if you want
    $arrToJSON = array(
            "phpResponse"=>"data you want to send to javascript",
            "asYouWant"=>"<div class=\".class1\">more data</div>"    
            );  
    echo json_encode(array($arrToJSON));


    }

【讨论】:

  • 第一个 Dropbox 功能到底是干什么用的?为什么剧本现在这么长?
  • 我在一个多语言站点中使用这个脚本。你有2个功能。一个用于下拉框,另一个用于按钮发送表单。您可以将其简化并合并为一个。我的网站非常动态。所以我的保管箱可以发送消息到服务器检索信息并做一些事情。形成一样的想法。我与这种技术没有冲突。即使我调用同一个 php 页面来分析触发了哪个事件
猜你喜欢
  • 2015-03-21
  • 2012-03-21
  • 2012-02-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-04
  • 2011-06-10
  • 1970-01-01
相关资源
最近更新 更多