JQuery - Getting form values ​​for ajax POST

I am trying to send form values ​​via AJAX to a php file. How do I collect form values ​​to submit within the "data" parameter?

$.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:

 <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> 
+52
jquery ajax
Sep 15 2018-11-11T00:
source share
6 answers

Use the serialize method:

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

Documents: serialize ()

+108
Sep 15 '11 at 5:11
source share
 $("#registerSubmit").serialize() // returns all the data in your form $.ajax({ type: "POST", url: 'your url', data: $("#registerSubmit").serialize(), success: function() { //success message mybe... } }); 
+7
Aug 28 '13 at 14:03
source share

you can use val function to collect data from inputs:

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

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

+5
Sep 15 2018-11-11T00:
source share
 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 do not need to worry about anything else. jquery will handle serialization, etc., you can also add a submit submit = 1 query string parameter to the data json object.

+1
Sep 15 '11 at 5:13
source share

try 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 () to send data through jquery Ajax ..

 ie: data : $("#registerSubmit").serialize() 

Thank.

0
Sep 15 2018-11-11T00:
source share
 var username = $('#username').val(); var email= $('#email').val(); var password= $('#password').val(); 
0
Sep 15 '11 at 5:12
source share



All Articles