2014-10-10 140 views
0

我在頁面中包含聯繫表單。在同一頁面中,我有一個腳本根據下拉的值獲取價格。現在,當我嘗試提交聯繫消息時,我與價格腳本發生衝突。基本上它試圖運行它,我不知道爲什麼。提交時的聯繫表單從來沒有工作...我只是得到一個新的網頁打開網址..?message = blablabla 任何想法是什麼問題?JQUERY/AJAX請求不通過並衝突

我正在Laravel 4.2上工作,所以你看到的路線重定向到我的PHP函數。 這裏是JSfiddle這裏是PHP代碼:

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

做的很好,你不要取消表單提交。 – epascarello 2014-10-10 12:20:08

+0

哎呀,對不起... – 2014-10-10 12:22:43

回答

1

取消點擊這樣的形式不會提交

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

新的錯誤,形式有接觸的ID,而不是類

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

需求是

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

如果我是正確的按鈕,請不要提交...只有輸入提交類型 – 2014-10-10 12:23:28

+0

@epascarello,不知道你明白你的意思嗎?必須單擊提交表單。衝突是與拉取價格叫做retrievePrice的腳本。謝謝 – commandantp 2014-10-10 12:25:30

+0

您打了一個Ajax調用onclick爲什麼你需要提交表單?嘗試添加代碼。 – epascarello 2014-10-10 12:26:07

0

這是我的相同情形

//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)); 


    } 
+0

什麼是第一個Dropbox函數的具體用途?爲什麼現在的腳本這麼長? – commandantp 2014-10-10 13:05:48

+0

我在多語言站點中使用這個腳本,你有2個函數,一個用於另一個按鈕發送表單的下拉框。你可以簡化它,並融合在一個我的網站是非常動態的,所以我的收件箱可以發送信息serever檢索信息和做someting形式作爲相同的想法我沒有這種技術的衝突即使當我打電話給同一個php頁面來分析哪個事件被觸發 – zwitterion 2014-10-10 14:34:34