Home » Php » Jquery AJAX post to PHP

Jquery AJAX post to PHP

Posted by: admin July 12, 2020 Leave a comment

Questions:

OK i’ve got my json string built but i’m not sure what to do next??

$('#submit').live('click',function(){ 

                var dataString = '[';
                    $('#items tr').not(':first').each(function(){
                        var index = $('#items tr').index(this);
                        var supp_short_code=$(this).closest('tr').find('.supp_short_code').text();
                        var project_ref=$(this).closest('tr').find('.project_ref').text();
                        var om_part_no=$(this).closest('tr').find('.om_part_no').text();
                        var description=$(this).closest('tr').find('.description').text();
                        var cost_of_items=$(this).closest('tr').find('.cost_of_items').text();
                        var cost_total=$(this).closest('tr').find('.cost_total').text();
                        dataString += '{"row":"' + index + '", "supp_short_code":"' + supp_short_code + '", "project_ref":"' + project_ref + '", "om_part_no":"' + om_part_no + '", "description":"' + description + '", "cost_of_items":"' + cost_of_items + '", "cost_total_td":"' + cost_total + '"}';
                    });
                    dataString += ']';

                $.ajax
                    ({
                    type: "POST",
                    url: "order.php",
                    data: dataString,
                    cache: false,
                    success: function()
                        {
                            alert("Order Submitted");
                        }
                    });
            });

In my php file i was attempting to write the dataString to a text file so i could see its coming through ok but nothing was in the text file!? Am i doing something wrong client side or PHP side, my php code:

<?php
    $stringData = $_POST['dataString']; 
    $myFile = "testFile.txt";
    $fh = fopen($myFile, 'w') or die("can't open file");
    fwrite($fh, $stringData);
    fclose($fh);
?>
How to&Answers:

Why don’t you try constructing your data like this

var postData = {};
$('#items tr').not(':first').each(function(index, value) {
    var keyPrefix = 'data[' + index + ']';
    postData[keyPrefix + '[supp_short_code]'] = $(this).closest('tr').find('.supp_short_code').text();
    postData[keyPrefix + '[project_ref]'] = $(this).closest('tr').find('.project_ref').text();
    // and so on
});

Then in your AJAX call

data: postData,

Now your PHP script can process the data as a multi-dimensional array

<?php
if (isset($_POST['data']) && is_array($_POST['data'])) {
    foreach ($_POST['data'] as $row => $data) {
        echo $data['supp_short_code'];
        echo $data['project_ref'];
        // and so on
    }
}

Answer:

This should do it:

...
$.ajax({
    type: "POST",
    url: "order.php",
    data: { 'dataString': dataString },
    cache: false,
    success: function()
        {
            alert("Order Submitted");
        }
    });

You may try to verify:

<?php
    $stringData = $_POST['dataString']; 
    echo $stringData;
?>

Answer:

First convert the json object to a string in js like this:

var json_string=JSON.stringify(json_object);

Then, pass it to PHP as a string and then in php decode it, like this :

<?php  
    $map = json_decode($_POST['json_string']); 
?> 

I hope this helps anyone just finding this thread…

Answer:

The problem will be that you are attempting to access a POST variable called “dataString” that does not exist. Just because you set the “data” property to the contents of a variable called “dataString” does not mean that your post variable will be called “dataString”.

You could try this:

data: { "dataString": dataString },

This passes an object to the jQuery function that has a property called “dataString” and the value of your actual data string. jQuery will take all the properties from this object (in this case just one) and set them as post variables on the HTTP request that it will eventually send to your PHP application. This allows you to access the data via the $_POST[“dataString”] call.

Steve

Answer:

I have problems when use:

url: "/folder/form.php",

And i have to use:

url: "folder/form.php",,