2014-10-04 114 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();