2014-10-04 113 views
0

我有一個使用jquery ajax()提交表單數據的聯繫人。它適用於xampp,但在服務器上,應該發送到php文件的表單數據變爲空白。什麼都沒有被髮送到外部文件ajax()在提交時丟失了表單數據

$(document).ready(function(){ 
    $("input#regbut").click(function(){ 
      var edin = $("input[name^='name']").val(); 
     var mailer = $("input[name^='email']").val(); 
     var dept = $("input:radio[name^='direct']:checked").val(); 
     var hint = $("input[name^='subject']").val(); 
     var bodied = $("textarea[name^='message']").val(); 
     var seri = "name=" + edin + "&email=" + mailer + "&direct=" + dept + "&subject=" + hint + "&message=" + bodied; 

     $("span#war").fadeIn(function(){ 
     $("span#war").css("display", "block"); 
     }); 
     $("#warn").html(''); 
     if(edin ==''){ 
       $("#warn").html('You did not tell us your name'); 
     }else if(mailer == ''){ 
       $("#warn").html('Your email is required'); 
     }else if(hint == ''){ 
       $("#warn").html('Subject is required'); 
     }else if(bodied == ''){ 
       $("#warn").html('You did not tell us what the problem is'); 
     }else{ 
     $.ajax({ 
      type: "GET", 
      url: "contactpro.php", 
      data: seri, 
      error: function(){ 
       $("#warn").html("Sorry! an error occurred"); 
      }, 
      success: function(repo){ 
       $("#warn").html(repo); 
       alert(repo); 
      } 
     }); 
     } 
     return false; 
    }); 
}); 

<form method="post" id="cotact"> 
       <table width="80%" border="0" cellspacing="3" cellpadding="1"> 
       <tr> 
        <td></td> 
        <td id="warn" style="color:#FF0000;"><h3 style="color:#0099FF;"><span id="war" style="display:none;">Sending...</span></h3></td> 
       </tr> 
       <tr> 
        <td>Name</td> 
        <td><label> 
        <input name="name" type="text" class="input" id="sign_up" /> 
        </label></td> 
       </tr> 
       <tr> 
        <td>Email </td> 
        <td><label> 
        <input name="email" type="text" class="input" id="sign_up" /> 
        </label></td> 
       </tr> 
       <tr> 
        <td>Direct to</td> 
        <td><p> 
         <label> 
         <input name="direct" type="radio" id="direct_0" value="1" checked="checked" /> 
         General</label> 
        /enquiry<br /> 
         <label> 
         <input type="radio" name="direct" value="2" id="direct_1" /> 
         Advertising</label> 
         <br /> 
         <label> 
         <input type="radio" name="direct" value="3" id="direct_2" /> 
         Reports</label> 
         <br /> 
         <label> 
         <input type="radio" name="direct" value="4" id="direct_3" /> 
         Suggestions</label> 
         <br /> 
        </p></td> 
       </tr> 
       <tr> 
        <td>Subject</td> 
        <td><label> 
        <input name="subject" type="text" class="input" id="sign_up" /> 
        </label></td> 
       </tr> 
       <tr> 
        <td>Message</td> 
        <td><label> 
        <textarea name="message" cols="45" rows="5" class="input" id="sign_up" style="height:100px;"></textarea> 
        </label></td> 
       </tr> 
       <tr> 
        <td>&nbsp;</td> 
        <td><label> 
        <input type="submit" name="submit" id="regbut" value="Submit" /> 
        </label></td> 
       </tr> 
       </table> 
      </form></td> 
     </tr> 
     <tr> 
      <td colspan="2" align="center"></td> 
     </tr> 
     </table> 
+0

相對路徑是一樣的嗎? – Wold 2014-10-04 04:59:09

+0

將'type:「GET」'更改爲'type:「POST」'和'data:「seri」'更改爲'data:「postDATA」'。另外,表單方法也應該是「POST」。 – mijopabe 2014-10-04 05:01:00

+0

爲什麼你用GET發送比較大量的信息?此外,構建像'seri =「name =」+ edin +「&email =」+ mailer +「&direct =」'這樣的數據是一個糟糕的主意 - 如果其中一個變量具有&,那該怎麼辦?你應該逃避數據或讓jQuery爲你做。 – Cheery 2014-10-04 05:01:18

回答

0

這很難說,這是怎麼回事,但嘗試在$.ajax調用傳遞對象到data而不是查詢字符串的像你,例如:

 ... 
     $.ajax({ 
      type: "GET", 
      url: "contactpro.php", 
      data: { 
       name: edin, 
       email: mailer, 
       ... 
      }, 
      error: function(){ 
       $("#warn").html("Sorry! an error occurred"); 
      }, 
      success: function(repo){ 
       $("#warn").html(repo); 
       alert(repo); 
      } 
     }); 
     ... 

此外,請確認您正在檢查$_GET,因爲您的ajax請求指定了請求方法(我注意到您的表單方法爲post),您確定您沒有檢查$_POST?如果您想要通過POST發送ajax請求,請將其類型更改爲"POST"

0

這很難說清楚。儘量使JSON和類型更改爲「後」

var seri = "name :edin,email: mailer, direct:dept, subject:hint, message:bodied} 
0
$("#form").submit(function(e) { 
    var postData = $(this).serializeArray(); 
    var formURL = $(this).attr("action"); 
    $.ajax({ 
    url : formURL, 
    type: "POST", 
    data : postData, 
    success:function(data, textStatus, jqXHR) 
    { 
     // should it work 
    }, 
    error: function(jqXHR, textStatus, errorThrown) 
    { 
     // should something go crazy  
    } 
    }); 
    e.preventDefault(); 
    e.unbind(); 
}); 

$("#form").submit();