Jump to content


Photo

Broswer Ignores Form


  • Please log in to reply
4 replies to this topic

#1 infophiliac

infophiliac
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 10 May 2006 - 05:38 PM

I'm in the midst of trying to create a registration form and everything appears to be running beautifully on the registration page except for the little matter of the form not being displayed. When the page is loaded, it immediately goes to "Thanks, you're registered" without ever collecting the data to be submitted to MySQL. Here's the code and any insights would be appreciated.

<?php # Scrip 7.3 - register.php

$page_title = 'Register';
include ('./includes/header.html');
?>
<h2>Register</h2>
<form action="register.php" method="post">
    <p>First Name: <input type="text" name="first_name" size="15" maxlength="15" value="<?php if (isset($_POST['first_name'])) echo $_POST['first_name']; ?>" /></p>
    <p>Last Name: <input type="text" name="last_name" size="15" maxlength="30" value="<?php if (isset($_POST['last_name'])) echo $_POST['last_name']; ?>" /></p>
    <p>Email Address: <input type="text" name="email" size="20" maxlength="40" value="<?php if (isset($_POST['email'])) echo $_POST['email']; ?>" /></p>
<p>Password: <input type="password" name="password1" size="10" maxlength="20" /></p>
<p>Password: <input type="password" name="password2" size="10" maxlength="20" /></p>
<p><input type="submit" name="submitted" value="TRUE" />
</form>
<?php
// Check if the form has been submitted.
if (isset($_POST['submitted'])) {

    $errors = array(); // Initialize error array.

    // Check for a first name.
    if (empty($_POST['first_name'])) {
        $errors[] = 'You forgot to enter your first name.';
    } else {
        $fn = trim($_POST['first_name']);
}

    // Check for a last name.
    if (empty($_POST['last_name'])) {
        $errors[] = 'You forgot to enter your last name.';
    } else {
        $ln = trim($_POST['last_name']);
}

    // Check for a email address.
    if (empty($_POST['email'])) {
        $errors[] = 'You forgot to enter your email address.';
    } else {
        $e = trim($_POST['email']);
}

    // Check for a password and match against the confirmed password.
    if (!empty($_POST['password1'])) {
        if ($_POST['password1'] != $_POST['password2']) {
            $errors[] = 'Your password did not match the confirmed password.';
        } else {
            $p = trim($_POST['password1']);
        }
    } else {
        $errors[] = 'You forgot to enter your password.';
    }
    
}

    if (empty($errors)) { // If everything's okay.

        //Register the user in the database.
        require_once ('../mysql_connect.php'); // Connect to the db.

        // Make the query.
        $query = "INSERT INTO users (first_name, last_name, email, password, registration_date) VALUES ('$fn', '$ln', '$e', SHA1('$p'), NOW() )"; 
        $result = @mysql_query ($query);
        if ($result) { // If it ran OK.

            // Send an email, if desired.

        // Print a message.
        echo '<h1 id="mainhead">Thank you!</h1>
    <p>You are now registered.</p><p><br /></p>';

        // Include the footer and quite the script (to not show the form).
        include ('./includes/footer.html');
        exit();

    } else { // If it did not run OK.
        echo '<h1 id="mainhead">System Error!</h1>
        <p class="error">You could not be registered due to a system error.  We apologize for any inconvenience.</p>'; // Public message.
        echo '<p>' . mysql_error() . '<br /><br />Query: ' . $query . '</p> '; // Debugging message.
        include ('./includes/footer.html');
        exit();
}

        mysql_close(); // Close the database connection.

    } else { // Report the errors.

        echo '<h1 id="mainhead">Error!</h1>
        <p class="error">The following error(s) occurred:<br />';
        foreach ($errors as $msg) { // Print each error.
            echo " - $msg<br />\n";
    } // End of if (empty($errors)) IF.

} //  End of the main Submit conditional.

include ('./includes/footer.html');
?>


#2 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 10 May 2006 - 05:49 PM

Well, I'm not sure why you put your form up above the processing code, because if you hit errors, wouldn't you want the form to show again with the errors showing?

Other than that, I'm not sure what's going on. If you look at the source, is the code for the form there? If it is, I'd say you have a layout issue, not a PHP issue.

Info: PHP Manual


#3 infophiliac

infophiliac
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 10 May 2006 - 05:52 PM

[!--quoteo(post=373002:date=May 10 2006, 01:49 PM:name=ober)--][div class=\'quotetop\']QUOTE(ober @ May 10 2006, 01:49 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Well, I'm not sure why you put your form up above the processing code, because if you hit errors, wouldn't you want the form to show again with the errors showing?

Other than that, I'm not sure what's going on. If you look at the source, is the code for the form there? If it is, I'd say you have a layout issue, not a PHP issue.
[/quote]


Oops. That was initially at the end, but I tried moving it to the beginning when the form wasn't recognized to see if it would make any difference and forgot to move it back to the end. Incidentally, it didn't make a difference (although I'm sure that's more because whatever the basic issue is was preventing things from getting to the point where the location of the form mattered).

#4 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 10 May 2006 - 05:57 PM

Try removing the processing code and just loading the form to see if that works.

Info: PHP Manual


#5 infophiliac

infophiliac
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 10 May 2006 - 06:00 PM

[!--quoteo(post=373010:date=May 10 2006, 01:57 PM:name=ober)--][div class=\'quotetop\']QUOTE(ober @ May 10 2006, 01:57 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Try removing the processing code and just loading the form to see if that works.
[/quote]

The page still loads, but it still goes directly to the "Thanks, you're registered" state.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users