Jump to content

Showing Errors


chriscloyd

Recommended Posts

heres my delima okay I want to check a form and if there is multi errors i want it to show all errors at once heres my code
[code]
<?php
//connect to mysql
mysql_connect('localhost','root','');
mysql_select_db('site');
//register begins
//define vars
$first = $_POST['first'];
$last = $_POST['last'];
$username = $_POST['username'];
$password = $_POST['password'];
$password2 = $_POST['password2'];
$email = $_POST['email'];
$email2 = $_POST['email2'];
$day = $_POST['day'];
$month = $_POST['month'];
$year = $_POST['year'];
$key = $_SESSION['key'];
session_destroy('key');
$sc = $_POST['sc'];
$hostname = $_SERVER["HTTP_HOST"];
$ip = $_SERVER['REMOTE_ADDR'];
//check them all
if (!$first) {
$error = '-You did not supply your First Name';
$complete = 0;
} elseif (!$last) {
$error = '-You did not supply your Last Name';
$complete = 0;
} elseif (!$username) {
$error = '-You did not supply a Username';
$complete = 0;
} elseif (!$password) {
$error = '-You did not supply a Password';
$complete = 0;
} elseif (!$password2) {
$error = '-You did not supply a Confirmation Password';
$complete = 0;
}  elseif (!$email) {
$error = '-You did not supply your Email address';
$complete = 0;
} elseif (!$email2) {
$error = '-You did not supply your Confirmation Email address';
$complete = 0;
}  elseif (!$day) {
$error = '-You did not supply your Birthday day';
$complete = 0;
} elseif (!$month) {
$error = '-You did not supply your Birthday month';
$complete = 0;
} elseif (!$year) {
$error = '-You did not supply your Birthday year';
$complete = 0;
} elseif (!$sc) {
$error = '-You did not supply the Security Code';
$complete = 0;
}
if ($complete == 0) {
//go back to page and display errors
}
?>[/code]
at the bottom if $complete == 0 i want it to go back and show all the errors is there a way of showing all the errors at once?
Link to comment
https://forums.phpfreaks.com/topic/32162-showing-errors/
Share on other sites

Put all the errors into an array. So instead of...

[code=php:0]
$error = '-You did not supply your First Name';
[/code]

Use...

[code=php:0]
$error[] = '-You did not supply your First Name';
[/code]

Then....

[code=php:0]
if ($complete == 0) {
  foreach($errors as $error) {
    echo $error."<br />";
  }
}
[/code]
Link to comment
https://forums.phpfreaks.com/topic/32162-showing-errors/#findComment-149268
Share on other sites

Put session_start() at the top of both pages. Store your errors using something like....

[code=php:0]
$_SESSION['error'][] = "your error message";
[/code]

then, after you redirect back use something allong the lines of.

[code=php:0]
if (isset($_SESSION['error'])) {
  foreach($_SESSION['error'] as $error) {
    echo $error."<br />";
  }
}
[/code]
Link to comment
https://forums.phpfreaks.com/topic/32162-showing-errors/#findComment-149274
Share on other sites

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.