Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. Linux, Apache, MySQL, PHP
  4. post data missing after php parses raw post to post variable

post data missing after php parses raw post to post variable

Scheduled Pinned Locked Moved Linux, Apache, MySQL, PHP
htmljavascriptphptoolsquestion
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    cjoki
    wrote on last edited by
    #1

    I ran across this a few years ago and found I could just handle the raw post data myself. Recently I had to write a script to handle an ajax call, from grease monkey in fact, and the raw post data is there and the post variable is set but it is blank. I would prefer to use the post variable instead of having to manually parse the data. A sample html/js/php fragments of what I am doing follows:

    <?php
    fwrite($fp,file_get_contents("php://input"));
    ?>

    I can read the raw post data. But when I use something like

    <?php
    echo $_POST['name'];
    ?>

    it is blank My source data is built via js like this...

    js...
    var postvar = "name="+encodeURI(name)+"&markup="+encodeURI(markup)
    ...js

    Name is a identifier and markup is a select html code. I am curious if anyone has figured out why php fails to parse the raw data sent to a page.

    F 1 Reply Last reply
    0
    • C cjoki

      I ran across this a few years ago and found I could just handle the raw post data myself. Recently I had to write a script to handle an ajax call, from grease monkey in fact, and the raw post data is there and the post variable is set but it is blank. I would prefer to use the post variable instead of having to manually parse the data. A sample html/js/php fragments of what I am doing follows:

      <?php
      fwrite($fp,file_get_contents("php://input"));
      ?>

      I can read the raw post data. But when I use something like

      <?php
      echo $_POST['name'];
      ?>

      it is blank My source data is built via js like this...

      js...
      var postvar = "name="+encodeURI(name)+"&markup="+encodeURI(markup)
      ...js

      Name is a identifier and markup is a select html code. I am curious if anyone has figured out why php fails to parse the raw data sent to a page.

      F Offline
      F Offline
      fly904
      wrote on last edited by
      #2

      cjoki wrote:

      <?php
      echo $_POST['name'];
      ?>

      Try print_r( $_REQUEST ); to see what was sent. Without seeing the rest of your javascript I can only guess at what the problem is. The most common problems are that the http request was opened with 'GET' rather than 'POST' eg. http_request.open('POST', url, true); Or the parameters weren't sent eg. http_request.send(postvar); Or I may have missed the point, so please clarify.

      If at first you don't succeed, you're not Chuck Norris.

      C 1 Reply Last reply
      0
      • F fly904

        cjoki wrote:

        <?php
        echo $_POST['name'];
        ?>

        Try print_r( $_REQUEST ); to see what was sent. Without seeing the rest of your javascript I can only guess at what the problem is. The most common problems are that the http request was opened with 'GET' rather than 'POST' eg. http_request.open('POST', url, true); Or the parameters weren't sent eg. http_request.send(postvar); Or I may have missed the point, so please clarify.

        If at first you don't succeed, you're not Chuck Norris.

        C Offline
        C Offline
        cjoki
        wrote on last edited by
        #3

        fly904, thanks for the response but I found my error. Using the GM_xmlhttpRequest object I mistakely set the content type to...

        GM_xmlhttpRequest({
        method: 'POST',
        url: '[URL deleted for post]/copy_prdt.php',
        data: postdata,
        headers:
        {
        'Content-type': 'x-www-form-urlencoded'
        },
        onload: function(responseDetails)
        {
        alert('Request returned ' + responseDetails.status + ' ' + responseDetails.statusText + '\n\n' +' data:\n' + responseDetails.responseText);
        },
        onError: function(responseDetails)
        {
        alert('Request complained ' + responseDetails.status + ' ' + responseDetails.statusText + '\n\n' +' data:\n' + responseDetails.responseText);
        }
        });

        and it needed to be...

        GM_xmlhttpRequest({
        method: 'POST',
        url: '[URL deleted for post]/copy_prdt.php',
        data: postdata,
        headers:
        {
        'Content-type': 'application/x-www-form-urlencoded'
        },
        onload: function(responseDetails)
        {
        alert('Request returned ' + responseDetails.status + ' ' + responseDetails.statusText + '\n\n' +' data:\n' + responseDetails.responseText);
        },
        onError: function(responseDetails)
        {
        alert('Request complained ' + responseDetails.status + ' ' + responseDetails.statusText + '\n\n' +' data:\n' + responseDetails.responseText);
        }
        });

        It is odd, I did not get any error messages and the script seem to work fine, except for the post variable not having the data parsed into it. I can only guess that the php parser was confused and quietly aborted the parsing of the post data.

        F 1 Reply Last reply
        0
        • C cjoki

          fly904, thanks for the response but I found my error. Using the GM_xmlhttpRequest object I mistakely set the content type to...

          GM_xmlhttpRequest({
          method: 'POST',
          url: '[URL deleted for post]/copy_prdt.php',
          data: postdata,
          headers:
          {
          'Content-type': 'x-www-form-urlencoded'
          },
          onload: function(responseDetails)
          {
          alert('Request returned ' + responseDetails.status + ' ' + responseDetails.statusText + '\n\n' +' data:\n' + responseDetails.responseText);
          },
          onError: function(responseDetails)
          {
          alert('Request complained ' + responseDetails.status + ' ' + responseDetails.statusText + '\n\n' +' data:\n' + responseDetails.responseText);
          }
          });

          and it needed to be...

          GM_xmlhttpRequest({
          method: 'POST',
          url: '[URL deleted for post]/copy_prdt.php',
          data: postdata,
          headers:
          {
          'Content-type': 'application/x-www-form-urlencoded'
          },
          onload: function(responseDetails)
          {
          alert('Request returned ' + responseDetails.status + ' ' + responseDetails.statusText + '\n\n' +' data:\n' + responseDetails.responseText);
          },
          onError: function(responseDetails)
          {
          alert('Request complained ' + responseDetails.status + ' ' + responseDetails.statusText + '\n\n' +' data:\n' + responseDetails.responseText);
          }
          });

          It is odd, I did not get any error messages and the script seem to work fine, except for the post variable not having the data parsed into it. I can only guess that the php parser was confused and quietly aborted the parsing of the post data.

          F Offline
          F Offline
          fly904
          wrote on last edited by
          #4

          cjoki wrote:

          I can only guess that the php parser was confused and quietly aborted the parsing of the post data.

          Debugging Ajax is a bitch, as it doesn't always throw an error, because technically it works although not the way you want it :( .

          If at first you don't succeed, you're not Chuck Norris.

          M 1 Reply Last reply
          0
          • F fly904

            cjoki wrote:

            I can only guess that the php parser was confused and quietly aborted the parsing of the post data.

            Debugging Ajax is a bitch, as it doesn't always throw an error, because technically it works although not the way you want it :( .

            If at first you don't succeed, you're not Chuck Norris.

            M Offline
            M Offline
            Marc Firth
            wrote on last edited by
            #5

            I tend to use an "output" box for this that just says what the code is doing step by step on js side and Firefox Error Console. But some errors do still seem to hide. A function you may know: To read headers sent using PHP you can use:

            <?php
            var_dump(headers_list());
            ?>

            Portfolio | Web Design, Web Hosting & IT Support

            C 1 Reply Last reply
            0
            • M Marc Firth

              I tend to use an "output" box for this that just says what the code is doing step by step on js side and Firefox Error Console. But some errors do still seem to hide. A function you may know: To read headers sent using PHP you can use:

              <?php
              var_dump(headers_list());
              ?>

              Portfolio | Web Design, Web Hosting & IT Support

              C Offline
              C Offline
              cjoki
              wrote on last edited by
              #6

              var_dump prints the results to a browser. In an ajax based system this can be messy. Although I could have used var_export, I instead used fwrite($fp,file_get_contents("php://input")); so to write the contents of the raw post data before the parser works on filling the $_POST array. Doing it this way allows the server side script of a ajax solution to continue and return some value so your javascript can shutdown gracefully while allowing you to capture the content. I also use a firefox addon called httpfox so I can observe the communication between my browser and the server.

              M 1 Reply Last reply
              0
              • C cjoki

                var_dump prints the results to a browser. In an ajax based system this can be messy. Although I could have used var_export, I instead used fwrite($fp,file_get_contents("php://input")); so to write the contents of the raw post data before the parser works on filling the $_POST array. Doing it this way allows the server side script of a ajax solution to continue and return some value so your javascript can shutdown gracefully while allowing you to capture the content. I also use a firefox addon called httpfox so I can observe the communication between my browser and the server.

                M Offline
                M Offline
                Marc Firth
                wrote on last edited by
                #7

                :thumbsup: httpfox :thumbsup: Been looking for something like that for a while. Plain & Simple.

                Portfolio | Web Design, Web Hosting & IT Support

                C 1 Reply Last reply
                0
                • M Marc Firth

                  :thumbsup: httpfox :thumbsup: Been looking for something like that for a while. Plain & Simple.

                  Portfolio | Web Design, Web Hosting & IT Support

                  C Offline
                  C Offline
                  cjoki
                  wrote on last edited by
                  #8

                  its good to share :-D

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups