2013-09-16 39 views
1

我是使用Ajax的新手,並試圖使用以下代碼接收表單中提交的數據。當以表格形式包裝數據時發生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 

與形式標記的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> 

然後重新加載頁面,提交表單 - 我失去了一些東西在這裏?

我相信這是一件很簡單的事情,我們都必須從某個地方開始!在此先感謝您的幫助。

回答

1

您沒有將數據傳遞給您的請求。添加您的表單標籤回和你通話序列,就像這樣:

function myCall() { 
    var request = $.ajax({ 
     url: "processor.php", 
     type: "GET",  
     data: $("#contact").serialize(),  
     dataType: "html" 
    }); 
+0

感謝您的幫助,但我仍然得到同樣的錯誤信息,然後提交表單,並帶我到PHP腳本.. 。 – AppleTattooGuy

+0

註釋掉JavaScript(或在瀏覽器中禁用JavaScript)。那它有用嗎? –

+0

是的,它在提交表單的意義上工作,PHP腳本目前只是從提交的表單中回顯ID。 – AppleTattooGuy