I have a simple form that I’m looking to add ajax to to load the results of the form on the same page, as oppose to sending the user off to a new page.
Here is the form below:
<HTML>
<HEAD>
<title>Unit Converter</title>
<link rel="icon" type="image/png" href="convert.png">
<link rel="StyleSheet" media="screen" href="basic.css">
</HEAD>
<BODY>
<h1>unit converter</h1>
<form action="convert.php" method="post">
What do you want to convert?<br>
<select name="conversionType">
<option value="null" selected="selected">Select unit</option>
<!-- Length -->
<option value="unit1">unit1</option>
</select>
<br><br>
How many units?<br>
<input type="text" name="conversionInput">
<br><br>
<input type="submit" value="Convert">
</form>
<p id="footer">
</p>
</BODY>
</HTML>
As you you can see on submit the form loads convert.php
How can I incorporate ajax to ensure that the results load within the same page?
One possibility is to use jQuery’s [.ajax()](http://api.jquery.com/jQuery.ajax/) method to submit the form to your PHP script and to display whatever results it returns.
In your case a very basic example would look something like this:
No, there’s a bit more to it than that I’m afraid.
You have to attach an onsubmit event handler to your form, call the $.ajax() method from within that, then prevent your forms default action so that it doesn’t submit twice.