所以我有一种方法,每当单击一个按钮时,该方法都会关闭。这些按钮是根据数据库表中的行数生成的。它们每个都有一个唯一的ID,该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);

?>


任何帮助将是巨大的,在此先感谢大家。

最佳答案

Java脚本

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']

08-18 22:08
查看更多