所以我有一个方法,只要点击一个按钮就会关闭。按钮是根据数据库中表的行数生成的。它们中的每一个都有一个唯一的id,它与数据库中的特定项id设置相同(这将使签入和退出项更容易。)所以方法看起来像这样
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);
?>
任何帮助都会很棒,谢谢大家。
答案 0 :(得分:0)
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']