Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Apache, Ajax and $_POST

  1. #1

    Unhappy 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.

    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>
    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.

  2. #2

    Default Re: Apache, Ajax and $_POST

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

    Code:
    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

  3. #3

    Default Re: Apache, Ajax and $_POST

    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.
    Last edited by BradBeattie; 07-Aug-2008 at 10:20. Reason: Tried something else

  4. #4
    Join Date
    Jun 2008
    Location
    Oklahoma, US
    Posts
    822

    Default Re: Apache, Ajax and $_POST

    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

  5. #5

    Default Re: Apache, Ajax and $_POST

    $.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.min.js.

  6. #6
    Join Date
    Jun 2008
    Location
    Oklahoma, US
    Posts
    822

    Default Re: Apache, Ajax and $_POST

    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.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

  7. #7

    Default Re: Apache, Ajax and $_POST

    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.

  8. #8
    Join Date
    Jun 2008
    Location
    UTC+10
    Posts
    9,686
    Blog Entries
    4

    Default Re: Apache, Ajax and $_POST

    $_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:

    Code:
    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.

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

  9. #9

    Default Re: Apache, Ajax and $_POST

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

    Code:
    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>
    Code:
    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
    Code:
    <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.

  10. #10
    Join Date
    Jun 2008
    Location
    Oklahoma, US
    Posts
    822

    Default Re: Apache, Ajax and $_POST

    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

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •