Jump to content

Error Messages Not Showing


marcus

Recommended Posts

I am making a user registration and it just checks to see if all the fields are filled in. But if they aren't then the file would echo off an error.

[code]
<?php include('header.php'); ?>
<?php
$act = $_POST[act];

if(!isset($act)){
echo "<table border=0 cellspacing=3 cellpadding=2>\n";
echo "<form name=reg method=post action='".$_SERVER[PHP_SELF]."'>\n";
echo "<tr><td class=gbr>Username:<td class=gbr><input type=text name=username>\n";
echo "<tr><td class=gbr>Password:<td class=gbr><input type=password name=pass>\n";
echo "<tr><td class=gbr>Confirm:<td class=gbr><input type=passowrd name=pass2>\n";
echo "<tr><td class=gbr>Email:<td class=gbr><input type=text name=email>\n";
echo "<tr><td class=gbr>Confirm:<td class=gbr><input type=text name=email2>\n";
echo "<tr><td class=gbr>Birthday:<td class=gbr>
<select name=month>
<option value=January>January
<option value=February>February
<option value=March>March
<option value=April>April
<option value=May>May
<option value=June>June
<option value=July>July
<option value=August>August
<option value=September>September
<option value=October>October
<option value=November>November
<option value=December>December
</select>\n<select name=day>\n";
for($i=1; $i<32; $i++){
echo "<option value=$i>$i</option>\n";
};
echo "</select>\n";
echo "<select name=year>\n";
for($i=1900; $i<1994; $i++){
echo "<option value=$i>$i</option>\n";
};
echo "</select>\n";
echo "<tr><td colspan=2><input type=submit value='".Register."'>&nbsp;<input type=reset value='".Reset."'>\n";
echo "<input type=hidden name=act value=register>\n";
echo "</form></table>\n";
}

if($act == register){
$username = $_POST[username];
$pass = $_POST[pass];
$pass2 = $_POST[pass2];
$email = $_POST[email];
$email2 = $_POST[email2];
$month = $_POST[month];
$day = $_POST[day];
$year = $_POST[year];



if(isset($username) && isset($pass) && isset($pass2) && isset($email) && isset($email2)){

if($pass != $pass2){
echo "<b>Your confirmation password is not the same as your password!</b><br>";
}

if($email != $email2){
echo "<b>Your confirmation email is no the same as your email!</b><br>";
}
}else {
echo "Please fill in all the fiedls!";
}
}
?>
<?php include('footer.php'); ?>
[/code]
Link to comment
Share on other sites

Try this:
[code]<?php

if(isset($_POST['act']) && $_POST['act'] == 'register')
{
    $errors = '';

    foreach($_POST as $key => $value)
    {
        if($key == 'pass' || $key == 'email')
        {
            $k = $key . '2';

            if(empty($_POST[$key]) || $_POST[$key] != $_POST[$k])
            {
                $field = (($key == 'pass') ? 'passwords' : 'email addresses');

                $errors[] = 'The ' . $field . ' do not match';
            }
        }
        elseif(isset($_POST[$key]) && empty($_POST[$key]))
        {
            if($key != 'pass2' && $key != 'email2')
            {
                $errors[] = 'Please fill in the ' . $key . " field<br />\n";
            }
        }
    }

    if(is_array($errors))
    {
        echo "Unable to submit the form, due to the following errors:<br />
<ul>\n";

        foreach($errors as $error)
        {
            echo '  <li>' . $error . "</li>\n";
        }

        echo "</ul>\n";

    }
}

echo <<<HTML
<form name="reg" method="post" action="{$_SERVER['PHP_SELF']}">
  <table border="0" cellspacing="3" cellpadding="2">
    <tr>
      <td class="gbr">Username:</td>
      <td class="gbr"><input type="text" name="username"></td>
    </tr>
    <tr>
      <td class="gbr">Password:</td>
      <td class="gbr"><input type="password" name="pass"></td>
    </tr>
    <tr>
      <td class="gbr"><i>Confirm:</i></td>
      <td class="gbr"><input type="password" name="pass2"></td>
    </tr>
    <tr>
      <td class="gbr">Email:</td>
      <td class="gbr"><input type="text" name="email"></td>
    </tr>
    <tr>
      <td class="gbr"><i>Confirm:</i></td>
      <td class="gbr"><input type="text" name="email2"></td>
    </tr>
    <tr>
      <td class="gbr">Birthday:</td>
      <td class="gbr">
        <select name="month">
          <option value="January">January</option>
          <option value="February">February</option>
          <option value="March">March</option>
          <option value="April">April</option>
          <option value="May">May</option>
          <option value="June">June</option>
          <option value="July">July</option>
          <option value="August">August</option>
          <option value="September">September</option>
          <option value="October">October</option>
          <option value="November">November</option>
          <option value="December">December</option>
        </select>
        <select name="day">

HTML;

for($i = 1; $i < 32; $i++)
    {
    echo '        <option value="' . $i . '">' . $i . "</option>\n  ";
}

echo "        </select>\n";
echo "        <select name=\"year\">\n";

for($i = 1900; $i < 1994; $i++)
    {
    echo '          <option value="' . $i . '">' . $i . "</option>\n";
}

echo <<<HTML
        </select>
      </td>
    </tr>
    <tr>
      <td colspan="2">
        <input type="submit" value="Register">&nbsp;<input type="reset" value="Reset">
        <input type="hidden" name="act" value="register">
      </td>
    </tr>
  </table>
</form>
HTML;

?>[/code]
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.