Apache, Ajax and $_POST

So I have here a fresh installation of openSUSE, with Apache2, PHP5 and all the necessary whozits as one would expect. Thing is, I’ve run into a very strange bug. My $_POST isn’t being populated through ajax posts.


<?php
    if (!empty($_POST)) {
        print_r($_POST);
        die;
    }
?>
<html>
    <body>
       <script src="jquery.js" type="text/javascript"></script>
       <script type="text/javascript">
           function postWithAjax() {
               $.post("test.php", {
                   sentBy: "ajax",
               });
           }
        </script>
        <form name="input" action="test.php" method="post">
            <input type="hidden" name="sentBy" value="formSubmit">
            <input type="submit" value="Ajax Post" onclick='postWithAjax(); return false;'>
            <input type="submit" value="Form Submit" onclick='testpost();'>
        </form>
    </body>
</html>

When I do a regular form submit, $_POST is populated as we’d expect, but the ajax post responds with the whole HTML as $_POST is empty. Empty? It shouldn’t be empty; I just posted a variable.

I’m stumped. Is this something strange with my Apache/PHP settings that I’m supposed to adjust to allow ajax posts? Nothing’s been altered. It’s all out-of-the-box config.

If it’s of any help, here’s the info Firebug provides me with in response to the ajax post.


**Response Headers**
Date	Thu, 07 Aug 2008 16:47:05 GMT
Server	Apache/2.2.8 (Linux/SUSE)
X-Powered-By	PHP/5.2.6
Content-Length	31
Keep-Alive	timeout=15, max=98
Connection	Keep-Alive
Content-Type	text/html

**Request Headers**
Host	workspaces
User-Agent	Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.0.1) Gecko/2008070400 SUSE/3.0.1-0.1 Firefox/3.0.1
Accept	*/*
Accept-Language	en-us,en;q=0.5
Accept-Encoding	gzip,deflate
Accept-Charset	ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive	300
Connection	keep-alive
Content-Type	application/x-www-form-urlencoded; charset=UTF-8
X-Requested-With	XMLHttpRequest
Referer	http://localhost/test.php
Content-Length	11
Pragma	no-cache
Cache-Control	no-cache

**Post Data**
sentBy	ajax

From tail /var/log/apache/access_log:

127.0.0.1 - - [07/Aug/2008:10:11:51 -0700] “POST /test.php HTTP/1.1” 200 31 “http://localhost/test.php” “Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.0.1) Gecko/2008070400 SUSE/3.0.1-0.1 Firefox/3.0.1”

Frustration grows. I tried this code on a friend’s Ubuntu installation and it worked without a hitch. Hrm.

Just tried enabling $HTTP_RAW_POST_DATA with php.ini’s always_populate_raw_post_data, but no luck there either.

BradBeattie wrote:

> Code:
> --------------------
>
> <?php
> if (!empty($_POST)) {
> print_r($_POST);
> die;
> }
> ?>
> <html>
> <body>
> <script src=“jquery.js” type=“text/javascript”></script>
> <script type=“text/javascript”>
> function postWithAjax() {
> $.post(“test.php”, {
> sentBy: “ajax”,
> });
> }
> </script>
> <form name=“input” action=“test.php” method=“post”>
> <input type=“hidden” name=“sentBy” value=“formSubmit”>
> <input type=“submit” value=“Ajax Post” onclick=‘postWithAjax(); return
> false;’> <input type=“submit” value=“Form Submit” onclick=‘testpost();’>
> </form>
> </body>
> </html>

your function “$.post()” is not defined/shown here, and the loss of $_POST
contents likely occurs in there. Please could you post the code for
$.post()?


L R Nix
lornix@lornix.com

$.post comes from jQuery, which is documented at Ajax/jQuery.post - jQuery JavaScript Library. The script works fine on the other machines nearby (non-openSUSE), so the issue isn’t script related. Something’s odd with the config.

You can try this on your machine by replacing the first script’s source from jquery.js to http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js.

BradBeattie wrote:

>
> $.post comes from jQuery, which is documented at ‘Ajax/jQuery.post -
> jQuery JavaScript Library’
> (http://docs.jquery.com/Ajax/jQuery.post#examples). The script works
> fine on the other machines nearby (non-openSUSE), so the issue isn’t
> script related. Something’s odd with the config.
>
> You can try this on your machine by replacing the first script’s source
> from jquery.js to
> http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js.
>
>

Ah, thank you. Learned something new today! jquery!

While much of what I know about jquery is new to me, I don’t see how you’re
going to get php and jquery to trade variables.

php is server-side, and runs in the apache server’s php context, while
jquery/ajax is client-side and runs in the user’s browser.

They don’t connect.

Nothing I can find gives an easy way to obtain http post data from the
client-side unless it’s explicitly given to it by the server-side.

Perhaps if your PHP code was to ‘encode’ or ‘hide’ the post data in the html
somehow, which could then be parsed by ajax… that would work, but be
unsecure and slow too.

Loni

L R Nix
lornix@lornix.com

With all due respect, clients have ways of getting data to the server. One of these ways is HTTP post. AJAX is a fairly common method of doing this. I appreciate your assistance, but this script works fine on every other machine I’ve tried it on and isn’t the source of the bug. I’m wondering if this is specific to openSUSE or if there’s something else going on I’m not aware of.

$_POST is getting populated and you are getting the data, which is literally: sentBy: ajax, as firebug shows, and also the access log shows (31 bytes returned). However the problem is in the way you display your $_POST:

if (!empty($_POST)) {
        print_r($_POST);
        die;
}

The problem is you were supposed to return HTML, not plain text. So you should at least put some HTML around it if you want your browser to display it.

echo '<html><body><pre>';
if (!empty($_POST)) {
        print_r($_POST);
}
echo '</pre></body></html>';

Unfortunately, this isn’t the issue either. I can split the code into two files to make this a little cleaner.


**test.php**
<html>
    <body>
       <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js" type="text/javascript"></script>
       <script type="text/javascript">
           function postAjaxData() {
               $.post("test2.php", {
                   sentBy: "ajax",
               });
           }
        </script>
        <form name="input" action="test2.php" method="post">
            <input type="hidden" name="sentBy" value="formSubmit">
            <input type="submit" value="Ajax Post" onclick='postAjaxData(); return false;'>
            <input type="submit" value="Form Submit">
        </form>
    </body>
</html>


**test2.php**
<?php
    echo '<html><body><pre>';
    if (!empty($_POST)) {
        print_r($_POST);
    }
    echo '</pre></body></html>';
?>

The result is the same. The “Form Submit” button displays the expected content of $_POST, but the “Ajax Post” button responds with

<html><body><pre></pre></body></html>

Again, the issue isn’t with the script. I’ve run this test script on other machies without issue; It was one of the first things I did to confirm I wasn’t going crazy. The issue is with the system configuration, which is essentially default.

BradBeattie wrote:

>
> With all due respect, clients have ways of getting data to the server.
> One of these ways is HTTP post. AJAX is a fairly common method of doing
> this. I appreciate your assistance, but this script works fine on every
> other machine I’ve tried it on and isn’t the source of the bug. I’m
> wondering if this is specific to openSUSE or if there’s something else
> going on I’m not aware of.
>
>

No worries. {Grin}

I’d still like to see how a client-side script (javascript/ajax) gets data
that’s only available via $_POST variable in a server-side php script.

Not saying it’s not doable, just that in my humble ignorance I cannot see
how it would work without some magic, which I cannot see.

{Chuckle} Obviously I’m not smart enough…

Annnd, I just realized your trying to SEND post data, not receive it…

Yup, blonde moment. My most profound apologies.

The ‘die();’ function only stops execution of the PHP script, it does not
stop the sending nor parsing of the rest of the page. If the ajax/jquery
sends anything, it would be visible in the source code of the returned
page, up at the beginning, BEFORE the html/body tags.

Ken has a workable solution in a following message, but it then creates a
weird source code with two “pages”.

I did notice that you have a trailing comma in your $.post function call

Currently:

$.post(“test.php”, { sentBy: “ajax”, } );

probably (but I’m an idiot) should be:

$.post(“test.php”, { sentBy: “ajax” } );

{Smile}

Looks like it works to me… but maybe I have a different concept
of ‘works’. Probably a blonde thing.

Take Care, and good luck.

Loni


L R Nix
lornix@lornix.com

Can you try removing the !empty() test? print_r won’t mind if you give it an empty object. $_POST is actually an associative array.

Ah hah! The problem is with Firebug 1.2.0b7 and Firefox 3.0.1-0.1.

I modified test2.php to this:


<?php
if (!empty($_POST)) {
    print_r("Yep: ");
    print_r($_POST);
} else {
    print_r("Nope.");
}
?>

and modified test.php to alert() the response. Well, the alert comes through with “Yep: …” and the Firebug tab comes back with “Nope.”. I gather Firebug is making some kind of auxiliary request to populate it’s response body.

Oh frustration relieved! System config it was.