Page 1 of 1

Got my array to ajax, but how do i get it backto php array ?

Posted: Fri Mar 05, 2010 5:05 am
by BrettCarr
Ok here it is, Im parsing my array to ajax and as i understand it you need to use json_encode to do so. So this is the send to ajax

Code: Select all

<script language="javascript" type="text/javascript">
    
    
    function addDatadb()
                {
                    myarray = <?= json_encode($_SESSION['batcharray'] ) ?>;
                    send( '/ajax/arrayclean.php', 'myarray=' + myarray, null , addDatadbRecv);                  
                }
</script>
 
Now in the sorce code of the page i can see its done its thing heres what i see in the sorce code of the page
function addDatadb()
{
myarray = [{"batch_date":"2010-03-05","batch_number":"8527412","original_proof_num":"645","batch_status":"New","invoice_num_ccaus":"","carrier":"Toll","num_of_boxes":"2","toll_consign_num":"7857527578","custId":"32","venueId":"202","bpay":"7856-75275-25","proof_tracking_comments":"Hope it works","veunue_comments":"Nope im afraid not"},{"batch_date":"2010-03-05","batch_number":"8527412","original_proof_num":"596","batch_status":"New","invoice_num_ccaus":"1","carrier":"Toll","num_of_boxes":"2","toll_consign_num":"7857527579","custId":"19","venueId":"13","bpay":"7856-75275-25","proof_tracking_comments":"Hope it works","veunue_comments":"Nope im afraid not"}];
send( '/ajax/arrayclean.php', 'myarray=' + myarray, null , addDatadbRecv);
}
so it's sending right? So to the PHP
I"m trying to turn it back into a PHP array the implode it into string to put in my query so hereis my attempt witch is not working at all

Code: Select all

 
<?PHP
 
    include_once($_SERVER['DOCUMENT_ROOT'].'/includes/global.php');
    $db = new Database();
    $db->connect();
    
    
 
 
 
    
    if (isset($_REQUEST['myarray']))//checks to see wheather anything is there
        {
        echo "It's Set<br/> ";
            
        
        var_dump($_REQUEST['myarray'])); //just to test 
        foreach ($_REQUEST['myarray'] as $obj)  
            {
               $testme = json_decode($obj);
                $comma_separated = implode("','", $testme); 
            $comma_separated = implode("','", $obj);
                $myquery = "INSERT INTO batch_import VALUES(null,'$comma_separated','0');";
                echo $myquery . "<br />";
                $db->query($myquery);       
                    
            }
        }
        else 
        {
        echo "It's not Set";
        exit;
        }
        
        // I did this to see weather json_decode was doing any thing the is part of the json_encoded array $_SESSION['batcharray'] witch on this side should be $_REQUEST['myarray'], so i just copy and pasted it in to see what happened.
        var_dump(json_decode('{"batch_date":"2010-03-05","batch_number":"852852852","original_proof_num":"645","batch_status":"New","invoice_num_ccaus":"","carrier":"Toll","num_of_boxes":"2","toll_consign_num":"7575275252","custId":"32","venueId":"202","bpay":"852752","proof_tracking_comments":"dghcdghmcghn","veunue_comments":"cghnchcgh"}')  );
        
?>
 
This is what the debug response tab in Mozilla says
It's Set<br/> NULL
object(stdClass)#2 (13) {
["batch_date"]=>
string(10) "2010-03-05"
["batch_number"]=>
string(9) "852852852"
["original_proof_num"]=>
string(3) "645"
["batch_status"]=>
string(3) "New"
["invoice_num_ccaus"]=>
string(0) ""
["carrier"]=>
string(4) "Toll"
["num_of_boxes"]=>
string(1) "2"
["toll_consign_num"]=>
string(10) "7575275252"
["custId"]=>
string(2) "32"
["venueId"]=>
string(3) "202"
["bpay"]=>
string(6) "852752"
["proof_tracking_comments"]=>
string(12) "dghcdghmcghn"
["veunue_comments"]=>
string(9) "cghnchcgh"
}
This is What the post tab in mozilla debug say's
myarray [object Object],[object Object]
So where am i going wrong or can some one tell what im surposed to do next?? lol Its friday and ive had a beer I hope im being clear

Re: Got my array to ajax, but how do i get it backto php array ?

Posted: Fri Mar 05, 2010 5:43 am
by VladSun
json_decode() does NOT change the argument passed to it - it RETURNS an JSON decoded object instead.

Re: Got my array to ajax, but how do i get it backto php array ?

Posted: Fri Mar 05, 2010 6:59 am
by VladSun
You neeed to perform a JSON decode $_REQUEST['myarray'] before you can use it ...