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)

图片转代码服务由CSDN问答提供 功能建议

我是php的新手,我试图保存多个值,这个值会动态增加。 我通过以下方法从javascript代码发送数组值。

  $(“。dyimei”)。each(function(i,value){
 if($  (this).val()==“”)
 err_flag = 1; 
 json_IMEI.push($(this).val()); 
}); 
 
 // json_IMEI = [“343  “,”3453“]&lt; =例如我得到这个值
 
 
 var strarray = JSON.stringify(json_IMEI); 
 // strarray =”[“343”,“3453”]“;  &lt; =这里这个值
 
 ajaxRequest.open(“POST”,“save_settings.php?strarray =”+ strarray,true); 
 ajaxRequest.send(null); 
   
 
 

我从服务器端代码(save_settings.php)访问此值

  $ strarray =(isset($ _ REQUEST ['strarray'])  ?$ _REQUEST ['strarray']:''); 
 $ strarray = json_encode($ strarray); 
 
 $ arrlength = count($ strarray); 
 for($ x = 0; $ x&lt;  = $ arrlength; $ x ++)
 {
 $ IMEI = $ strarray [$ x]; 
 $ insert_imei =“插入imeidetails(ProductID,IMEIID)值('$ tabid','$ IMEI');”  ; 
 $ run_query = mysqli_query($ dbcon,$ insert_imei); 
} 
   
 
 

但我得到的$ IMEI值始终为0(零)< / p>

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dsakktdog498483070 2015-10-20 16:56
    已采纳

    change this:

    $strarray=json_encode($strarray);
    

    to:

    $strarray=json_decode($strarray);
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题