douzhi9939 2015-10-20 16:51
浏览 52
已采纳

如何将数组值从javascript ajax传递给php并读取它?

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)

  • 写回答

1条回答 默认 最新

  • dsakktdog498483070 2015-10-20 16:56
    关注

    change this:

    $strarray=json_encode($strarray);
    

    to:

    $strarray=json_decode($strarray);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥188 寻找能做王者评分提取的
  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图
  • ¥30 乘子法解约束最优化问题的matlab代码文件,最好有matlab代码文件
  • ¥15 写论文,需要数据支撑
  • ¥15 identifier of an instance of 类 was altered from xx to xx错误
  • ¥100 反编译微信小游戏求指导
  • ¥15 docker模式webrtc-streamer 无法播放公网rtsp
  • ¥15 学不会递归,理解不了汉诺塔参数变化
  • ¥15 基于图神经网络的COVID-19药物筛选研究
  • ¥30 软件自定义无线电该怎样使用