代码之家  ›  专栏  ›  技术社区  ›  Gavin5511

AJAX发布发送电子邮件-网络矩阵

  •  1
  • Gavin5511  · 技术社区  · 12 年前

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

    许多在线论坛建议我也应该加入“return flase”元素,这会是问题吗?

    2 回复  |  直到 12 年前
        1
  •  0
  •   jswolf    12 年前

    就像这个

    $("#submitButtonId").click(function () {
        var getdata = $('#idForm').serialize();
        $.post('~Email/BookingEnquiry', {setdata:getdata}, function (data) {
            $('#mydata').html(data);
        });
    });
    
        2
  •  0
  •   Mr.Turtle    12 年前

    对不起,我删除了我的旧答案,看起来你在某个地方有语法错误(我找不到)

    这就是我应该做的,我没有编程太多,但我认为这是一种可以做的方式。 请问我是否有什么可以帮忙的

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