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

Link to comment
Share on other sites

<?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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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);
}
?>

Link to comment
Share on other sites

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!';
	}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.