0

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

Josh
  • 33
  • 5
  • To get a better idea try to print each of the conditions and also `$_POST`. like `echo isset($_POST['phone'])` – mdh.heydari Jul 21 '19 at 05:23
  • 1
    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 Jul 21 '19 at 05:34
  • 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 Jul 21 '19 at 05:40
  • You don't need to echo inside a IF statement, the way you have described your code in question is fine. – ascsoftw Jul 21 '19 at 05:54
  • Try using `$_GET[]` instead of `$_POST[]` – Nigel Ren Jul 21 '19 at 07:32
  • Are you posting data by submitting a `
    ` with `method="post"`? It might help to include the relevant HTML in your question.
    – showdev Jul 21 '19 at 07:49
  • Possible duplicate of [When should I use GET or POST method? What's the difference between them?](https://stackoverflow.com/questions/504947/when-should-i-use-get-or-post-method-whats-the-difference-between-them). – showdev Jul 21 '19 at 11:21

1 Answers1

0

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);
    }
    ?>
François Huppé
  • 2,006
  • 1
  • 6
  • 15