0

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.

jhonkola
  • 3,316
  • 1
  • 15
  • 31
4713n
  • 39
  • 1
  • 4

2 Answers2

6

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?

Community
  • 1
  • 1
n00dle
  • 5,596
  • 2
  • 31
  • 48
  • 3
    Or $_REQUEST to catch both POST and GET type requests (and cookies, sadly). – Tino Didriksen Oct 30 '12 at 12:31
  • That's true. I always feel a little bit dirty using `$_REQUEST` though, unless there's no way I can know if I should be receiving `GET` or `POST`. In 99% of cases though, you know which you'll be getting and it seems like accepting from both could open up insecurities... Just a thought though - may well just be my paranoia! – n00dle Oct 30 '12 at 12:35
  • It's still not working. I tried $_POST and $_GET before, but it seems like php just ignores it. Even when I manually try to open localhost/?text1=blabla in browser, it doesnt work. – 4713n Oct 30 '12 at 12:40
  • What function is "some_function()"? The code you've got should work for the type of manual get request you mention. It sounds like your "some_function" isn't working as you'd expect. – n00dle Oct 30 '12 at 12:49
  • Sorry, its working now. It was something with POST as you said. Thanks. – 4713n Oct 30 '12 at 12:52
1

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

Clyde Lobo
  • 8,747
  • 6
  • 34
  • 58
SHAKIR SHABBIR
  • 1,205
  • 1
  • 14
  • 24
  • To some extent I agree - it could do with refactoring so that the PHP block is before the block and includes an `exit` or `die` if the POST exists. That way you'll only get the result of the function sent back to jQuery... – n00dle Oct 30 '12 at 15:15