So, I'm passing an array via a hidden form field and using json to get it across correctly. When I decode the json after passing, it fails.
I made a quick sample file on the same server to test the ins and outs of json decoding and to my sheer horror it is working. Same code, same server. How does it decide when to work and when to fail?
Any help would be greatly appreciated.
This is the array that I encode before passing it via form
Array
(
[0] => Array
(
[cont_twit] => BillionDollarID
[cont_name] => BillionDollar-ID
[cont_foll] => 382903
[cont_frnd] => 296230
[cont_ftf] => 1.29
[cont_inact] => Last tweeted today
)
[1] => Array
(
[cont_twit] => CrimeConspiracy
[cont_name] => MI6 Crown Carroll
[cont_foll] => 452638
[cont_frnd] => 441748
[cont_ftf] => 1.02
[cont_inact] => Last tweeted today
)
[2] => Array
(
[cont_twit] => 107KingTN
[cont_name] => TN
[cont_foll] => 6505
[cont_frnd] => 6360
[cont_ftf] => 1.02
[cont_inact] => Last tweeted today
)
)
This is the form code
<form action="" id="project" method="POST">
<input type="hidden" value="<?php echo htmlspecialchars(json_encode($dataarray)); ?>" name="bigarray">
<input name="projectsub" type="submit" value="Create Project"></p>
</form>
This is the bit that captures the posted data
<?php
if(isset($_POST['projectsub'])){
$datajson = ($_POST['bigarray']);
echo "<br>datajson ".$datajson; //output is messed up
$datajson2 = str_replace('"','"',$datajson);
echo "<br> datajson2 ".$datajson2; //output is ok
$dataarray = json_decode($datajson2);
echo"<br> data: ";
print_r($dataarray); //output is blank
}
?>
And this is the test code that is working ok
<?
$json_object = "[{"cont_twit":"BillionDollarID","cont_name":"BillionDollar-ID","cont_foll":382902,"cont_frnd":296230,"cont_ftf":1.29,"cont_inact":"Last tweeted today"},{"cont_twit":"CrimeConspiracy","cont_name":"MI6 Crown Carroll","cont_foll":452637,"cont_frnd":441749,"cont_ftf":1.02,"cont_inact":"Last tweeted today"},{"cont_twit":"107KingTN","cont_name":"TN","cont_foll":6505,"cont_frnd":6360,"cont_ftf":1.02,"cont_inact":"Last tweeted today"}]";
$json_object2 = str_replace('"','"',$json_object);
$json_object3 = '[{"cont_twit":"BillionDollarID","cont_name":"BillionDollar-ID","cont_foll":382903,"cont_frnd":296230,"cont_ftf":1.29,"cont_inact":"Last tweeted today"},{"cont_twit":"CrimeConspiracy","cont_name":"MI6 Crown Carroll","cont_foll":452638,"cont_frnd":441748,"cont_ftf":1.02,"cont_inact":"Last tweeted today"},{"cont_twit":"107KingTN","cont_name":"TN","cont_foll":6505,"cont_frnd":6360,"cont_ftf":1.02,"cont_inact":"Last tweeted today"}]';
$json_object4 = "[{\"cont_twit\":\"BillionDollarID\",\"cont_name\":\"BillionDollar-ID\",\"cont_foll\":382902,\"cont_frnd\":296230,\"cont_ftf\":1.29,\"cont_inact\":\"Last tweeted today\"},{\"cont_twit\":\"CrimeConspiracy\",\"cont_name\":\"MI6 Crown Carroll\",\"cont_foll\":452637,\"cont_frnd\":441749,\"cont_ftf\":1.02,\"cont_inact\":\"Last tweeted today\"},{\"cont_twit\":\"107KingTN\",\"cont_name\":\"TN\",\"cont_foll\":6505,\"cont_frnd\":6360,\"cont_ftf\":1.02,\"cont_inact\":\"Last tweeted today\"}]";
$decoded_object = json_decode($json_object);
print_r($decoded_object); //this fails for obvious reasons
echo "<br/>";
$decoded_object2 = json_decode($json_object2);
print_r($decoded_object2); //this works perfectly
echo "<br/>";
$decoded_object3 = json_decode($json_object3);
print_r($decoded_object3); //this works perfectly
echo "<br/>";
$decoded_object4 = json_decode($json_object4);
print_r($decoded_object4); //this works perfectly
?>
Based on the comments I received on this post, I implemented some error checking
ok, thanks, so the last error returns Code 4 which is probably caused by a bad character.
jsonlint fails as an undefined function. last error message comes back blank.
Ok, if this is to do with a bad character in my json, 2 questions - why does it not fail on my test script? And how do I solve the bad character error because I can't predict what bad character will there be next time it grabs data. Because it's all dynamic.