I’m attempting to POST a JSON string that I grab from a MySql database to another php page. Here’s my code for the parent page:
$ResultLists = array();
$counter = 0;
echo "<form target=\"_blank\" enctype=\"multipart/form-data\" method=\"post\" action=\"_popup.php\" name=\"compare\">";
while($data != NULL and $info = mysql_fetch_array( $data ))
{
Print "<tr>";
//..
//I printed some data here, but not relevant to problem
//..
$ResultLists[$counter] = $info['ResultList']; //gets JSON string
echo gettype($ResultLists[$counter]); //prints "string"
//echo $ResultLists[$counter]; //prints the whole list (it is a list of lists <--from python)
$var = json_decode($ResultLists[$counter]); //decode JSON string
echo $var[1][0]; //prints wonderfully (I am only printing one value in "array", but it works).
Print '<td><input type="checkbox" name="data[]" value="'.$ResultLists[$counter].'" /></td>';
Print '</tr>';
$counter++;
}
echo '<tr><td><input type="hidden" name="index" value="'.$counter.'" />';
echo '<input type="submit" value ="Compare Data"> </input></td></tr>';
On the child page, I try to get the data:
<?php
$index = $_POST['index'];
echo 'Number of rows: '.$index.'<br>';
$array = $_POST['data'];
$x = 0;
//echo gettype($array);
echo '<b>Type:</b> '.gettype($array[$x]).'<b> Length:</b> '.strlen($array[$x]).' <b>Data:</b> '.$array[$x].'<br>';
$var = json_decode($array[$x]);
echo gettype($var).strlen($var);
echo $var[1][0];
?>
Which produces (for example)
Number of rows: 8
Type: string Length: 2 Data: [[
NULL0
So, it doesn’t work. (It does this for every row of data I have submitted, so varying $x results in same output as long as its in the range of number of checkboxes I selected, if that makes sense.) Any ideas? Why does it show up as “[[” instead of the entire list? I can print the whole list on parent page and it works fine.
I have a suspicion its due to the presence of single quote ” ‘ ” characters, or something. Any ideas?
Edit:
So disabling magic quotes seems to have helped me post.
However, I’m having a strange issue, where it somehow submits all my data.
So when I get back data[] via POST, the length is the correct number of checkboxes that were checked, but the data it holds starts from the first row that is printed. Any ideas?
You may probably want to escape the HTML characters in the json-ized string using
htmlspecialchars. I.e.,