4

I am new in PHP.

I have a variable in jquery called editMode set to false. I used $.post() in jquery to pass the variable in a php function. But when I check its value, the value is fine, but if statement does not function the way it's supposed to. Here is my code:

jQuery (just a snippet):

var editMode = false;
$(document).ready(function(){
    //some function that handles UI input that triggers editMode=true

    $("#form").submit(function(event){
      event.preventDefault();

      var fName = $("#firstname").val();
      var lName = $("#lastname").val();
      //... and the rest of post values coming from UI input

      $.post('includes/sql/register.php', {
         fName: fName,
         lName: lName,
         //... and the rest of post values coming from UI input
         editMode: editMode // this is where I post the value for editMode
      },
      //jQuery function to handle the result
});

In my register.php:

<?php
  if(isset($_POST['submit'])){
     $fName = mysqli_escape_string($conn,$_POST['fName']);
     $lName = mysqli_escape_string($conn,$_POST['lName']);
     //and other post values from UI input
     $editMode = $_POST['editMode'];

     if($editMode) {
        echo $editMode . ": edit"; //fires update query
     } else {
        echo  $editMode . "add"; //fires insert query
     }
  } 
?>

When testing the code, it returns the correct value of $editMode, but does not perform the if statement correctly. What I mean is, if $editMode is true, it echoes: true: edit, which is good. But when it's false, it echoes: false: edit. So it still performs the if clause, even if its value is now false. I also tried if($editMode === true) {//edit} else {//add}, and it does the opposite. Whether $editMode is true or false, it performs the else clause. Please enlighten me.

2
  • You can send a flag as 1 for true and 0 for false (instead of true/false in JavaScript). That becomes if ("0") which is false, or if ("1") which is true. All POST data are sent as strings (booleans too for some reason, if I'm not mistaken). Commented Jul 29, 2019 at 4:39
  • That answered it! Can you post this as an answer, so I can mark this as solved. Thanks mate! :) Commented Jul 29, 2019 at 4:45

3 Answers 3

4

All POST requests sent will be treated as strings - and if I'm not mistaken, a boolean value of true in JavaScript becomes the string of "true" in PHP (as it is converted to a sting before being sent to PHP).

I would suggest sending the data as an integer representation of the boolean value, that is doing sending the editMode data as editMode ? 1 : 0.

$.post('includes/sql/register.php', {
    fName: fName,
    lName: lName,
    //... and the rest of post values coming from UI input
    editMode: editMode ? 1 : 0 
},

Which in PHP becomes if ("1") for true and if ("0") for false. Since PHP is a weakly typed language, these will be treated as integers (if (1) and if (0)) which will be true and false expressions respectively.

So in PHP you can do

if ($editMode) {
    echo "true: edit"; //fires update query
} else {
    echo "false: edit";  //fires insert query
}
Sign up to request clarification or add additional context in comments.

Comments

1

You can validate the boolean value before checking in your conditional statement:

$editMode = 'False';
$editMode = filter_var($editMode, FILTER_VALIDATE_BOOLEAN);
if ($editMode) {
    echo $editMode . ": edit"; //fires update query
} else {
    echo  $editMode . "add"; //fires insert query
}

1 Comment

Hmm.. I like this answer better, since it "forces" the PHP to treat the variable as boolean, instead of string. Thanks mate!
0

if you want to keep your code as it is just send the false in string

instead of

var editMode = false;

use

var editMode = "false";

and in your php file

if($editMode == "true") {

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.