Thursday, August 30, 2018

php - Trying to pass value to ISSET condition but getting error



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.



     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


Answer



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



    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);
}

?>

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...