jQuery -获取ajax POST的表单值

时间:2022-02-01 13:19:09

I am trying to post form values via AJAX to a php file. How do I collect my form values to send inside of the "data" parameter?

我正在尝试通过AJAX将表单值发布到php文件中。如何收集要发送到“data”参数中的表单值?

$.ajax({
        type: "POST",
        data: "submit=1&username="+username+"&email="+email+"&password="+password+"&passconf="+passconf,
        url: "http://rt.ja.com/includes/register.php",
        success: function(data)
        {   
            //alert(data);
            $('#userError').html(data);
            $("#userError").html(userChar);
            $("#userError").html(userTaken);
        }
    });

HTML:

HTML:

<div id="border">
  <form  action="/" id="registerSubmit">
    <div id="userError"></div>
      Username: <input type="text" name="username" id="username" size="10"/><br>
      <div id="emailError" ></div>
      Email: <input type="text" name="email" size="10" id="email"/><br>
      <div id="passError" ></div>
      Password: <input type="password" name="password" size="10" id="password"/><br>
      <div id="passConfError" ></div>
      Confirm Password: <input type="password" name="passconf" size="10" id="passconf"/><br>
      <input type="submit" name="submit" value="Register" />
  </form>
</div>

6 个解决方案

#1


114  

Use the serialize method:

使用序列化方法:

$.ajax({
    ...
    data: $("#registerSubmit").serialize(),
    ...
})

Docs: serialize()

文档:序列化()

#2


7  

$("#registerSubmit").serialize() // returns all the data in your form
$.ajax({
     type: "POST",
     url: 'your url',
     data: $("#registerSubmit").serialize(),
     success: function() {
          //success message mybe...
     }
});

#3


5  

you can use val function to collect data from inputs:

您可以使用val函数从输入中收集数据:

jQuery("#myInput1").val();

http://api.jquery.com/val/

http://api.jquery.com/val/

#4


2  

var data={
 userName: $('#userName').val(),
 email: $('#email').val(),
 //add other properties similarly
}

and

$.ajax({
        type: "POST",
        url: "http://rt.ja.com/includes/register.php?submit=1",
        data: data

        success: function(html)
        {   
            //alert(html);
            $('#userError').html(html);
            $("#userError").html(userChar);
            $("#userError").html(userTaken);
        }
    });

You dont have to bother about anything else. jquery will handle the serialization etc. also you can append the submit query string parameter submit=1 into the data json object.

你不必为别的事操心。jquery将处理序列化等,您还可以将提交查询字符串参数submit=1附加到数据json对象中。

#5


0  

var username = $('#username').val();
var email= $('#email').val();
var password= $('#password').val();

#6


0  

try as this code.

试一试这段代码。

$.ajax({
        type: "POST",
        url: "http://rt.ja.com/includes/register.php?submit=1",
        data: "username="+username+"&email="+email+"&password="+password+"&passconf="+passconf,

        success: function(html)
        {   
            //alert(html);
            $('#userError').html(html);
            $("#userError").html(userChar);
            $("#userError").html(userTaken);
        }
    });

i think this will work definitely..

我想这一定能行得通。

you can also use .serialize() function for sending data via jquery Ajax..

您还可以使用.serialize()函数通过jquery Ajax发送数据。

i.e: data : $("#registerSubmit").serialize()

Thanks.

谢谢。

#1


114  

Use the serialize method:

使用序列化方法:

$.ajax({
    ...
    data: $("#registerSubmit").serialize(),
    ...
})

Docs: serialize()

文档:序列化()

#2


7  

$("#registerSubmit").serialize() // returns all the data in your form
$.ajax({
     type: "POST",
     url: 'your url',
     data: $("#registerSubmit").serialize(),
     success: function() {
          //success message mybe...
     }
});

#3


5  

you can use val function to collect data from inputs:

您可以使用val函数从输入中收集数据:

jQuery("#myInput1").val();

http://api.jquery.com/val/

http://api.jquery.com/val/

#4


2  

var data={
 userName: $('#userName').val(),
 email: $('#email').val(),
 //add other properties similarly
}

and

$.ajax({
        type: "POST",
        url: "http://rt.ja.com/includes/register.php?submit=1",
        data: data

        success: function(html)
        {   
            //alert(html);
            $('#userError').html(html);
            $("#userError").html(userChar);
            $("#userError").html(userTaken);
        }
    });

You dont have to bother about anything else. jquery will handle the serialization etc. also you can append the submit query string parameter submit=1 into the data json object.

你不必为别的事操心。jquery将处理序列化等,您还可以将提交查询字符串参数submit=1附加到数据json对象中。

#5


0  

var username = $('#username').val();
var email= $('#email').val();
var password= $('#password').val();

#6


0  

try as this code.

试一试这段代码。

$.ajax({
        type: "POST",
        url: "http://rt.ja.com/includes/register.php?submit=1",
        data: "username="+username+"&email="+email+"&password="+password+"&passconf="+passconf,

        success: function(html)
        {   
            //alert(html);
            $('#userError').html(html);
            $("#userError").html(userChar);
            $("#userError").html(userTaken);
        }
    });

i think this will work definitely..

我想这一定能行得通。

you can also use .serialize() function for sending data via jquery Ajax..

您还可以使用.serialize()函数通过jquery Ajax发送数据。

i.e: data : $("#registerSubmit").serialize()

Thanks.

谢谢。