gpt4 book ai didi

php - 尝试获取项目 id 以与 ajax 一起使用

转载 作者:行者123 更新时间:2023-11-29 19:49:06 27 4
gpt4 key购买 nike

所以我有一个只要单击按钮就会关闭的方法。这些按钮是根据数据库表中的行数生成的。它们每个都有一个唯一的 id,该 id 设置为与数据库中的特定项目 id 相同(将使 checkin 和 checkout 项目更容易。)因此该方法如下所示

function calculate(event) {
console.log(event.target.id);
var id = event.target.id;
var valueofbut = event.target.value;
if(valueofbut==="Check Out"){
document.getElementById(id).value = "Check In";
$.ajax({
type: "POST",
url: "checkitemin.php",
}).done(function( msg ) {
alert( "Item has been successfully checked out" + msg );
});

}else{
document.getElementById(id).value = "Check Out";
$.ajax({
type: "POST",
url: "checkitemout.php",
}).done(function( msg ) {
alert( "Item has been successfully checked in" + msg );
});

}
}

我对 php 和 ajax 相当陌生,如何将变量 id 传递到 ajax 中以在 checkitemout.php 中使用。

我的 checkitemout.php 看起来像这样

<?php//basically is there any way to use that variable id from the method in this document..?
session_start();
require_once 'Dbconnect.php';
$res=mysql_query("SELECT * FROM members WHERE memberid=".$_SESSION['user']);
$userRow=mysql_fetch_array($res);
$usersemail = $userRow['email'];
$sql = "UPDATE inventory SET name ='$usersemail' WHERE ";
mysql_query($sql);

?>

任何帮助都会很棒,提前感谢大家。

最佳答案

Javascript

function calculate(event) {
console.log(event.target.id);
var id = event.target.id;
var valueofbut = event.target.value;
if(valueofbut==="Check Out"){
document.getElementById(id).value = "Check In";
$.ajax({
type: "POST",
url: "checkitemin.php",
data: {idparam:id}, //send value of variable id
}).done(function( msg ) {
alert( "Item has been successfully checked out" + msg );
});

}else{
document.getElementById(id).value = "Check Out";
$.ajax({
type: "POST",
url: "checkitemout.php",
data: {idparam:id},
}).done(function( msg ) {
alert( "Item has been successfully checked in" + msg );
});

}
}

PHP

$id=$_POST['idparam']

关于php - 尝试获取项目 id 以与 ajax 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40878520/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com