简体   繁体   English

在同一页面上将变量从javascript获取到PHP

[英]Getting variable from javascript to PHP on the same page

I am completely new to JS and PHP programming. 我是JS和PHP编程的新手。 Please help me with the following code.. 请用以下代码帮助我。

<?php

echo'
<button id="checkin" onclick="myFunction()">CheckIn</button>        
<script>
function myFunction() {
document.getElementById("checkin").disabled = true;
var CheckinoutStatus=true;
        $(function(){
        $.ajax({
              type: "POST",
              url: "functions.php",
              data: "&CheckinoutStatus="+CheckinoutStatus,
              success: function(data)
              {
                 alert("Checked In");
              }
            });
        });
   }
 </script>
 ';
 if(isset($_POST['CheckinoutStatus']))
 {

     $CheckinoutStatus=isset($_POST['CheckinoutStatus']);
     $update = $db->query("UPDATE Details SET     CheckinoutStatus='$CheckinoutStatus' where date='$date' ");

}
?>

Clicking the checkin button should disable the button, send a CheckinoutStatus variable value of true to the database using an AJAX call, and then show a "Checked In" alert after the database has been updated. 单击checkin按钮应禁用该按钮,使用AJAX调用将CheckinoutStatus变量值true发送到数据库,然后在更新数据库后显示“已签入”警报。

I am defining and setting the variable CheckinoutStatus to true in js, and using AJAX, am trying to get that value in PHP and update that data in an existing table. 我在js中定义变量CheckinoutStatus并将其设置为true ,然后使用AJAX尝试在PHP中获取该值并更新现有表中的数据。 I have not been able to pass the value from js to php. 我无法将值从js传递到php。

Unless your table only has one record, you will also need to pass a key of some sort so you can update the correct record. 除非您的表只有一个记录,否则您还需要传递某种键,以便您可以更新正确的记录。 Fir the line below, I'll presume you have a hidden field with an id of "key" in your form. 在下面的代码行中,我假设您的表单中有一个ID为“ key”的隐藏字段。

Change the line: 更改行:

data: "&CheckinoutStatus="+CheckinoutStatus,

To: 至:

data: "&CheckinoutStatus=1&key="+$('#key').val(),

Then create a PHP file named functions.php which checks $_GET['CheckinoutStatus'] for the value and save it to your table using update with a where clause similar to: 然后创建一个名为functions.php的PHP文件,该文件检查$ _GET ['CheckinoutStatus']的值,并使用带有where子句的update将其保存到表中,该语句类似于:

UPDATE `mystatustable`
SET `checkinoutstatus` = $_GET['key']
WHERE `id` = $_GET['key'];

UPDATE: I actually got it working when I pass the values through links.. 更新:当我通过链接传递值时,我实际上使它工作了。

window.location.href = "exp1.php?w1=" + CheckinoutStatus + "&w2="+date ; window.location.href =“ exp1.php?w1 =” + CheckinoutStatus +“&w2 =” + date;

but I cannot able to do that by using the same php file.. I have to create a new php file exp1.php inorder for it to work.. 但是我无法通过使用相同的php文件来做到这一点。我必须创建一个新的php文件exp1.php才能使其工作..

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

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