按钮将多个数据发送到数据库(php,ajax)

时间:2018-02-01 20:59:46

标签: php jquery ajax

我使用foreach将数据从数据库发送到屏幕。当我点击按钮时,我看不到任何结果。我在哪里犯错误请求帮助

 <table class="table">
   <thead class="thead-dark" align="center"  >

   <th >id</th>
   <th >EğitimAdı</th>
   <th >Adres</th>
   <th >Onay </th>
   </thead>
   <tbody>
    <form  id="form1" action="" method="post"> 
  <?php $i=1; foreach($dbb as $ac){ ; ?>
       <tr align="center" >  
           <td > <?php echo $ac[0]; ?> </td>
           <td > <?php echo $ac[1]; ?></td>
           <td   align="center"> <iframe width="360" height="115" src="<?php echo $ac[2]; ?>" frameborder="0" gesture="media" allow="encrypted-media" allowfullscreen  ></iframe></td>
            <td >
             <button type="button" value="<?php echo $ac[0]; ?>" id="gonder"  onclick="gonder('this.val()')">İzledim</button>  
            </td>
       </tr>
  <?php $i++; } ?>
  </form> <p></p>
 </tbody>

   </table>

gonder.js发送到数据库

   function gonder(deger){
        $.ajax({
        type:"post",
        url:"isleme.php",
        data:deger,
        success:function(cevap) {
        $("p").text(cevap);
        }
        })

}

isleme.php查看屏幕

<?php 
   $b=$_POST['deger'];
   echo $b;
    ?>

2 个答案:

答案 0 :(得分:0)

首先你缺少括号:

onclick="gonder" has to be onclick="gonder()"

其次变量“deger”为空,因为#id不存在,请尝试更改为:

var deger = $("#gonder").val();

答案 1 :(得分:0)

我注意到你正在迭代并为所有元素提供相同的id。理想情况下,每个文档中的id都应该是唯一的。

您可以执行以下任一操作:

  1. 为每个迭代元素准备并具有唯一ID。
  2. 尝试删除id元素,因为您不依赖于使用id。
  3. 请告诉我,如果有帮助吗?