我正在尝试使用序列化将表单数据发送到使用该数据发送电子邮件的另一个页面。它不是将数据发送到进程页面,而是将数据附加到表单页面上的查询字符串。我在ajax查询中包含了正确的URL,所以我不确定为什么会这样?
这是我的代码:
<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 :(得分:0)
喜欢这个
$("#submitButtonId").click(function () {
var getdata = $('#idForm').serialize();
$.post('~Email/BookingEnquiry', {setdata:getdata}, function (data) {
$('#mydata').html(data);
});
});
答案 1 :(得分: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>