Use jQuery To Submit Form To PHP/MySQL
written by: ryan coughlin
Using jQuery to submit a form, is something you are seeing more and more of as jQuery’s popularity builds. When I first started reading about jQuery and Ajax all I wanted to do was learn how to submit a form using jQuery.
I was so confused and lost, seeing the code was so abstract to me. So in this tutorial, I want to show you how to use the jQuery framework to submit data through a form using jQuery to a PHP page that submits to a database.
We are going to take form data and submit it to a jQuery Ajax call then send that to a PHP page with the data. Check out a live example of this post.
YOU SHOULD HAVE AN IDEA OF
- Some principles of PHP
- Basic knowledge of JavaScript and/or jQuery framework
- MySQL
- XHTML to build the form
JQUERY FUNCTIONS
- submit() – documentation
- jQuery.ajax – documentation
HTML OUTLINE
<form id="submit" method="post"> <fieldset><legend>Enter Information</legend> <label for="fname">Client First Name:</label> <input id="fname" class="text" type="text" name="fname" size="20" /> <label for="lname">Client Last Name:</label> <input id="lname" class="text" type="text" name="lname" size="20" /> <button class="button positive"> <img src="../images/icons/tick.png" alt="" /> Add Client </button></fieldset> </form> <div class="success" style="display: none;">Client has been added.</div>
JQUERY TO COOK IT ALL
$(document).ready(function(){
$("form#submit").submit(function() {
// we want to store the values from the form input box, then send via ajax below
var fname = $('#fname').attr('value');
var lname = $('#lname').attr('value');
$.ajax({
type: "POST",
url: "ajax.php",
data: "fname="+ fname +"& lname="+ lname,
success: function(){
$('form#submit').hide(function(){$('div.success').fadeIn();});
}
});
return false;
});
});
The next section is the bulk of the code. jQuery makes it very nice and easy to create Ajax calls. As you see, we have type, url, data, and success. These are pretty self explanatory.
- TYPE – Are you going to use POST or GET
- URL – What is the URL you are sending the data to
- DATA – The data that you are sending the the URL to be processed
- SUCCESS – What should the function do after the call has been executed
Remember that all of these are explained at the jQuery page, you can find the documentation for the Ajax calls here. There is a slew of options you can run within this one jQuery.ajax() function.
The success part:
success: function(){
$('form#submit').hide(function(){$('div.success').fadeIn();});
}
Once the form is successfully submitted, the form will hide and thanks to jQuery callbacks, we then show the success message.
THE BACKEND – AJAX.PHP
Below is what our ajax.php file will look like, this will carry out the submission. Save this as ajax.php.
<?php
include ("../../inc/config.inc.php");
// CLIENT INFORMATION
$fname = htmlspecialchars(trim($_POST['fname']));
$lname = htmlspecialchars(trim($_POST['lname']));
$addClient = "INSERT INTO clients (fname,lname) VALUES ('$fname','$lname')";
mysql_query($addClient) or die(mysql_error());
?>
There you have it, a straight forward guide to to using jQuery, PHP, and a simple HTML form to submit a form. Hopefully this will be a help to users new to the jQuery/Ajax world! If you have any comments or questions get in touch with me.
Pingback: Retrieving Data with Jquery, AJAX, and PHP from a MySQL Database | DIGG LINK