1

我是使用 Ajax 的新手,我正在尝试使用以下代码接收以表单提交的数据。

<div>

    <p> <label for="name">Full Name:</label>    
    <input type="text" name="name" required/></p>
    <br />

    <p><label for="email">Email:</label>    
    <input type="text" name="email" required/></p>
    <br />

    <p><label for="id">id:</label>  
    <input type="text" name="id" value="<?php echo($id); ?>" required/></p>
    <br />

    <p><label for="phone">Phone:</label>    
    <input type="text" name="phone" /></p>
    <br />

    <p><label for="phone">Message:</label>  
    <textarea required></textarea></p>

    <button onclick="myCall()" type="submit">Submit</button>
    <div id="mybox">
        Answer: 
    </div>

</div>

jQuery

<script>

    function myCall() {
        var request = $.ajax({
            url: "processor.php",
            type: "GET",            
            dataType: "html"
        });

        request.done(function(msg) {
            $("#mybox").html(msg);          
        });

        request.fail(function(jqXHR, textStatus) {
            alert( "Request failed: " + textStatus );
        });
    }
</script>

这在仅回显测试消息时工作正常,但是没有发送表单数据,因为它显然需要包装在表单标签中,当我这样做时,出现以下错误。

Request failed: error

带有 form 标签的 HTML:

<form class="claim" method="get" id="contact">
          <div>

         <p> <label for="name">Full Name:</label>   
            <input type="text" name="name" required/></p>
            <br />

          <p><label for="email">Email:</label>  
            <input type="text" name="email" required/></p>
            <br />

            <p><label for="id">id:</label>  
            <input type="text" name="id" value="<?php echo($id); ?>" required/></p>
            <br />

            <p><label for="phone">Phone:</label>    
            <input type="text" name="phone" /></p>
            <br />

            <p><label for="phone">Message:</label>  
            <textarea required></textarea></p>

            <button onclick="myCall()" type="submit">Submit</button>
            <div id="mybox">
             Answer: 
        </div>
          </div>
        </form>

然后页面重新加载,提交表单 - 我在这里遗漏了什么吗?

我相信这是一件简单的事情,我们都必须从某个地方开始!在此先感谢您的帮助。

4

1 回答 1

1

You aren't passing the data in with your request. Add your form tag back in and serialize it in your call, like this:

function myCall() {
    var request = $.ajax({
        url: "processor.php",
        type: "GET",      
        data: $("#contact").serialize(),      
        dataType: "html"
    });
于 2013-09-16T13:22:16.617 回答