Php 在Ajax中使用$\u POST从骨架表检索索引时出错。未定义索引

Php 在Ajax中使用$\u POST从骨架表检索索引时出错。未定义索引,php,jquery,html,ajax,Php,Jquery,Html,Ajax,因此,我尝试创建一个可以执行活动CRUD的实时交互表,但由于某些原因,我尝试为添加功能插入的值没有正确读取到$\u POST 这是表格: <?php include "../db.php"; $connection = DB::CreateConnection(); $output = ''; $sql = "SELECT * FROM users ORDER BY userid ASC"; $result = mysqli_query($connection, $sql); $outpu

因此,我尝试创建一个可以执行活动CRUD的实时交互表,但由于某些原因,我尝试为添加功能插入的值没有正确读取到$\u POST

这是表格:

<?php
include "../db.php";
$connection = DB::CreateConnection();
$output = '';
$sql = "SELECT * FROM users ORDER BY userid ASC";
$result = mysqli_query($connection, $sql);
$output .= '
     <div class="table-responsive">
          <table class="table table-bordered">
               <tr>
                    <th width="10%">Id</th>
                    <th width="40%">First Name</th>
                    <th width="40%">Last Name</th>
                    <th width="10%">Delete</th>
               </tr>';
if(mysqli_num_rows($result) > 0)
{
     while($row = mysqli_fetch_array($result))
     {
          $output .= '
               <tr>
                    <td>'.

$row["userid"].'</td>
                    <td class="userfirstname" data-id1="'.$row["userid"].'" contenteditable>'.$row["userfirstname"].'</td>
                    <td class="userlastname" data-id2="'.$row["userid"].'" contenteditable>'.$row["userlastname"].'</td>
                    <td><button type="button" name="delete_btn" data-id3="'.$row["userid"].'" class="btn btn-xs btn-danger btn_delete">x</button></td>
               </tr>';
     }
     $output .= '
          <tr>
               <td></td>
               <td id="userfirstname" contenteditable></td>
               <td id="userlastname" contenteditable></td>
               <td><button type="button" name="btn_add" id="btn_add" class="btn btn-xs btn-success">+</button></td>
          </tr>';
}
else
{
     $output .= '<tr>
                         <td colspan="4">Data not Found</td>
                </tr>';
}
$output .= '</table>


     </div>';
echo $output;
?>

此处显示实时数据编辑文件:

<!-- This should be where matt's code goes to edit via popup or live -->
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
  <body>
    <div class="container">
      <br />
      <br />
      <br />
      <div class="table-responsive">
          <h3 align-"center">Live Table</h3>
          <br />
          <div id="live_data"></div>
      </div>
    </div>
  </body>
  <script type="text/javascript">
  function fetch_data()
  {
       $.ajax({
            url:"UViewTABLE.php",
            method:"POST",
            success:function(data){
                 $('#live_data').html(data);
            }
       });
  }
 fetch_data();
 $(document).on('click', '#btn_add', function(){
          var userfirstname = $('#userfirstname').text();
          var userlastname = $('#userlastname').text();
          if(userfirstname == '')
          {
               alert("Enter First Name");
               return false;
          }
          if(userlastname == '')
          {
               alert("Enter Last Name");
               return false;
          }
          $.ajax({
               url:"UViewTABLEinsert.php",
               method:"POST",
               data:{userfirstname:userfirstname, userlastname:userlastname},
               dataType:"text",
               success:function(data)
               {
                    alert(data);
                    fetch_data();
               }
          })
     });
  </script>




活动台
函数fetch_data() { $.ajax({ url:“UViewTABLE.php”, 方法:“张贴”, 成功:功能(数据){ $('live#u data').html(数据); } }); } 获取_数据(); $(文档)。在('click','btn_add',函数()上{ var userfirstname=$('#userfirstname').text(); var userlastname=$('#userlastname').text(); 如果(userfirstname==“”) { 警报(“输入名字”); 返回false; } 如果(userlastname==“”) { 警告(“输入姓氏”); 返回false; } $.ajax({ url:“UViewTABLEinsert.php”, 方法:“张贴”, 数据:{userfirstname:userfirstname,userlastname:userlastname}, 数据类型:“文本”, 成功:功能(数据) { 警报(数据); 获取_数据(); } }) });
以下是插入文件:

<?php
include "../db.php";
$connection = DB::CreateConnection();
$sql = "INSERT INTO users (userfirstname, userlastname) VALUES('".$_POST["userfirstname"]."', '".$_POST["userlastname"]."')";
 if(mysqli_query($connection, $sql))
 {
      echo 'Data Inserted';
 }
 ?>

我得到的错误是:

注意:中未定义的索引:userfirstname /第4行的var/www/html/UOM/UViewTABLEinsert.php

注意:中未定义的索引:userlastname /第4行的var/www/html/UOM/UViewTABLEinsert.php


我知道这意味着在我的编辑文件中,我没有正确定义这些索引,但我看不出有什么问题。我已经研究了3天这个问题,如果有人愿意帮忙,请帮忙,显然我不知道我做错了什么。

只需从ajax选项中删除
数据类型
,因为您显式地将数据类型设置为文本,数据不以键值对提交,插入后,以JSON发送响应,可能有用。有关
$.ajax
数据类型
选项的更多信息,请参阅。如果要显式设置
dataTyp
,请将响应保持为ajax请求中设置的相同类型。

尝试删除ajax选项中的
dataType
,可能会奏效。请参阅以下链接,可能会有所帮助: