Reputation: 25
I am trying to do the following validation on my registration page;
at the beginning of the page I have,
<?php
if(!isset($_POST['sumbit']) && (!validateName($_POST['phonenumber'])) {
here html show errors if the form was submitted with errors
<form method= "post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>">
<div>Phone Number</div>
<input type="text" name="phonenumber" size="20" placeholder="phone" value="<?php echo htmlspecialchars($_POST['phonenumber']); ?>" required>
<input type="submit" name="submit" value="Next">
</form>
but I am getting "Notice: Undefined index: phonenumber in ...." before submit page
I have the validationName function in a separated file that I call from here but I am getting error
any help??
Upvotes: 1
Views: 59
Reputation: 298
Try setting new var like $phn_number = isset($_POST['phonenumber']);
if(!isset($_POST['sumbit']) && (!validateName($phn_number)) {
Its just a NOTICE, you may work even if continues.
Upvotes: 0
Reputation: 16727
It should be
if(isset($_POST['submit']) && (!validateName($_POST['phonenumber'])) {
notice the lack of '!' and you hava a typo 'sumbit'. But to make it simpler it could be
if(isset($_POST['phonenumber']) && (!validateName($_POST['phonenumber'])) {
Upvotes: 0
Reputation: 2815
You have to validate that the variable is set:
if(isset($_POST['var']))
or (for checking if it is not empty):
if(!empty($_POST['var']))
But you can just ignore notices, this is not the best solution, but they are not shown on productive environments. Furthermore, you can leave out this:
action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>"
For your error (variable not set), try this for displaying all POSTed things:
var_dump($_POST);
EDIT: There is another logical mistake:
if(!isset($_POST['sumbit']) && (!validateName($_POST['phonenumber'])) {
Should be this, to display something in case of error:
if(isset($_POST['submit']) && !validateName($_POST['phonenumber'])) {
Upvotes: 3