0
votes

I have created a PHP script and if use the script its always going to else condition and I am not sure why its not going to else condition.

  <?php
   require_once  'db_functions.php';
   $db = new DB_Functions();
   $response = array();

   $phone="1234";
   $name="Test";
   $birthdate="1994-01-01";
   $address="123 M";

    if(isset($_POST['phone']) &&
    isset($_POST['name']) &&
    isset($_POST['birthdate']) &&
    isset($_POST['address']))


   {
    echo "Hello World 1";

    $phone = $_POST['phone'];
    $name = $_POST['name'];
    $birthdate = $_POST['birthdate'];
    $address = $_POST['address'];

    echo "Hello World 2";

   }

   else{

    echo "Hello";
    $response["error_msg"] = "Required parameter 
    (phone,name,birthdate,address) is missing!";
    echo json_encode($response);
    }
    ?>

Output:

_msg":"Required parameter (phone,name,birthdate,address) is missing!"}

If the value is passed then it should go to if condition instead of else condition.

Options Tried

Tried Below options but I am getting empty value:

$test=$_POST['phone']; echo "Hey......".$test;

echo isset($_POST['phone']);

URL USED https://www.aaa.ccc/php/register.php?phone=232&name=test&birthdate=1954-04-04&address=232

1
To get a better idea try to print each of the conditions and also $_POST. like echo isset($_POST['phone']) - mdh.heydari
You are checking for $_POST in your if condition but the variable you have assigned are local. Are you sure you are getting anything in $_POST. - ascsoftw
I am getting error unexpected T_ECHO. if(echo isset($_POST['phone']) && echo isset($_POST['name']) && echo isset($_POST['birthdate']) && echo isset($_POST['address'])) - Josh
You don't need to echo inside a IF statement, the way you have described your code in question is fine. - ascsoftw
Try using $_GET[] instead of $_POST[] - Nigel Ren

1 Answers

0
votes

You are passing the parameters in the url using the GET method, not POST, so you need:

 <?php
   require_once  'db_functions.php';
   $db = new DB_Functions();
   $response = array();

   $phone="1234";
   $name="Test";
   $birthdate="1994-01-01";
   $address="123 M";

    if(isset($_GET['phone']) &&
    isset($_GET['name']) &&
    isset($_GET['birthdate']) &&
    isset($_GET['address']))


   {
    echo "Hello World 1";

    $phone = $_GET['phone'];
    $name = $_GET['name'];
    $birthdate = $_GET['birthdate'];
    $address = $_GET['address'];

    echo "Hello World 2";

   }

   else{

    echo "Hello";
    $response["error_msg"] = "Required parameter 
    (phone,name,birthdate,address) is missing!";
    echo json_encode($response);
    }
    ?>