2017-07-21 8 views
0

アクティブなCRUDを実行できるライブインタラクティブテーブルを作成しようとしていますが、何らかの理由で追加機能に挿入しようとしている値が正しく$ _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; 
?> 

ここでライブデータ編集ファイル:私が手にエラーがある

<?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'; 
} 
?> 

<!-- 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> 

ここでは、挿入ファイルです:

お知らせ:未定義のインデックス:ライン上の /var/www/html/UOM/UViewTABLEinsert.phpでuserfirstname 4

お知らせ:未定義のインデックス:上 /var/www/html/UOM/UViewTABLEinsert.phpでuserlastname 4行目

私の編集ファイルでは、これらのインデックスを適切に定義していないことを意味していますが、間違いはありません。私はこの問題を3日間研究しました。誰かが助けてくれるほど親切であれば、私が間違っていることは明らかにわかりません。

+0

ajaxオプションで 'dataType'を削除してみるとうまくいく可能性があります。 – Girish

答えて

0

明示的にデータ型をテキストに設定しているため、キー値のペアでデータが送信されておらず、挿入後JSONで応答を送信するため、ajaxオプションからdataTypeを削除するだけで動作する可能性があります。 $.ajaxdataTypeオプションの詳細については、thisを参照してください。 dataTypを明示的に設定する場合は、ajaxリクエストで設定したのと同じタイプの応答を保持します。

+0

残念ながら私はそれを試みましたが、うまくいきませんでした。 –

+0

次のリンクを参考にしてください。https://stackoverflow.com/questions/6974684/how-to-send-formdata-objects-with-ajax-requests-in-jquery – Girish

関連する問題