I am a php beginner. Can anyone explain me why it is not working and please correct the code if possible. registrationp.php if(empty($_POST['password'])) { $error="enter your password"; header('Location:registration.php?errors='.$error. ' '); } registration.php <input type="password" name="password" id="password" placeholder="password"><?php echo $_GET['errors']; ?>
can you explain what is happening and if you see any error message what it is? Also it is tricky to guess what other code you have that could affect the code that you have posted. and please use the code feature in the texteditor when posting code. makes it much easier to look at registrationp.php if(empty($_POST['password'])) { $error="enter your password"; header('Location:registration.php?errors='.$error. ' '); } PHP: registration.php <input type="password" name="password" id="password" placeholder="password"><?php echo $_GET['errors']; ?> PHP:
Hi stephan,By mistakenly I point out this code has error. My error is out of this part in coding. Another coding error: <?php $con=mysql_connect("localhost","root","","test"); mysql_select_db("test",$con); if(empty($_POST['emailid'])) { header("Location:registration.php?error=enter your email id"); } else{ $emailid=$_POST['emailid']; } if(empty($_POST['name'])) { header("Location:registration.php?error1=enter your name"); } else{ $name=$_POST['name']; } if(empty($_POST['age'])) { header("Location:registration.php?error2=enter your age"); } else{ $age=$_POST['age']; } if(empty($_POST['password'])) { $error="enter the password"; header('Location:registration.php?errors='.$error.''); } else{ $password=$_POST['password']; } ?> <html> <body> <form action="registrationp.php" method="POST"> <input type="text" name="emailid" placeholder="Emailid" id="emailid"><?php echo $_GET['error']; ?></br> <input type="text" name="name" id="name" placeholder="Name"><?php echo $_GET['error1']; ?></br> <input type="text" name="age" id="age" placeholder="Age"><?php echo $_GET['error2']; ?></br> <input type="password" name="password" id="password" placeholder="password"><?php echo $_GET['errors']; ?></br> <input type="hidden" name="formsubmitted" value="True"></br> <input type="submit" value="Register"> </form> </body> </html> I could like to show all field errors but it shows me only last field's error. I know I did a minor mistake in coding. but I couldnt correct that please guide me.
The whole code is... kinda horrible. First of all, why do you use $_GET to show errors? Just use $_POST, and show errors based on which input contains no or wrong info. Something like this: First, change your submit to also have a name-attribute, something like: <input type="submit" name="submit_form" value="Register"> PHP: Then: $errors = array(); if (isset($_POST['submit_form'])) { if (empty($_POST['emailid'])) { $errors['emailid'] = 'Please enter your emailid'; } if (empty($_POST['name'])) { $errors['name'] = 'Enter your name'; } } PHP: Then you use the array to show errors, based on whether or not the array_key_exists - something like this: <input type="text" name="emailid"><?php if (array_key_exists('emailid',$errors)) { echo $errors['emailid']; } ?> PHP: And so on. And for the love of god, learn how to use the [ code ]-feature