简体   繁体   English

如何将数组中超过 1 个元素的值从 ajax 传递给 PHP?

[英]How to pass the value of more then one 1 elements in an array from ajax to PHP?

Actually I am getting the value of buttons with same id through a loop and an array and i want to pass these values from ajax to php but the problem comes it showing only value of last element?实际上,我通过循环和数组获取具有相同 id 的按钮的值,我想将这些值从 ajax 传递给 php,但问题是它只显示最后一个元素的值?

function remove() {
            var elms = document.querySelectorAll("[id='botn']");

            for (var i = 0; i < elms.length; i++)
            var datastring=elms[i].value;
            console.log(datastring);

            $.ajax({
               url: 'php/del_beacon.php',
                type: 'post',
                data: {data:datastring},
                success: function(response) {

                }

        });
        }

When i use alert(elms[i].vlaue) then it shows the value of all selected buttons This my php file当我使用 alert(elms[i].vlaue) 然后它会显示所有选定按钮的值 这是我的 php 文件

<?php
$servername="localhost";
    $username="root";
    $password="";
    $dbname="beelist";

$conn=mysqli_connect($servername,$username,$password,$dbname);
error_reporting(0);
$connDB= mysqli_select_db($conn,'beelist');
$myArray = $_POST['data'];
echo $myArray;



?>

You're only storing one value in the datastring variable because it's a string variable. 您只在datastring变量中存储一个值,因为它是一个字符串变量。

for (var i = 0; i < elms.length; i++)
    var datastring = elms[i].value;

For each element of this loop, you rewrite the variable, that's why only the last value is saved. 对于此循环的每个元素,都重写变量,这就是为什么仅保存最后一个值的原因。

Change it to an array, and feed values to it via push() . 将其更改为数组,然后通过push()向其提供值。 Then you should be getting the whole array to play with in PHP: 然后,您应该在PHP中使用整个数组:

var datastring = [];
for (var i = 0; i < elms.length; i++)
    datastring.push(elms[i].value);

You are getting only the last element because you are reassigning the value every time the loop is executing. 您仅获得最后一个元素,因为每次循环执行时都要重新分配值。

you need to use the push method. 您需要使用push方法。

var data.push(elms[i].value);

Since you are getting all the values of a selected data via ajax. 由于您正在通过ajax获取所选数据的所有值。 The php is where you are having issue. PHP是您遇到问题的地方。 In your php code, you did not specify what you want to do with the posted data like Insert, update or delete actions. 在您的php代码中,您未指定要对发布的数据执行的操作,例如插入,更新或删除操作。

Anyway at php, you will need to loop through the posted data as per code below 无论如何在php中,您都需要按照以下代码遍历发布的数据

foreach($myArray as $pid){

echo $pid;
}

The code below assumes that you want to loop through the posted data to delete records from posts table based on posts_id 下面的代码假定您要遍历发布的数据以基于posts_id从posts表中删除记录

<?php
$servername="localhost";
    $username="root";
    $password="";
    $dbname="beelist";

$conn=mysqli_connect($servername,$username,$password,$dbname);
error_reporting(0);
$connDB= mysqli_select_db($conn,'beelist');
$myArray = $_POST['data'];


//loop through your post and then insert or delete or update records
foreach($myArray as $pid){

// Delete record
    $query = "DELETE FROM posts WHERE id=".$pid;
    mysqli_query($con,$query);


echo $pid;
}


?>

Let me know if you still face any issue.. 让我知道您是否仍然遇到任何问题。

In your code what you are trying to do is在您的代码中,您要做的是

var datastring = elms[i].value; // Your problem is here

Here you are declaring datastring everyting and because of this you are getting last value in data.在这里,您正在声明数据字符串,因此您将获得数据中的最后一个值。 instead you can push values in array and then convert array to JSON and then send it over ajax call to php like this.相反,您可以将值推送到数组中,然后将数组转换为 JSON,然后像这样通过 ajax 调用将其发送到 php。

function remove(){
 var elms = document.querySelectorAll("[id='botn']");
 var datastring = [];
 for (var i = 0; i < elms.length; i++)
 {
   datastring.push(elms[i].value);
   console.log(datastring);
 }
var myData = JSON.stringify(datastring);

$.ajax({
   url: 'php/del_beacon.php',
   type: 'post',
   data: {data:myData},
   success: function(response) {
    }
  });
}

then in your php code you can again convert json to array by json_decode() function in php json_decode然后在您的 php 代码中,您可以再次通过 php json_decode 中的json_decode() 函数将 json 转换为数组

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM