【问题标题】:AJAX post to send email - webmatrixAJAX post 发送电子邮件 - webmatrix
【发布时间】:2013-09-26 07:24:59
【问题描述】:

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

【问题讨论】:

  • 另外,我还没有在我的表单中添加方法和操作,但我不需要正确,ajax 代码应该处理所有这些?

标签: jquery ajax razor webmatrix


【解决方案1】:

喜欢这个

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

【讨论】:

  • 我不确定我是否理解,它如何知道将数据发送到哪个 URL?
  • 现在您可以发送此文件中的数据,并且数据会通过 html 返回到#mydata div
【解决方案2】:

对不起,我删除了我的旧答案,您似乎在某处有语法错误(我找不到)

这就是我会做的,我没有编程很多,但我认为这是一个不错的方法。 请问有什么可以帮忙的

<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>

【讨论】:

    猜你喜欢
    • 2014-11-30
    • 2011-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多