Merbin Joe
Merbin Joe

Reputation: 688

How can I pass array value from javascript ajax to php and read it?

I am new to php and, I am trying to save more then one values the value will increase dynamically. I am send the array value by the following method from javascript code.

    $(".dyimei").each(function(i,value){
        if($(this).val()=="")
            err_flag=1;     
        json_IMEI.push($(this).val());
    });

    //json_IMEI=["343","3453"]  <= eg im getting this value


    var strarray=JSON.stringify(json_IMEI);
   //strarray = "["343","3453"]"; <= here this value

 ajaxRequest.open("POST","save_settings.php?strarray="+strarray, true);
 ajaxRequest.send(null);

And I am accessing this value from serverside code (save_settings.php)

$strarray=(isset($_REQUEST['strarray']) ? $_REQUEST['strarray'] : '');
$strarray=json_encode($strarray);

$arrlength = count($strarray);
    for($x = 0; $x <= $arrlength; $x++)
    {
        $IMEI=$strarray[$x];
        $insert_imei="insert into imeidetails(ProductID,IMEIID) values('$tabid','$IMEI');";
        $run_query=mysqli_query($dbcon,$insert_imei);
    }

But I am getting the $IMEI values is always 0 (zero)

Upvotes: 0

Views: 41

Answers (1)

Radu Gheorghies
Radu Gheorghies

Reputation: 176

change this:

$strarray=json_encode($strarray);

to:

$strarray=json_decode($strarray);

Upvotes: 1

Related Questions