Sorry for the weird title. I have the following code that tests the post() function of jquery:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252" />
<title>Untitled Document</title>
<script type="text/javascript" src="../../jquery/jquery-1.4.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("form#f").submit(function(){
var result = "";
$.ajax({
async: false,
type: "POST",
url: "test.php",
dataType: "script",
success: function(data){
result = data;
}
});
alert(result);
});
}
</script>
</head>
<body>
<form method="post" action="test.php" id="f">
<input type="text" name="name" />
<input type="submit"/>
</form>
</body>
</html>
... and this is the test.php implementation:
<?php
header('Content-Type: text/plain');
echo "hello " . $_POST['name'];
?>
Why is the page shown instead of data appearing in the alert dialog in submit() function? Sorry I know that this is a n00b question. I'm not really good at Javascript.
return false;at the end of the function you assign to the submit action. I believe this will override the default behaviour which is to actually submit the form to the action url. - Endophage<input type='submit'/>to<input type='button'/>- Rafay