Page 1 of 1

json str through ajax

Posted: Wed Mar 10, 2010 8:07 pm
by BrettCarr
I have created a json string i want to send through AJAX.
This is the function that makes the string if you uncomment the last three line you can see this work

Code: Select all

<?php 
public function myJsonString()
            {
                $test = json_encode($_SESSION['batcharray']);
                $str = "(". $test .")" ;    
                var_dump($str);
                eval("\$str = \"$str\";");
                var_dump($str);
                $str = substr($str, 1, strlen($str) - 2); // remove outer ( and ) 
                $str = preg_replace("/([a-zA-Z0-9_]+?):/" , "\"$1\":", $str); // fix variable names 
                echo "<br/>";
                echo "Ok string is created" ."<br/>";
                $_SESSION['myJsonString'] = $str;
                //var_dump($str);
                //$output = json_decode($str, true);
                //var_dump($output);
            }
?>
This is what the string looks like

[{"batch_date":"1970-01-01","batch_number":"52727","original_proof_num":"596","batch_status":"New","invoice_num_ccaus":"3","carrier":"Toll","num_of_boxes":"","toll_consign_num":"3","custId":"","venueId":"","bpay":"","proof_tracking_comments":"","veunue_comments":""},{"batch_date":"1970-01-01","batch_number":"52727","original_proof_num":"596","batch_status":"New","invoice_num_ccaus":"3","carrier":"Toll","num_of_boxes":"","toll_consign_num":"3","custId":"","venueId":"","bpay":"","proof_tracking_comments":"","veunue_comments":""}]"
So this is ok to use json_decode on Right?? So now i want to send this string through AJAX and then json_decode on the server side right? this is the ajax send

Code: Select all

<script language="javascript" type="text/javascript">
    
    
    function addDatadb()
                {
                        myarray = <?= $_SESSION['myJsonString'] ?>;
                        send( '/ajax/arrayclean.php', 'myarray=' + myarray, null , null);
                        alert(myarray);
                }
</script>
this is on the server side

Code: Select all

<?PHP
include_once($_SERVER['DOCUMENT_ROOT'].'/includes/global.php');
    $db = new Database();
    $db->connect();
 
        
        
        $str = $_REQUEST['myJsonString'];
        $output = json_decode($str, true);
        var_dump($output); 
        
        
        
        
        
            foreach ($output as $ojb)
                {
                    $comma_separated = implode("','", $ojb);
                    $myquery = "INSERT INTO batch_import VALUES(null,'$comma_separated','0');";
                    echo $myquery . "<br />";
                    $db->query($myquery);
                }
                
?>
 
Im lost, three days ive been attempting to get this to no avail PLEASE SOMEONE HELP ME...

Re: json str through ajax

Posted: Thu Mar 11, 2010 10:07 am
by pickle
You should be able to just use json_encode() without doing all your other concatenation & eval().

And you don't need to put all your text in [ quote ][ /quote ] boxes. You can if you want though, I guess.