2013-09-26 66 views
1

我試圖發送表單數據,使用序列化,到另一個頁面,使用該數據發送電子郵件。它不是將數據發送到進程頁面,而是將數據附加到表單頁上的查詢字符串。我在ajax查詢中包含了正確的URL,所以我不確定爲什麼會發生這種情況?AJAX post發送電子郵件 - webmatrix

這裏是我的代碼:

<form id="idForm"> 
<div> 
    <label>Your name:</label> 
    <input type="text" name="customerName" /> 
</div> 

<div> 
    <label>Your email address:</label> 
    <input type="text" name="customerEmail" /> 
</div> 

<div> 
    <label>Details about your enquiry:</label> 
    <textarea name="customerRequest" cols="45" rows="4"></textarea> 
</div> 
<input type="hidden" name="propertyid" value="@rPropertyId"> 
<button id="submitButtonId" type="submit" class="btn btn-default" value="Submit">Submit</button> 

</form> 

<script> 
$(document).ready(function() { 
    $("#submitButtonId").click(function() { 
    var url = "~Email/BookingEnquiry"; 
     $.ajax({ 
      url: url, 
      data: $("#idForm").serialize(), // serializes the form's elements. 
     success: function(data) 
     { 
      alert(data); 
     } 
    }); 

return false; 
    }); 
}:; 

</script> 

我被數不清的網上論壇,我應該包括「返回FLASE」元素太多,可這是問題的建議?

+0

另外,我有沒有添加的方法和措施,以幫助我完成表格,但我不應該需要右鍵,Ajax代碼應該處理所有的問題? – Gavin5511

回答

0

贊一個

$("#submitButtonId").click(function() { 
    var getdata = $('#idForm').serialize(); 
    $.post('~Email/BookingEnquiry', {setdata:getdata}, function (data) { 
     $('#mydata').html(data); 
    }); 
}); 
+0

我不知道我明白,它將如何知道將數據發送到哪個URL? – Gavin5511

+0

現在你可以在這個文件上發送數據,數據通過html在#mydata div上返回 – jswolf

0

對不起,我刪除了我的老回答,好像你有一個語法錯誤的地方(我不能找到它)

這是我會怎麼做它,我沒有編寫很多程序,但我認爲它是一個可行的方法。 請詢問是否有什麼我可以用

<form id="idForm"> 
<div> 
    <label>Your name:</label> 
    <input type="text" id="customerName" /> 
</div> 

<div> 
    <label>Your email address:</label> 
    <input type="text" id="customerEmail" /> 
</div> 

<div> 
    <label>Details about your enquiry:</label> 
    <textarea id="customerRequest" cols="45" rows="4"></textarea> 
</div> 
<input type="hidden" id="propertyid" value="@rPropertyId"> 
<button id="submitButtonId" type="submit" class="btn btn-default" value="Submit">Submit</button> 

</form> 

<script> 
$(document).ready(function() { 
    $('#idForm').submit(function(){ // If the form was submitted 
     var customerName = $('#customerName').val(), // All the values from the fields... 
      customerEmail = $('#customerEmail').val(), // 
      customerRequest = $('#customerRequest').val(), // 
      propertyid = $('#propertyid').val(); // 
     $.post("PATH/FILENAME.php", // The php-file to process the data 
      { 
      // In the php-file, use $_POST['customerName']; --> etc 
      customerName : customerName, // all the values + their names 
      customerEmail : customerEmail, 
      customerRequest : customerRequest, 
      propertyid : propertyid 
      }, 
      function(data){ // What to do with the data when finished. 
      alert(data); // Data is the same as whats beeing echo'ed in the php-script 
     }); 
    }); 
}); 
</script>