ajax()在提交时丢失表单数据

时间:2014-10-04 04:52:00

标签: jquery forms

我有一个联系人,使用jquery ajax()来提交表单数据。它适用于xampp,但在服务器上,应该发送到php文件的表单数据变为空白。什么都没发送到外部文件

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

3 个答案:

答案 0 :(得分:0)

很难说出发生了什么,但是尝试将对象传递到data调用中的$.ajax,而不是像您一样将查询字符串传递给 ... $.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

此外,请验证您是否正在检查post,因为您的ajax请求指定了请求方法(我注意到您的表单方法为$_POST)您确定没有检查POST ?如果您希望通过"POST"发送ajax请求,请将类型更改为{{1}}

答案 1 :(得分:0)

这很难说。尝试制作JSON并将类型更改为“post”

var seri = "name :edin,email: mailer, direct:dept, subject:hint, message:bodied}

答案 2 :(得分: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();