Friday, 30 July 2010 10:02

How to submit a form using jQuery and PHP

Written by Chris Jacoby
  • Print
  • E-mail
  • Share |
  • Be the first to comment!
Want to submit a form without having to reload or refresh the current page you're on? jQuery can help you achieve this, and this kind of functionality is something we see more and more every day on modern websites. The first thing I wanted to learn about jQuery when I first started using it a few years back, was how to submit and retrieve information without having to reload a page.

In this tutorial I want to show you how you can submit a form using the jQuery framework. Basic PHP programming knowledge is required for the server side posting.

Step 1: Including the jQuery library on your web page

Create a page called "example.html" and save it. To get started the first thing you will need is to include the jQuery library on your page. You can include this script in one of two ways:
  1. Download jQuery (latest build) from www.jquery.com and include the JavaScript in-between your head tags.
  2. Include the following script in your head tags:
        
        
Step 2: Create your HTML form

Below I have set up a simple form that will be used to submit the information to the PHP controller which can then submit the information to a MySQL database to store the information. For the example below the end result will be pre-pended onto the results area.


Step 3: Add your JavaScript to the page

Add the below javascript to your page once you have created your HTML form. The JavaScript can be added in the head tag AFTER the JavaScript you added in Step 1.

//open script
function submit_form() {
	//retrieve the values from your input fields using the ID element
	var nickname = $('#nickname').val();
	var message = $('#message').val();
	//check if the values are not empty
	if(nickname == "") {
		alert('Please enter your nickname!');
		$('#nickname').focus();
		return false;
	}
	if(message == "") {
		alert('Please enter a message!');
		$('#message').focus();
		return false;
	}
	//the page you would like to send the information to
	var page = "test.php";
	//below the form will get submitted to your specified page
	$.post(page, { value1: nickname, value2: message },
	function(data){
		$('#result').prepend(data);
	});
}
//close script

Step 4: Create the PHP controller

Before testing the form we need to setup the PHP controller page where the information will get submitted (posted) to. The PHP controller will handle the requests sent and return the data back to your website.

The PHP controller can also further be modified to save the data to your database, email the data to your email address or anything else you need it to do.

Create a PHP page called "controller.php" and save it in the same directory as your html form. Add the following code to your PHP document.

//open PHP tag
//POSTING THE INFORMATION 
$nickname 	= htmlspecialchars(trim($_POST['value1']));  
$message	= htmlspecialchars(trim($_POST['value2']));  

/*
Here you can setup your mysql information to save the queries to your database, or set-up an emailer or anything you want to do with the submitted information.
*/

//RETURN THE DATA YOU WISH TO DISPLAY OR TEST ETC
echo '
'.$nickname.' says:
'.$message.'
'; exit(0); //close PHP tag

Step 5: Testing your form and displaying the results

Now that you have set up your PHP controller and HTML form, you are now ready to test it. Try it! Type in something under nickname and message and click the submit button. You should now see your results below the HTML form.

If your form is not working, please make sure that you have read every step properly. You can also download a copy of this tutorial to use on your website.

Click here to view a live example of this tutorial

Click here to download this tutorial

Enjoy your new jQuery based form! :)how-to-submit-a-form-using-jquery-and-PHP
Last modified on Friday, 30 July 2010 11:09
Chris Jacoby

Chris Jacoby

Chris is an Information Systems Engineer who has great experience with the online web world. His strengths lie in website application development, user friendly website design and development, graphics design and website optimization. 

Nothing is impossible! ~Chris


E-mail: This e-mail address is being protected from spambots. You need JavaScript enabled to view it

Add comment



Call Us: +27 11 612 7460
LiveZilla Live Help