0

I just want receive the array and print it which is sent by jquery.post()

my HTML-JS

<input type="submit"  id="myButton" />

<script type="text/javascript">

    var arr = new Array();
    arr[0] = "Ding";
    arr[1] = "Dong";
    arr[2] = "Bong";

    var json_string= JSON.stringify(arr); // convert it into a json string. 

    //and sending the data to server. temp.php.
    $("#myButton").click(function(){
    $.post('temp.php' , {json : json_string },function(data){

                       location.href = "temp.php";
         }); 

    });

</script>

I have checked by alert() that data sending is successful. After button click page is also redirecting to temp.php

My php page temp.php

<?php
    $json_string = $_POST['json']; //geting the post request. //this is line 3
    $array_items = json_decode($json_string); //converting json string to a php array.
    echo $array_items[0]; //just to print 1st element of array
 ?>

It should print Ding. But I am getting error

Undefined index: json in C:\wamp\www\phpJSarray\temp.php on line 3

What is my mistake in temp.php page ? Please help me out.

14
  • 2
    "my HTML-JS" which is completed commented out in your question, btw. Commented Dec 27, 2013 at 1:16
  • in your temp.php you should add some debug code to make sure you are getting what you think you should. on line 4 you could print $json_string; and if that's good, on line 6 try print_r($array_items); to see if you have an array with expected values. Commented Dec 27, 2013 at 1:16
  • Where is your form and PHP? Since you're using $json_string = $_POST['json']; The possibilities of an error are rather broad without seeing full codes. Is there not a named element such as name="json" ? Commented Dec 27, 2013 at 1:16
  • Did you check the network tab of your javascript console / firebug? Does the data get sent properly? Commented Dec 27, 2013 at 1:19
  • 1
    why are you redirecting after a ajax request? Commented Dec 27, 2013 at 1:22

4 Answers 4

4

call this javascript on your onclick or on form submit however u like.

var arr = new Array();
arr[0] = "Ding";
arr[1] = "Dong";
arr[2] = "Bong";

var json_string = JSON.stringify(arr); // convert it into a json string.
$("#data").val(json_string);    
$("#form1").submit(); //and sending the data to server. temp.php.

HTML:

<form action="temp.php" id="form1" method="POST">
    <input type="hidden" id="data" name="data">
</form>

temp.php

$dataArray = json_decode($_POST['data']);
Sign up to request clarification or add additional context in comments.

1 Comment

@demo_Ashif if this post solved your question, please click the checkbox to mark it as accepted.
2

The $.post is doing an AJAX call on the current page... which has no apparent effect because once it finishes, the location.href = "temp.php"; is redirecting you to temp.php.

If you'd like to make a POST to temp.php, you could just do it through a normal form. Use jQuery to set the value of a hidden input element to json_string if desired (or better, let jQuery change the form data to JSON).

Comments

2
<script>

$(document).ready(function() {    
   $("#myButton").on( 'click', function () {
    $.ajax({
        type: 'post',
        url: 'temp.php',
        data: {
            arr1: "Ding",
            arr2: "Dong",
            arr3: "Bong",
        },
        success: function( data ) {
            console.log( data );
        }
    });
});
});

</script>

You'd then call it by doing $_POST['arr1'];. I hope this solves your problem.

7 Comments

json_string is alrdy encoded in json. I am calling the sent post request of json. And later decode is as php array. Btw i have tried with $_POST['json_string']. And same error
mistake is in my javascript code. but i am not getting any idea to solve it. Plz check if you can.
your code is ok and geting success alert. now what should i do to print the array data in temp.php page. Now i am reloading temp.php but not getting any data.
i just want print the sent array data in temp.php page
So you don't want to do anything with it like store it anywhere? Because the code that I written doesn't actually let you see it anywhere, it will just post the data to that page, it doesn't actually take you to it, therefore you can only store it in a database, write it to somewhere, etc.
|
0

After you send your data via AJAX to temp.php and it returns a successful response, redirect to temp.php with an empty POST happens. Thats why you get no data after redirect.

You should also check whether array element exists in temp.php:

<?php

    if (isset($_POST['json'])) {
        $json_string = $_POST['json']; 
        $array_items = json_decode($json_string); 
        echo $array_items[0]; 
    }

?>

Comments

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.