2

I have an array that i pass from javascript to php and in php page i am trying to put it in session to be used in the third page. The code is as below

JavaScript:

var table_row = [];

table_row[0] = [123,123,123];
table_row[1] = [124,124,124];
table_row[2] = [125,125,125];

var jsonString = JSON.stringify(table_row);

 $.ajax({
 type: "POST",
 url: "test1.php",
 dataType: "json",
 data: {myJSArray: jsonString},
 success: function(data) {
                        alert("It is Successfull");
                        }
     });

test1.php

<?php
session_start();
$check1 = $_POST['myJSArray'];
$_SESSION['array']= $check1;
echo $check1;
?>

test2.php

<?php 
session_start();
$test = $_SESSION['array'];
echo $test;
?>

on submit i call the function in javascript and the form takes me to test2.php. It is giving error on test2.php page Notice: Undefined index: array in C:\xampp\htdocs\test\test2.php on line 13

Any suggestions please do let me know.

Alz
  • 291
  • 5
  • 14
  • Try renaming that index to something else. In test1.php, make sure you are getting the JSON value you sent via ajax. – JayKandari Jul 16 '14 at 05:55
  • Does test1 echo correctly? To test, why not get your ajax script to temporarily echo the returned value? It should be the same as the JSON String you sent. – James Hunt Jul 16 '14 at 05:57

3 Answers3

1

You don't need to stringify yourself, jquery does it for you, if you stringify it, jQuery will believe you want a string instead

var table_row = [];

table_row[0] = [123,123,123];
table_row[1] = [124,124,124];
table_row[2] = [125,125,125];



 $.ajax({
 type: "POST",
 url: "test1.php",
 dataType: "json",
 data: {myJSArray: table_row},
 success: function(data) {
                        alert("It is Successfull");
                        }
     });

However, on the php side, you still need to decode it as it is always a string when you get it from $_POST. use json_decode to do it.

$check1 = json_decode($_POST['myJSArray']);
David Lin
  • 12,681
  • 5
  • 43
  • 41
0

look at your test2.php

<?php 
session_start();
$test = $_SESSION['array'];
echo $test;
?>

if it's only the code in the file then the error you got C:\xampp\htdocs\test\test2.php on line 13 is mindless, because there is not line 13,

but if you have something about the code you show us, may there be something echoed before? because session has to be started before any output,

otherwise I've tested whole script and works fine...

To check if session really started (otherwise $_SESSION will not work), try this:

if(session_id())
{
    echo "Good, started";
}
else 
{
    echo "Magic! strangeness";
}

if problem not found in test2.php you can check test1.php echo $_SESSION['array'] after saving it, and in your javascript callback function alert data param itself, I'm sure you can catch the problem by this way.

George Garchagudashvili
  • 6,657
  • 12
  • 39
  • 53
  • thank you for the answer. i have checked both test1.php and test2.php. if i give hardcoded value both work. the issue is not isolated to javascript n ajax. The value is not coming from ajax to test1.php where it can be collected by $check1 = $_POST['myJSArray']; – Alz Jul 16 '14 at 06:46
  • then you can use browser's developers tool and check `network` tab, there you can see any ajax request, with responses, etc... – George Garchagudashvili Jul 16 '14 at 06:49
  • im using mozilla firefox, where can i gind this developers tool there and how to check – Alz Jul 16 '14 at 07:33
  • OK then right click anywhere on document choose 'Inspect Element' (Ctrl+Shift+k) and you will see `Network` tab, keep it open, next reload page (or if you do ajax call by clicking on button etc.., just make ajax call) and you will see network activity (file includes, ajax calles) with status and click appropriate item for detailed info – George Garchagudashvili Jul 16 '14 at 07:46
0

i got it to work, the code is below

Javascript file: in page index.php

Either you can call this function and pass parameter or use code directly

var table_row = []; //globally declared array
var table_row[0]=["123","123","123"];
var table_row[1]=["124","124","124"];
var table_row[2]=["125","125","125"];

    function ajaxCode(){
            var jsonArray = JSON.stringify(table_row)
             $.ajax
                        ({
                            url: "test1.php",
                            type: "POST",
                             dataType: 'json',
                            data: {source1 : jsonArray},
                            cache: false,
                            success: function (data) 
                            {
                             alert("it is successfull")

                             }

                        });
            } 

Page: test1.php

session_start();
unset($_SESSION['array']); 

$check1 = $_POST['source1'];    

$_SESSION['array']= $check1;
echo json_encode(check1);

Page: test2.php //final page where i wanted value from session

  if(session_id())
   {
    echo "Session started<br>";

    $test = $_SESSION['array'];
    echo "The session is".$test;

      }
   else 
  {
echo "Did not get session";
  }

 ?>

In index page i have a form that is submitted and on submission it calls the ajax function. Thank you for the help, really appreciate it.

Alz
  • 291
  • 5
  • 14