0

I am completely new to JS and PHP programming. 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.

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. I have not been able to pass the value from js to php.

traktor
  • 17,588
  • 4
  • 32
  • 53
Sindhu
  • 100
  • 3
  • 14
  • Possible duplicate of [How to pass JavaScript variables to PHP?](http://stackoverflow.com/questions/1917576/how-to-pass-javascript-variables-to-php) – Sagar V Mar 31 '17 at 14:44
  • 1
    You need to parse the URL query in your php; (ie listen to the Ajax post on your server) especially since your ajax is sending data with a checkinoutstatus – Denis Tsoi Mar 31 '17 at 14:45
  • when i return the CheckinoutStatus in the ajax i am getting the output as true.. when i use the variable in php it is throwing undefined variable error – Sindhu Mar 31 '17 at 15:44
  • Edit for readability. – traktor Mar 31 '17 at 23:11
  • I believe it's the fact you have an ampersand instead of a question mark at the beginning of `data`. – Connor Gurney Mar 31 '17 at 23:16
  • Does the PHP code starting with `if(isset($_POST['CheckinoutStatus']))` appear in the web page `.php` file where the button gets clicked, or in `functions.php` where the value is received and the database gets updated? – traktor Mar 31 '17 at 23:26
  • Both the js and php code are in the same functions.php file..they are in the same page.. – Sindhu Apr 03 '17 at 14:38

2 Answers2

0

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.

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:

UPDATE `mystatustable`
SET `checkinoutstatus` = $_GET['key']
WHERE `id` = $_GET['key'];
Sloan Thrasher
  • 4,953
  • 3
  • 22
  • 40
  • I am not using any hidden id.. Do I have to use one..?? I am just getting a variable from JS and passing it to PHP to update a database table.. Think I have to do some research on hidden fields.. – Sindhu Mar 31 '17 at 15:52
  • How will you know which record in the database to update? – Sloan Thrasher Mar 31 '17 at 16:45
  • I have a table in the database with CheckinoutStatus and date field.. So in the calendar when i click a particular date the CheckinoutStatus gets updated for that date.. – Sindhu Mar 31 '17 at 17:22
  • So you would need to pass that date so the SQL update statement will be able to update the correct record. – Sloan Thrasher Mar 31 '17 at 17:41
  • I could able to update the record in the correct date.. but i couldn't able to get the CheckinoutStatus=true to PHP so that i could use the below sql statement $update = $db->query("UPDATE Details SET CheckinoutStatus='$CheckinoutStatus' where date='$date' "); and the database should have CheckinoutStatus=1 – Sindhu Mar 31 '17 at 17:51
  • Since you are using get syntax, the value is passed as a string, not a boolean. Notice in my answer, I pass 1 for the value of the status, not "true" – Sloan Thrasher Mar 31 '17 at 17:53
  • I just now tried giving the ChckinoutStatus value as 1.. But the problem is the program is not going into the if loop... if(isset($_POST['CheckinoutStatus'])) { – Sindhu Mar 31 '17 at 18:05
0

UPDATE: I actually got it working when I pass the values through links..

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..

Sindhu
  • 100
  • 3
  • 14