1

我正在尝试使用序列化将表单数据发送到另一个使用该数据发送电子邮件的页面。它不是将数据发送到流程页面,而是将数据附加到表单页面上的查询字符串。我在 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>

许多在线论坛都建议我也应该包含“returnflase”元素,这可能是问题吗?

4

2 回答 2

0

像这个

$("#submitButtonId").click(function () {
    var getdata = $('#idForm').serialize();
    $.post('~Email/BookingEnquiry', {setdata:getdata}, function (data) {
        $('#mydata').html(data);
    });
});
于 2013-09-26T07:38:06.900 回答
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>
于 2013-09-26T09:40:53.490 回答