2017-07-29 10 views
-1

user_questionと言語入力フィールドについてajax経由で情報を送信しようとしましたが、この要素をajax javascriptに正しく書き込んでデータベースにテーブル要素の値を保存する方法を説明しました。ajax、PHPデータを正しく保存する方法

ありがとうございました。

スクリプト要素。

<table id="myTable" class="table table-sm table-hover order-list"> 
     <thead> 
      <tr> 
       <td>User Question</td> 
       <td>Language</td> 
      </tr> 
     </thead> 
     <tbody> 
      <tr> 
       <td class="col-md-9">' . HTML::inputField('user_question', null, 'placeholder="Write a short answer"'). '</td> 
       <td class="col-md-2">' . HTML::inputField('language', null, 'placeholder="Language"') . '</td> 
       <td class="col-sm-1"><a id="delete_row" class="deleteRow"></a></td> 
      </tr> 
     </tbody> 
     <tfoot> 
      <tr> 
      <td colspan="5"> 
       <input type="button" class="btn btn-lg btn-block " id="addrow" value="Add Row" /> 
      </td> 
      </tr> 
      <tr></tr> 
     </tfoot> 
    </table> 

<script> 
$(document).ready(function() { 
    var counter = 0; 

    $("#addrow").on("click", function() { 
     var newRow = $("<tr>"); 
     var cols = ""; 

     cols += '<td><input type="text" class="form-control" name="user_question' + counter + '"/></td>'; 
     cols += '<td><input type="text" class="form-control" name="language' + counter + '"/></td>'; 

     cols += '<td><input type="button" class="ibtnDel btn btn-md btn-danger " value="Delete"></td>'; 
     newRow.append(cols); 
     $("table.order-list").append(newRow); 
     counter++; 

    // element pb    
    // var data = $("#new_product").serialize(); // form id or table id ? 

    var dataString = 'user_question='+user_question+'language='+language; 

    $.ajax({ 
     type: 'POST', 
     url: '{$ajax_url}', 
     data : dataString, 
     success: function(data) { 
      alert(data); 
      $("p").text(data); 

     } 
    }); 

    $("table.order-list").on("click", ".ibtnDel", function (event) { 
     $(this).closest("tr").remove();  
     counter -= 1 
    }); 

}); 

</script> 
+0

2つの以上のparamを追加する&や記号を使用します同じ名前で、PHPがすべての入力の配列を取得できるように、それらを 'name =" user_question [] "'と 'name =" language [] "'という名前にする必要があります。 – Barmar

+0

私は 'cols + ='を試みます​​ '; 'と' var dataString = "user_question [] =" + user_question; "結果は次のようになります。 '["user_question"] => array(1){ [0] => 文字列(25) "[object HTMLInputElement]" } – maegeri

答えて

0

の.phpページではJavaScript内でPHPの変数の値を取得するには、このような使用

url: "<?php echo $ajax_url; ?>", 

また、あなたが複数の入力を持っている場合は、あなたのdataString

関連する問題