用php mysql jquery ajax更新选择框的值 [英] update the value of select box with php mysql jquery ajax
问题描述
通过jquery-ajax将<select>
标记中的status
更改为数据库时,我遇到了主要问题.
I'm facing major issue in changing status
in <select>
tag to database through jquery-ajax.
注意:我已经在stackoverflow中搜索了此问题.但是那些回答与我的问题不太接近.这是下面的那些链接 link1 , link3
Note : I have searched in stackoverflow for this question. but those answere not come close to my question. Here are those links below link1, link2, link3
当我单击第一行选择框时,数据通过ajax发送,status column in table
得到更新,并且也在mysql数据库中更新.
但是当选择第二,第三排它不会改变不更新
When i click on first row select box the data is sent through ajax and the status column in table
get updated and also it is updated in mysql database.
But when is select 2nd, 3rd row it doesn't change not update the status column
in html page nor in database here is the output image.
先谢谢您.
这是我的代码的详细信息 数据库表名称:ajaxselect
Here is the details of my code database table name: ajaxselect
HTML文件:index.php
HTML file: index.php
<?php
include('processing.php');
$newobj = new processing();
?>
<html>
<head>
<title>Jquery Ajax select <tag> with PHP Mysql</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
</head>
<body>
<table border="1">
<tr>
<th>Id</th>
<th>Product name</th>
<th>Status</th>
<th>Action</th>
</tr>
<?php echo $newobj->display();?>
</table>
<script>
$(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);
}
});
});
});
</script>
</body>
</html>
Ajax文件:ajaxreceiver.php
Ajax file : ajaxreceiver.php
<?php
include('processing.php');
$newobj = new processing();
if(isset($_POST['statusname'],$_POST['getid'])){
$statusid = $_POST['statusname'];
$id = $_POST['getid'];
$newobj->getdata($statusid,$id);
}
?>
PHP类文件:processing.php
PHP class file : processing.php
<?php
class processing{
private $link;
function __construct(){
$this->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()){
?>
<tr>
<td><input type="hidden" id="getid" value="<?php echo $id;?>"><?php echo $id;?></td>
<td><?php echo $productname;?></td>
<td><p id="display"><?php echo $status;?></p></td>
<td>
<select id="selectstatus">
<option>Pending</option>
<option>Delivered</option>
<option>Cancelled</option>
<option>Amount Paid</option>
</select>
</td>
</tr>
<?php
}
}
}
}
function getdata($statusid,$id){
$sql = $this->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
All of your select-boxes have the same id, so $("#selectstatus").val()
will return always the same value. You should get value of changed element. Javascript: this.value
or jQuery $(this).val()
Example (notice that selectstatus
is a class, so you should add a new class):
HTML
<tr>
<td><?php echo $productname;?></td>
<td><p id="display"><?php echo $status;?></p></td>
<td>
<select status-id="<?php echo $id;?>" class="selectstatus" id="selectstatus">
<option value="Pending">Pending</option>
<option value="Delivered">Delivered</option>
<option value="Cancelled">Cancelled</option>
<option value="Amount paid">Amount Paid</option>
</select>
</td>
</tr>
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);
}
});
});
这篇关于用php mysql jquery ajax更新选择框的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!