簡體   English   中英

如何將數組值從javascript ajax傳遞到php並讀取它?

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

我是php的新手,我正嘗試保存多個值,然后該值將動態增加。 我從JavaScript代碼通過以下方法發送數組值。

    $(".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);

而且我正在從服務器端代碼(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);
    }

但是我得到的$ IMEI值始終為0(零)

改變這個:

$strarray=json_encode($strarray);

至:

$strarray=json_decode($strarray);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM