Sending Javascript Object to PHP via Ajax

Asked
Active3 hr before
Viewed126 times

6 Answers

sendingjavascriptobject
90%

As you are sending the data as json , use json_decode in php function to convert the json to PHP array and then you can parse it – Webdev Nov 10 '16 at 10:48 ,from the second ajax you can access the data based on the property names like: $_GET['data']['field01'] , 2 from the second ajax you can access the data like: $_GET['data']['field01'] – madalinivascu Nov 10 '16 at 10:46 , Thanks for assistance @RoryMcCrossan – TheLovelySausage Nov 10 '16 at 10:50

Please try to pass the array in the format of json. Then use the get the json in your php and access the json array.

<script>

arr_data = {
    field01: "data 01",
    field02: "data 02",
    field03: "data 03",
    field04: "data 04"
}
var  myJsonString= JSON.stringify(arr_data);
$.ajax({
    url: "scripts/php/phpfunc.php",
    type: "GET",
    dataType: "json",
    data: {
        'action': "exec_find",
        'data': myJsonString
    },
    success: function(result) {
        // continue program
    },
    error: function(log) {
        // handle error
    }
});
</script>

this is your java script. and below is the php

$dataJson = json_decode($_GET['data']);
load more v
88%

We included JQuery, which is a JavaScript library that makes it handy to send Ajax requests.,We created an example object called user. This object contains three properties: Two strings and an array.,This is a short guide on how to send a JavaScript object to a PHP script using an Ajax request. In this tutorial, I will use the JQuery library to send the Ajax request.,Let’s take a look at a simple example:

Let’s take a look at a simple example:

<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
<script>
var user = {
    'name': 'Wayne',
    'country': 'Ireland',
    'selected': new Array(1, 5, 9)
};
var userStr = JSON.stringify(user);
$.ajax({
    url: 'json-receive.php',
    type: 'post',
    data: {user: userStr},
    success: function(response){
        //do whatever.
    }
});
</script>
load more v
72%

Here's what I have tried:,How can I get this in php as a key value pair like row_ : LM#00000010, row_LM#00000002, row_LM#00000009,LM#00000008, so that I can loop each value? I tried JSON.stringify(), I got TypeError: cyclic object value,It might help to simplify what you're working with, the dataTables object might be what's going cyclic on you.,The console.log of deleteInvoice gave me:

I looked at some other questions such as this and this, but it does not me help me solve my problem. When I console-log my data, I get this:

Object["row_LM#00000010", "row_LM#00000002", "row_LM#00000009", "row_LM#00000008"]

Here's what I have tried:

            $.ajax({
               type: 'POST',
               url: 'delinvoices.php',
               data: {
                  del_id: JSON.stringify(deleteInvoice)
               },
               success: function(data) {
                  //other codes
               }
            });

The console.log of deleteInvoice gave me:

Object["row_LM#00000010", "row_LM#00000002", "row_LM#00000009", "row_LM#00000008"]

The base data comes from datatable

var deleteInvoice = dt.rows({
   selected: true
}).ids();
load more v
65%

You can pass the data to the PHP script as a JSON object. Assume your JSON object is like:,You can pass this object to the php code in two ways:,You can handle the data passed to the result.php as :,1. Pass the object as a string:

You can pass the data to the PHP script as a JSON object. Assume your JSON object is like:

var stuff = {
   'key1': 'value1',
   'key2': 'value2'
};
load more v
75%

2 AJAX POST Example, the jQuery way 2.1 Server side code for our AJAX form ,3.1 Posting JSON data with jQuery $.ajax()

So let's get our hands dirty. Here's our HTML5 and jQuery:

<!DOCTYPE html>
<html>
<head>
    <title>jQuery AJAX POST Form</title>
    <meta charset="utf-8">
</head>
<body>

<div id="response">
    <pre></pre>
</div>

<form id="my-form">
    <input type="text" id="first-name" name="first-name" placeholder="First Name" />
    <input type="text" id="last-name" name="last-name" placeholder="Last Name" />

    <button type="submit">Submit</button>
</form>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
    (function($){
        function processForm( e ){
            $.ajax({
                url: 'users.php',
                dataType: 'text',
                type: 'post',
                contentType: 'application/x-www-form-urlencoded',
                data: $(this).serialize(),
                success: function( data, textStatus, jQxhr ){
                    $('#response pre').html( data );
                },
                error: function( jqXhr, textStatus, errorThrown ){
                    console.log( errorThrown );
                }
            });

            e.preventDefault();
        }

        $('#my-form').submit( processForm );
    })(jQuery);
</script>
</body>
</html>
load more v
40%

Sometimes async = false are used for quick testing. You will also find synchronous requests in older JavaScript code.,By sending asynchronously, the JavaScript does not have to wait for the server response, but can instead:,Synchronous XMLHttpRequest (async = false) is not recommended because the JavaScript will stop executing until the server response is ready. If the server is busy or slow, the application will hang or stop.,Modern developer tools are encouraged to warn about using synchronous requests and may throw an InvalidAccessError exception when it occurs.

Send a Request To a Server

To send a request to a server, we use the open() and send() methods of the XMLHttpRequest object:

XMLHttpRequest
load more v

Other "sending-javascript" queries related to "Sending Javascript Object to PHP via Ajax"