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.
This is what it might look like in your case:
Code:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Unit Converter</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<style>#myForm div{margin-bottom:15px;}</style>
</head>
<body>
<h1>unit converter</h1>
<form action="convert.php" method="post" id="myForm">
<div>
<label for="conversionType">What do you want to convert?</label>
<select name="conversionType" id="conversionType">
<option value="null" selected="selected" >Select unit</option>
<option value="unit1">unit1</option>
</select>
</div>
<div>
<label for="conversionInput">How many units?</label>
<input type="text" name="conversionInput" id="conversionInput">
</div>
<input type="submit" value="Convert">
</form>
<p id="footer"></p>
<script type="text/javascript">
$(document).ready(function() {
$("#myForm").submit(function(e) {
e.preventDefault();
type = $("#conversionType").val();
units = $("#conversionInput").val();
$.ajax({
type : "POST",
cache : false,
url : "convert.php",
data : 'conversionType=' + type + '&conversionInput=' + units,
success : function(data) {
$("#footer").text(data);
}
});
});
});
</script>
</body>
</html>
convert.php
PHP Code:
<?php
$type = $_POST['conversionType'];
$units = $_POST['conversionInput'];
echo "You entered $type and $units";
?>
Hopefully this should give you something to go on.
If you have any questions, just let me know.
P.S. You might notice that i have added <label> tags to your form. You can read about why this is good here: http://www.webbie.org.uk/usingthelabelelement.htm
Bookmarks