The code: (index.php
):
<head>
<?php
if(isset($_GET['text1']))
{
some_function();
}
?>
</head>
<body>
<form id="form">
<input type="text" id="text1" name="text1"/>
<input type="text" id="text2" name="text2"/>
<input type="submit" id="submit" value="submit"/>
</form>
<script>
$(document).ready(function(){
$("#submit").click(function(){
var dataString=$("form#form").serialize();
var proccessPage="<?php echo $_SERVER['PHP_SELF'] ?>";
alert(dataString);
$.ajax({
type: "POST",
url: proccessPage,
data: dataString,
});
return false;
});
});
</script>
</body>
Anyone know why some_function();
call is not triggered? I don't know if the problem is in ajax, or php, or where. I have spent two days in trying to use $.post
, or $.ajax
, searching for possible errors in code etc, but I cant find anything wrong with it.
Do you know something about it? Thanks for all answers.
Because you're looking for GET
not POST
. Change your PHP to the following:
<?php
if(isset($_POST['text1']))
{
some_function();
}
?>
GET
and POST
are different types of HTTP requests and you need to make sure your PHP code is looking for the right one. There's a fairly comprehensive description of the difference between them on this question: What is the difference between POST and GET?
This wont work anyhow!
The reason is you are making an AJAX POSTBACK which is Asynchronus.
In order to get what you need, you have to try this way:
<form id="form" action="" method="post">
and remove the script part. It will work!
~Shakir Shabbir