繁体   English   中英

jQuery ajax发布到php脚本并更新mysql数据库

[英]jQuery ajax post to php script & update mysql database

我有一个显示库存的页面。 当库存出现故障(或其他任何状态)时,我想从下拉列表中选择该选项,并相应地更新数据库,而无需刷新页面。 我一直在玩.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”。 我的最终结果是将其更新为下拉菜单的值,但是我敢肯定,如果我可以让这部分正常工作,那么我可以弄清楚其余部分。

预先感谢您的任何帮助! Bonnejournée!

编辑

非常有效,非常感谢您。 不幸的是,我遇到了另一个问题。 例如,我有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

成功是成功执行ajax调用时将执行的功能,您也可以像这样使用它

success: function(data) { console.log(data) ... }

现在,您将ajax调用中的值作为简单字符串发送,您希望它是键/值对。

更改此:

data: mydata

对于这样的事情:

data: {'StarmontInventoryItemID' : mydata}

现在服务器端您应该可以访问$_POST['StarmontInventoryItemID']

请注意,您始终可以执行print_r($_POST)来查看发布数据如何到达服务器端。

强制性通知:您正在使用不推荐使用的mysql_*方法,并且需要切换到mysqli_*PDO 您的查询也是不安全的,并且可以进行SQL注入。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM