dongpo1846 2018-04-29 14:13
浏览 140
已采纳

来自Ajax jQuery数组的未定义的POST索引

Im trying to insert the info from the array into my database. The problem im having is first of all that i am getting undefined index of $_POST['Person'], $_POST['Color_name'], $_POST['Opinion'] from the php.

The second issue is that i would like to use dataType: "json"; in my Ajax jQuery but my data is not reaching the php file if i use it.

What i have tried so far is to json stringify my return data in the get_data function without any success. I also tried decoding the data in the php but since it's getting undefined index i am guessing there has to be some work done in the jQuery.

This is the array the get_data() function returns

{Person: "James", Color_name: "blue", Opinion: "Looks good"}
{Person: "James", Color_name: "green", Opinion: "Looks ok"}
{Person: "Rebecka", Color_name: "blue", Opinion: "Looks bad"}
{Person: "Rebecka", Color_name: "black", Opinion: "Looks very bad"}

HTML

<div>
  <ul data-person="James">
    <li data-color_opinion="blue">Looks good</li>
    <li data-color_opinion="green">Looks ok</li>
  </ul>
  <ul data-person="Rebecka">
    <li data-color_opinion="blue">Looks bad</li>
    <li data-color_opinion="black">Looks very bad</li>
  </ul>
</div>

JQuery

function get_data() {
  var data = [];
  $.each($('ul'), function(i, el) {
    $.each($(el).find("[data-color_opinion]"), function(j, child) {
      let person = $(el).data('person');
      let color_name = $(child).data('color_opinion');
      let opinion = $(child).text();
      data.push({ Person: person, ColorName: color_name, Opinion: opinion });
    });
  });
  return data;
};
console.log(get_data())

$.ajax({
type: "POST",
url:'colors.php',
data:get_data´(),
success: function(data) {
  console.log(data);
}
});
});

PHP

<?php

$servername = "servername";
$username = "username";
$password = "password";

$person = $_POST['Person'];
$color_name = $_POST['Color_name'];
$opinion = $_POST['Opinion'];


try {
$db = new PDO("mysql:host=$servername;dbname=DB_NAME", $username, $password);
 $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
 $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);


 $sql = "INSERT INTO colors (person, color_name, opinion)
     VALUES ($person, $color_name, $opinion)";

$db->exec($sql);
echo "New records created successfully";
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
$db = null;

 ?>
  • 写回答

2条回答 默认 最新

  • doufendi9063 2018-04-29 14:24
    关注

    There is no $_POST['Person']; because all the javascript objects are in an outer array and get_data() returns the whole array

    Try something like:

    $.ajax({
      type: "POST",
      url: 'colors.php',
      data: {items : get_data()},
            // ^^ items contains an array
      success: function(data) {
        console.log(data);
      }
    });
    

    Then in php

    $items = $_POST['items'];// is array of sub arrays
    
    foreach($items as $item){
       $person = $item['Person'];
        // get the other values and do a query within each iteration of this loop
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何修改pca中的feature函数
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法