json str through ajax

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
BrettCarr
Forum Newbie
Posts: 22
Joined: Fri Feb 12, 2010 6:45 pm

json str through ajax

Post 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...
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: json str through ajax

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply