i f else loop submit check


#1

Hi,
i have a simple form and i will check if the submit button is click or not and i check the input of the fields.
but what ever i do i came in the else loop also if i click the submit button.
if i change the code in is the button not click and i click submit then i come in the if loop and every think is fine.
for the moment i am confused.
I am a beginner in php i hope somebody can help.
Thank’s a lot.

index.php
[php]

Error handler tutorial

Sign up

<br>
<input type="text" name="first" placeholder="Firstname">
<br>
<input type="text" name="last" placeholder="Lastname">
<br>
<input type="text" name="email" placeholder="eMail">
<br>
<input type="text" name="uid" placholder="Username">
<br>
<input type="password" name="pwd" placholder="Password">
<br><br>
<button type="submit" name"submit" class="button">Sign up</button>

signup.inc.php

[php]

<?php // Check if the user have click the signup button if (isset($_POST['submit'])) { // Then we include the database //include_once 'dbh.inc.php'; // And we get the data from the signup form $first = $_POST['first']; $last = $_POST['last']; $email = $_POST['email']; $uid = $_POST['uid']; $pwd = $_POST['pwd']; // Check if inputs are empty if (empty($first) || empty($last) || empty($email) || empty($uid) || empty($pwd)) { header("Location: ../index.php?signup=empty"); exit(); } else { // Check if input characters are valid if (!preg_match("/^[a-zA-Z]*$/", $first)||(!preg_match("/^[a-zA-Z]*$/", $last))) { header("Location: ../index.php?signup=char"); exit(); } else { // Check if email is valide if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { header("Location: ../index.php?signup=invalidemail"); exit(); } else { header("Location: ../index.php?signup=success"); exit(); } } } } else { header("Location: ../index.php?signup=error"); exit(); } [/php] [/php]

#2

The name attribute for the submit button is missing the =, so, there is no valid field named ‘submit’ for the php code to detect. You have also misspelled placeholder twice. You can find these type of errors by validating your html at validator.w3.org

You can also dump the $_POST variable in the php code to see what it actually contains, using -

[php]var_dump($_POST);[/php]

Either you or someone starting with this same code has posted else where and have received a list of things that will simplify the code and make it easier to produce a working application. See the following items that apply to the current code in this thread -

  1. You need to put the form and the form processing code on the same page (the processing code would be above the form.) This will simplify a lot of the logic and you can then re-populate the form fields when there is a validation error so that the user doesn’t need to keep reentering the same data, which increases the chance of a typo. This will also eliminate the redirects back to the index/signup page and let you validate all the inputs at once.

  2. Don’t create variables without a good reason and don’t copy variables to other variables without a good reason. The only time you should copy variables to other variables is if you make a change to the value (meaning) of the data in the variable. For processing user data, it is a good idea to trim all the data so that you can detect if all white-space characters were entered. You can do this with a single statement that operates on the data as a set, not by writing a line of code for each possible variable.

  3. You need to validate all the input data at once, storing individual, helpful, validation errors in an array. The current code stops and redirects at the first validation error, so it can take multiple form submissions to find all the errors. At the end of the validation logic, if the array holding the errors is empty, you can use the submitted data. When you (re)display the form, you can display all the validation errors by looping over the array holding the validation error messages.


#3

See the following example code that implements the above suggestions, plus some items that weren’t specifically listed -
[php]<?php
// define some ‘helper’ functions - these would typically be defined in an external .php file and ‘required’ when needed
// apply html htmlentities to a value
function _ent($val)
{
return htmlentities($val); // this uses the current/default character encoding setting
}

// return an element from an array - used to reference array elements that might not be set
function _element($arr,$index)
{
return isset($arr[$index]) ? $arr[$index] : ‘’;
}

// recursive function to trim data.
function _trim($val)
{
if(is_array($val))
{
return array_map(’_trim’,$val);
} else {
return trim($val);
}
}

// define a array of the expected/required form fields
$fields = [];
$fields[‘first’] = [‘label’=>‘First Name’];
$fields[‘last’] = [‘label’=>‘Last Name’];
$fields[‘email’] = [‘label’=>‘Email’];
$fields[‘uid’] = [‘label’=>‘Username’];
$fields[‘pwd’] = [‘label’=>‘Password’];

$errors = []; // define an array to hold validation errors
$post = []; // define an array to hold a working copy of the submitted form data

// form processing code
if($_SERVER[‘REQUEST_METHOD’] == ‘POST’) // this is a general purpose way of detecting if a post method form has been submitted.
{
// get a trimmed copy of the submitted form data
$post = array_map(’_trim’,$_POST);

// validate the submitted data

// check that the required fields are not empty
foreach($fields as $field=>$arr)
{
	if($post[$field] == '')
	{
		$errors[$field] = "{$arr['label']} is empty.";
	}
}

// check for permitted characters in the first name
if(empty($errors['first']) && !preg_match("/^[a-zA-Z]*$/", $post['first']))
{
	$errors['first'] = "Invalid characters in the {$fields['first']['label']}.";
}

// check for permitted characters in the last name
if(empty($errors['last']) && !preg_match("/^[a-zA-Z]*$/", $post['last']))
{
	$errors['last'] = "Invalid characters in the {$fields['last']['label']}.";
}

// check the email format
if(empty($errors['email']) && !filter_var($post['email'], FILTER_VALIDATE_EMAIL))
{
	$errors['email'] = "Invalid {$fields['email']['label']} format.";
}

// add any other validation here...

// at this point, if there are no errors, use the submitted form data
if(empty($errors))
{
	// use the data in $post here...
	
}

}

// output the html document starting here…
?>

Error handler tutorial

Sign up

<?php // display any errors if(!empty($errors)) { echo implode('
',$errors); } ?>
 <br>
 <input type="text" name="first" placeholder="Firstname" value='<?php echo _ent(_element($post,'first')); ?>'>
 <br>
 <input type="text" name="last" placeholder="Lastname" value='<?php echo _ent(_element($post,'last')); ?>'>
 <br>
 <input type="text" name="email" placeholder="eMail" value='<?php echo _ent(_element($post,'email')); ?>' >
 <br>
 <input type="text" name="uid" placeholder="Username" value='<?php echo _ent(_element($post,'uid')); ?>'>
 <br>
 <input type="password" name="pwd" placeholder="Password" value='<?php echo _ent(_element($post,'pwd')); ?>'>
 <br><br>
 <button type="submit" name="submit" class="button">Sign up</button>
[/php]

#4

hi phdr,

many thank’s again for your help and for the teaching sometimes i can not see the simple things. I follow a tutorial in youtube https://www.youtube.com/watch?v=qVU3V0A05k8&index=1&list=PLY47cuSMzvAbCiASrbUVsWGA9e4GWeSQ_
and there are many things different from the the things i learn here.

Salamat
ignarein