0

I have the following code snippet below. When the "Create Order" button is clicked, the user inputted data (not shown in the code) is converted to JSON, via the function "convertToJson". I am able to convert the data to JSON. My problem is passing the JSON object to PHP so that I can insert the object to the database. PHP is returning a "Null" value when I run $var_dump.

Note that I am trying to POST to the same page, which is "orders.php"

     <head>
        <script type="text/javascript">
        function convertToJson()
        {
            var tableData = $('#productOrder').tableToJSON({
            ignoreColumns: [0]
            }); 

            var xhr = new XMLHttpRequest();
            xhr.open("GET","orders.php",true);
            xhr.setRequestHeader("Content-Type", "application/json");
            xhr.send(JSON.stringify(tableData));            
        }   
        </script>
     </head>
    ...more code...     

    <button type="button" class="btn btn-success btn-md" style="float: right;" onclick="convertToJson();">Create Order</button>
    <div>
    <?php
        $data = json_decode(file_get_contents('php://input'));
        var_dump($data);

    ?>
    </div>
1
  • Check if the request gets sent. Commented Mar 26, 2016 at 12:35

1 Answer 1

1

you should use an actual ajax request to do post your data

        $.ajax({
            url: 'yourscript',
            type: 'POST',
            data: { data: tableData }
            dataType: "json",
            beforeSend: function() {
              //do something
            }
        }).done(function(msg){
            //do something when done
        }).fail(function(msg){
            //error handling
        }).complete(function(msg){
            //do something when completed
        });

    }

and get the data with $_POST["data"]

also note that as stated before you need to take care of your script lifecycle as you are now working with async requests

Sign up to request clarification or add additional context in comments.

6 Comments

is it possible to post to the same page?
@wowomg No, posting to the same page results in this
sure you can post to the same script. you have to read the data ofcourse and do something with it. note the code sequence tho. My Recommendation however is to seperate the Code from the View completely and also handle the ajax reqiest in an explicit controller function
@SteVoi How does one post AJAX request to the same page that contains the ajax code block? I am aware it can be done, but not without some significant challenges that are well beyond the abilities of most novices. Specifying a secondary, independent, PHP page in the url: "secondary_page.php", parameter of the ajax code block is by far the easiest way for the OP to solve his problem, and is the way 90% of coders would solve it. Why would you remark that it can be done without pointing out the significant inherent difficulties with that approach?
@gibberish: just handle the ajax call on top of the script. again i do not recommend doing this but if you would like to its abolutely possible: if(ajax_data) { do somehting } Rest of the script. the proper way is to route the ajax call do a dedicated function, return the reply and handle the reply using JS in the actual script. Basically just like you recommended
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.