我有一个显示库存的页面。当库存出现缺陷(或任何其他状态)时,我想从下拉列表中选择该选项,并相应地更新数据库,而无需刷新页面。我一直在使用 .ajax 和 jQuery,我想我已经很接近了,但我只是无法让它正常工作。
首先,这是填充下拉列表的代码。这段代码没有任何问题(在我网站的其他地方使用过)。尽管我会出于名称和 ID 目的而包含
$myQuery2 = "SELECT InventoryItemStatus.InventoryItemStatusID, InventoryItemStatus.InventoryItemStatusDescription FROM InventoryItemStatus";
$myConnection = mysql_connect($myServer,$myUsername,$myPassword);
if (!$myConnection){
die('Conncetion Failed:' . mysql_error());
}
@mysql_select_db($myDatabase) or die("Unable to select database");
$result2 = mysql_query($myQuery2) or die (mysql_error());
if(mysql_num_rows($result2)){
$select2= '<select style="width:90px" id="StatusList" name="StatusList">';
$select2.='<option value="0.5"> --Status-- </option>';
while($rs=mysql_fetch_array($result2)){
$select2.='<option value="'.$rs['InventoryItemStatusID'].'">'.$rs['InventoryItemStatusDescription'].'</option>';
}
}
$select2.='</select>';
这是我正在使用的 jQuery 代码。 .change 工作正常。 (如果我用警报替换 $.ajax 方法,当我更改下拉列表时,它会警报弹出窗口)
<script type="text/javascript">
$(document).ready(function(){
$('[name="StatusList"]').change(function() {
var mydata = $('[name="StatusList"]').val();//$(this).val();
var inputdata = $('[name="StarmontInventoryItemID"]').val();
$.ajax({
type: 'POST',
url: 'update_status/update_starmont.php',
data: {StatusList:mydata ,StarmontInventoryItemID:inputdata}
});
});
});
</script>
这是我的 php 代码中包含的表单。其中包括一个隐藏字段,其中包含我想要更改其状态的库存项目的唯一 ID,以及下拉菜单。
<form method='post'>
{$select2}
<input type='hidden' value='{$row14['InventoryItemID']}' name='StarmontInventoryItemID' id='StarmontInventoryItemID'/>
</form>
这是在更改时调用的 php 脚本。我认为这是我没有正确使用的部分。我只是不清楚数据如何传递给这个脚本。我无法弄清楚它是否在 $_POST 变量中传递。
include_once('../../../php/common.php');
include_once('../../../php/db.php');
$InventoryItemID = $_POST['StarmontInventoryItemID'];
//$InventoryItemStatusID = $_POST['StatusList'];
mysql_query("UPDATE InventoryItem SET InventoryItemStatusID = 2 WHERE InventoryItemID = '$InventoryItemID'") or die (mysql_error());
如您所见,我只是尝试将库存项目 ID 传递给脚本并将该项目更新为状态“2”。我的最终结果是将其更新为下拉菜单的值,但我确信如果我能让这部分工作,我就能弄清楚其余的部分。
预先感谢您的帮助! 祝你旅途愉快!
编辑
这非常有效,非常感谢。不幸的是我遇到了另一个问题。例如,我有 10 个库存商品。我执行复杂的查询来检索数组中所需的所有信息,然后执行 foreach。顶部的库存项目似乎可以正确更新状态,但其下面的任何项目都不会执行更新的 sql 查询。
if (sizeof($rows14) > 0) {
foreach($rows14 as $row14):
$query17 = "
SELECT
SystemID,
Serial
FROM
Device
WHERE
Device.InventoryItemID = :InventoryItemID
";
$query_params17 = array(
':InventoryItemID' => $row14['InventoryItemID']
);
try {
$stmt17 = $db->prepare($query17);
$stmt17->execute($query_params17);
}
catch(PDOException $ex) {
die("Failed to run query: " . $ex->getMessage());
}
$row17 = $stmt17->fetch();
$myQuery2 = "SELECT InventoryItemStatus.InventoryItemStatusID, InventoryItemStatus.InventoryItemStatusDescription FROM InventoryItemStatus";
$result2 = mysql_query($myQuery2) or die (mysql_error());
if(mysql_num_rows($result2)){
$select2= '<select style="width:90px" name="StatusList">';
while($rs=mysql_fetch_array($result2)){
if($rs['InventoryItemStatusID'] == $row14['Status']){
$select2.='<option value="'.$rs['InventoryItemStatusID'].'" selected="'.$row14['Status'].'">'.$rs['InventoryItemStatusDescription'].'</option>';
}
else{
$select2.='<option value="'.$rs['InventoryItemStatusID'].'">'.$rs['InventoryItemStatusDescription'].'</option>';
}
}
}
$select2.='</select>';
echo"
<tr class='tableRowClass2'>
<td height='20'>{$row14['OnHand']}</td>
<td height='20'>{$row14['ItemName']} - {$row17['SystemID']} - {$row17['Serial']}</td>
<td height='20'>
<form method='post'>
{$select2}
<input type='hidden' value='{$row14['InventoryItemID']}' name='StarmontInventoryItemID'/>
</form>
</td>
</tr>";
endforeach;
}
我尝试了多种不同的方法来使其适用于所有库存项目。我不确定将其放入 foreach 循环中是否有效。再次非常感谢任何帮助!
最佳答案
你必须编辑你的JS
$(document).ready(function(){
$("#StatusList").change(function() {
var mydata = $(this).val();
var inputdata = $("#StarmontInventoryItemID").val();
$.ajax({
type: 'POST',
url: 'update_status/update_starmont.php',
data: {StatusList:mydata ,StarmontInventoryItemID:inputdata }
});
});
});
在 jquery ajax cal 数据中,必须是一个映射 {myvar:myvalue},其中 myvar 是您的 php 代码 $_POST['myvar'] 中的内容
success: success
success 是如果你有一个成功的 ajax 调用就会执行的函数,你也可以像这样使用它
success: function(data) { console.log(data) ... }
关于php - jQuery ajax post 到 php 脚本并更新 mysql 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20381066/