Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - Getting form values for ajax POST

Tags:

jquery

ajax

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({         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> 
like image 759
user547794 Avatar asked Sep 15 '11 05:09

user547794


People also ask

Can we submit form using Ajax?

We can submit a form by ajax using submit button and by mentioning the values of the following parameters. type: It is used to specify the type of request. url: It is used to specify the URL to send the request to. data: It is used to specify data to be sent to the server.

How can I get form data with javascript jQuery?

The serializeArray() method creates an array of objects (name and value) by serializing form values. This method can be used to get the form data. Parameter: It does not accept any parameter.

What is jQuery form data?

The jQuery Ajax formData is a method to provide form values like text, number, images, and files and upload on the URL sever. The jQuery Ajax formData is a function to create a new object and send multiple files using this object.


2 Answers

$("#registerSubmit").serialize() // returns all the data in your form $.ajax({      type: "POST",      url: 'your url',      data: $("#registerSubmit").serialize(),      success: function() {           //success message mybe...      } }); 
like image 44
Ritchie Avatar answered Sep 23 '22 01:09

Ritchie


Use the serialize method:

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

Docs: serialize()

like image 198
Robin Avatar answered Sep 24 '22 01:09

Robin