Reputation: 4908
I have a simple form below where I check for zero field values in a submitted form. If I find a zero field I set the $register_failed_message accordingly, identifying the field. At that point I just want to skip the rest of the form processing and display the form because the form includes a
<?=$register_failed_message?>
to tell the user, right on the form, what the problem is.
But it's not clear how to break out of the processing loop and jump down to the form display HTML. I have exits there, now, but that's not going to work because they stop the entire script. I need a goto to ?>.
Is there a standard way to program this kind of thing?
Thanks
<?php
if(!empty($_POST)){
// Form was submitted.
if(empty($_POST['firstName'])) {
$register_failed_message = "Please enter a firstName.";
exit;
}
if(empty($_POST['lastName'])) {
$register_failed = "Please enter a lastName.";
exit;
}
if(empty($_POST['email'])) {
$register_failed = "Please enter an email.";
exit;
}
[Process form]
header("Location: login.php");
exit;
}
?>
<!doctype html>
<!-- HTML5 -->
<html>
<head>
<body>
<div id="content">
<h2 id="heading">Open a Free Account . . .</h2>
<form id='register' action=<?= $_SERVER["PHP_SELF"] ?> autocomplete="off" method="post">
<div id='fname_label' class='label'>First Name:</div>
<input id='fname' type="text" name="firstName" />
<div id='lname_label' class='label'>Last Name:</div>
<input type="text" name="lastName" />
<div id='email_label' class='label'>Email:</div>
<input type="text" name="email" autocomplete="off" />
<input id='register_button' type="submit" value="Open your Free Account" />
</form>
<div id='register_failed'><?=$register_failed_message?></div>
</div>
</div>
</body>
</head>
</html>
Upvotes: 2
Views: 996
Reputation: 686
<?php
$register_failed_message = '';
if(isset($_POST))
{
// Form was submitted.
$error = array();
if(empty($_POST['firstName']))
{
$error[] = "Please enter a firstName.";
}
if(empty($_POST['lastName']))
{
$error[] = "Please enter a lastName.";
}
if(!preg_match('#^[a-z0-9.!\#$%&\'*+-/=?^_`{|}~]+@([0-9.]+|([^\s]+\.+[a-z]{2,6}))$#si', $_POST['email']))
{
$error[] = "Please enter an email.";
}
if(count($register_failed_message) == 0)
{
header("Location: login.php");
die();
}
$register_failed_message = implode('<br />',$error);
}
?>
Upvotes: 1
Reputation: 6202
I was going to just suggest break() as I did in the comments, but that is not your best solution here. You really don't want to throw a message "please enter a first name" and have them enter a first name THEN throw ANOTHER message "please enter a last name". So don't break, just gather ALL the error messages and display them:
(also did you notice that you named the the message inconsistently?)
<?php
if(!empty($_POST)){
// Form was submitted.
$register_failed_message = "";
if(empty($_POST['firstName'])) {
$register_failed_message .= "Please enter a firstName.<br />";
}
if(empty($_POST['lastName'])) {
$register_failed_message .= "Please enter a lastName.<br />";
}
if(empty($_POST['email'])) {
$register_failed_message .= "Please enter an email.<br />";
}
header("Location: login.php");
}
?>
Notice how I use .=
. This is a handy shortcut that means "append to" instead of "set to".
Upvotes: 0
Reputation: 1745
There are a couple problems with your code. Firstly, you have two message variables, $register_failed_message and $register_failed, but you only reference one later. Secondly, your messages are going to overwrite each other. If you put them in an array, you can display all the messages, not just one, if necessary.
Here is how you do what you're trying to do, plus those two mistakes fixed.
<?php
$register_failed = array();
if(!empty($_POST)){
// Form was submitted.
if(empty($_POST['firstName'])) {
$register_failed[] = "Please enter a firstName.";
}
if(empty($_POST['lastName'])) {
$register_failed[] = "Please enter a lastName.";
}
if(empty($_POST['email'])) {
$register_failed[] = "Please enter an email.";
}
//If there are no error messages, we can process the form
if(sizeof($register_failed) == 0) { //This means there are 0 messages collected
[Process form]
header("Location: login.php");
exit; //we exit here to get to the other page
}
}
?>
Later:
<div id='register_failed'>
<?php echo implode('<br>',$register_failed); ?>
</div>
Upvotes: 2
Reputation: 6319
You can break out of scripts with "return", but it will stop the page content too.
You can put your script into another file, use "return" to stop your script, and then include it on the page you have provided.
Upvotes: 0