使用php mysql jquery ajax更新选择框的值
我通过jquery-ajax将标签中的
status
更改为数据库时遇到了重大问题。
注意:我在stackoverflow中搜索了这个问题。 但那些回答并不接近我的问题。 以下是link1 , link2 , link3下面的链接
当我点击第一行选择框时,数据通过ajax发送status column in table
的status column in table
更新,并在mysql数据库中更新。 但是当选择第2行,第3行时,它不会更改,不会更新html页面中的status column
,也不会更新数据库中的输出图像。
先感谢您..
以下是我的代码数据库表名称的详细信息:ajaxselect
HTML文件:index.php
Jquery Ajax select with PHP Mysql Id Product name Status Action display();?>
$(document).ready(function(){ $("#selectstatus").change(function(){ var statusname = $("#selectstatus").val(); var getid = $("#getid").val(); $.ajax({ type:'POST', url:'ajaxreceiver.php', data:{statusname:statusname,getid :getid}, success:function(result){ $("#display").html(result); } }); }); });
Ajax文件:ajaxreceiver.php
getdata($statusid,$id); } ?>
PHP类文件:processing.php
link= new mysqli('localhost','root','','example'); if(mysqli_connect_errno()){ die ("connection failed".mysqli_connect_errno()); } } function display(){ $sql = $this->link->stmt_init(); $id=1; if($sql->prepare("SELECT id,productname,status FROM ajaxselect")){ $sql->bind_result($id,$productname,$status); if($sql->execute()){ while($sql->fetch()){ ?> <input type="hidden" id="getid" value=""> Pending Delivered Cancelled Amount Paid link->stmt_init(); if($sql->prepare("UPDATE ajaxselect SET status=? WHERE id=?")){ $sql->bind_param('si',$statusid,$id); if($sql->execute()){ echo $statusid; } else { echo "Update Failed"; } } } } ?>
所有的选择框都具有相同的id,因此$("#selectstatus").val()
将始终返回相同的值。 你应该获得改变元素的价值。 Javascript: this.value
或jQuery $(this).val()
示例(注意selectstatus
是一个类,所以你应该添加一个新类):HTML
JS
$(".selectstatus").change(function(){ var statusname = $(this).val(); var getid = $(this).attr("status-id"); $.ajax({ type:'POST', url:'ajaxreceiver.php', data:{statusname:statusname,getid :getid}, success:function(result){ $("#display").html(result); } }); });
在这种情况下你不能使用id,否则它只适用于第一次出现
`
`