Jump to content

[SOLVED] shoing errors using implode or explode


chriscloyd

Recommended Posts

<?php
include("config.php");
//check required fileds 
//username password c_password email c_email
if ($_POST['username'] == '') {
$error = '1';
$reason = '-You did not submit a username.';
}
if ($_POST['password'] == '') {
$error = '1';
$reason = '-You did not submit a password.';
}
if ($_POST['c_password'] == '') {
$error = '1';
$reason = '-You did not submit a email address.';
}
if ($_POST['email'] == '') {
$error = '1';
$reason = '-You did not submit a password.';
}
if ($_POST['c_email'] == '') {
$error = '1';
$reason = '-You did not submit a confirmation email address.';
}
if ($error) {
//return to register page and display reasons
}
?>

say if they did not fill in any of those fields I want to go back and show all the errors when i do it, it was only showin 1 error

<?php
include("config.php");
//check required fileds 
//username password c_password email c_email
if ($_POST['username'] == '') {
$error = '1';
$reason .= '-You did not submit a username.';
}
if ($_POST['password'] == '') {
$error = '1';
$reason .= '-You did not submit a password.';
}
if ($_POST['c_password'] == '') {
$error = '1';
$reason .= '-You did not submit a email address.';
}
if ($_POST['email'] == '') {
$error = '1';
$reason .= '-You did not submit a password.';
}
if ($_POST['c_email'] == '') {
$error = '1';
$reason .= '-You did not submit a confirmation email address.';
}
if ($error) {
//return to register page and display reasons
}
?>

 

use .= to append to the current string. That should work.

If the OP does it that way all the messages end up on one line. To get them on separate lines, try something like this:

<?php
include("config.php");
$reason = array();
//check required fileds 
//username password c_password email c_email
if ($_POST['username'] == '') {
$reason[] = '-You did not submit a username.';
}
if ($_POST['password'] == '') {
$reason[] = '-You did not submit a password.';
}
if ($_POST['c_password'] == '') {
$reason[] = '-You did not submit a email address.';
}
if ($_POST['email'] == '') {
$reason[] = '-You did not submit a password.';
}
if ($_POST['c_email'] == '') {
$reason[] = '-You did not submit a confirmation email address.';
}
if (!empty($reason)) {
        echo 'The following errors were detected:<br>' . implode("<br>\n",$reason) . "<br>\n"); // shows how to display the messages
//return to register page and display reasons
}
?>

 

You'll notice that you don't need a "error" flag with this method, just check to see if the array is empty or not.

 

Ken

When i do that its showing all the errors go here

http://www.chaoslegionclan.net/CEGL-Work/newsite/index.php?p=register and try to fill in just the user name

it comes back with all the errors not just the password c-password email and c-email

http://www.chaoslegionclan.net/CEGL-Work/newsite/index.php?p=register

 

yes that link i showed u shows you the form even when i eneter info into the required ones

which r

username password confirm password email confirm email i still get the errors heres my updated code

 

<?php
include("config.php");
//check required fileds 
//username password c_password email c_email
$reason = array();
$username = $_POST['username'];
$password = $_POST['password'];
$c_password = $_POST['c_password'];
$email = $_POST['email'];
$c_email = $_POST['c_email'];
if (empty($username)) {
$reason[] = '-You did not submit a username.';
}
if (empty($password)) {
$reason[] = '-You did not submit a password.';
}
if (empty($c_password)) {
$reason[] = '-You did not submit a email address.';
}
if (empty($email)) {
$reason[] = '-You did not submit a password.';
}
if (empty($c_email)) {
$reason[] = '-You did not submit a confirmation email address.';
}
if (!empty($reason)) {
$reasons = implode("<br>",$reason);
header("Location: ../index.php?p=register&error=".$reasons);
}
?>

Why not do this:

 

if (isset($_POST['username'])) {
	$username = $_POST['username'];
} else {
	$username = NULL;
}
if (isset($_POST['password']) && isset($_POST'c_password'])) { //if password and conformation is set
           if ($_POST['password'] == $_POST['c_password'])) { //if pass 1 matches pass 2
	$password = $_POST['password']; //set the password variable
           } else { // passwords do not match
                $password = NULL
           }
} else { // password or conformation is blank
	$password = NULL;
}
if (isset($_POST['email'])) {
	$email = $_POST['email'];
} else {
	$email = NULL;
}
if ($username && $email && $password) { //if the variables are set
	//register the user
} else { //variables arent set, lets echo an error message for the ones not set
	if ($username == NULL) {
		echo 'You must enter a username!';
	}
	if ($password == NULL) {
		echo 'Your passwords do not match 100%. Please go back and confirm your password.!';
	}
	if ($email == NULL) {
		echo 'You must enter valid email address!';
	}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.