SitePoint Sponsor |
|
User Tag List
Results 1 to 6 of 6
-
Sep 24, 2009, 16:28 #1
Displaying Errors and Redirecting a Form
Can the following be done entirely with PHP and not using Javascript...
There is a form that has a required field.
When the user clicks the "Submit" button...
1.) If the required field was left blank, then the form should re-appear with a warning message next to the omitted field stating, ERROR: Field must contain a value.
2.) If the form was filled out correctly, then the form should re-direct to another page (which is the next step in whatever process).
If so, what would the PHP code look like?
Thanks,
Amy
-
Sep 24, 2009, 18:35 #2
- Join Date
- Jan 2009
- Posts
- 73
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Yes that should be easier with PHP!
Sample code(not tested):
PHP Code:<?php
$errors= array();
if(isset($_POST['submit']))
{
if(empty($_POST['name']))
{
$errors['name']='Name is required';
}
if(empty($_POST['email']))
{
$errors['email']='email is required';
}
if(empty($errors))
{//validation success
//send email, save to db ...
header('Location: thankyou.html');
}
}
?>
<html>
<head>
</head>
<body>
<form method='post'>
Name: <input name='name' type='text'><?php if(isset($errors['name'])) echo $errors['name']; ?><br>
Email: <input name='email' type='text'><?php if(isset($errors['email'])) echo $errors['email']; ?>
</form>
</body>
</html>
Javascript form validation gives quicker response.
-
Sep 24, 2009, 21:51 #3
Thanks for the code snippet.
Does anyone else have a different approach?
This seems to work, other than it needs to be fleshed out like making the form sticky.
Amy
-
Sep 24, 2009, 22:25 #4
- Join Date
- Jul 2008
- Posts
- 5,757
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
That's the core logic.
Make sure to call exit; after sending a redirect header
Make sure to use htmlspecialchars() if you output those variables back into the form.
Don't use empty() if a falsy value might be acceptable. For example, 0. strlen(trim(...)) is usually reasonable.
-
Oct 6, 2009, 19:32 #5
prasanthmj,
I adapted your code but it doesn't seem to be working?!
I stepped through my code in NetBeans and the array is receiving a value...
Code:<?php // Start output buffering //ob_start(); // Initialize a session session_start(); // Initialize $errors array $errors= array(); if (isset($_POST['submitted'])) { // Handle the form. echo "For was submitted!"; if(empty($_POST['email'])) { $errors['name']='E-mail is required'; } if(empty($errors)) { //validation success //send email, save to db ... header('Location: thankyou.html'); } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en-US"> <head> <title>Sign-In/Create Account</title> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <!--<link rel="stylesheet" type="text/css" href="tabs.css" />--> <style type="text/css"> </style> </head> <body> <h2>Registering is quick and easy...</h2> <!-- <form action="104_CreateUser.xhtml" method="post"> --> <form action="103_SignIn.php" method="post"> <ol> <!-- a.) E-MAIL ADDRESS --> <li class="step"> <label for="email">Enter your e-mail address:</label> <input id="email" type="text" name="email" size="30" maxlength="80" /> <?php if(isset($errors['email'])) echo $errors['email']; ?> </li> </ol> <!-- HIDDEN INPUT --> <input type="hidden" name="submitted" value="TRUE" /> <!-- PROCEED BUTTON --> <input type="image" src="images/Proceed.jpg" alt="Proceed" class="proceed" border="0" /> </form> </div> </div> </body> </html>
Amy
-
Oct 6, 2009, 21:24 #6
Saving a value here...
if(empty($_POST['email'])) {
$errors['name']='E-mail is required';
}
But then referencing it here...
<label for="email">Enter your e-mail address/label>
<input id="email" type="text" name="email" size="30" maxlength="80" />
<?php if(isset($errors['email'])) echo $errors['email']; ?>
Gets ya everytime?!
Thanks,
Amy
Bookmarks