What is this 'isset($_POST)' code doing?
Trying to understand what this isset($_POST) code is doing
if (isset($_POST['Submit'])) {
$title=$_POST['title'];
$forename = $_POST['forename'];
$surname=$_POST['surname'];
$dob=$_POST['dob'];
$gender=$_POST['gender'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$password=$_POST['password'];
if (authRegister($title, $forename, $surname, $dob, $gender, $email, $phone, $password))
{
echo 'Thank you for registering your details, you can now lo开发者_开发问答gin';
}
else
{
outputErrors();
}
It checks that there is a field with name 'Submit' (probably submit button) in the form submitted to this php page. In other words it shows that for has been submitted and has to be processed.
In addition I'll say that whenever you post form to a php page all fields that have name attribute of the form are represented in the $_POST
array. There is 2 ways to check if a key exists in an array:
$array = array('key_name' => 'value');
isset($array['key_name']);
array_key_exists('key_name', $array)
isset() also returns false if the 'key_name'
exists in $array
but value is null
. array_key_exists would return true in this key.
$_POST is the super global variable for user parameters sent by a form using the POST method.
the expression isset($_POST['Submit']) return true only if 'Submit' is an existing parameter, i.e. if the user has sent such a value using n HTML form. It's typically testing if a button named 'Submit' has been used to send a form.
If it's so, then the next code is retrieving user parameters also sent by the same form (title, forename, srname, ...). The parameters are saved into PHP local variables. This part of the code assumes that this extra parameters has been sent by the form. Nevertheless such parameters my be omitted if the sent of the form is hacked.
The isset()
function checks if the argument variable exists or "is set".
isset($_POST['submit'])
is typically used to check if a form has been submitted
Prior to running some code. In your example, it is likely that the form submit
Button has been named 'submit'. If the form button had been named 'widget'
Then you would see isset($_POST['widget'])
.
isset
checks if variable exists, it can exist and be empty. If you want to check if variable is empty then use empty
.
精彩评论