Jump to content

Recommended Posts

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.

[code]
<?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');
?>
[/code]
Link to comment
https://forums.phpfreaks.com/topic/9487-broswer-ignores-form/
Share on other sites

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.
Link to comment
https://forums.phpfreaks.com/topic/9487-broswer-ignores-form/#findComment-34992
Share on other sites

[!--quoteo(post=373002:date=May 10 2006, 01:49 PM:name=ober)--][div class=\'quotetop\']QUOTE(ober @ May 10 2006, 01:49 PM) [snapback]373002[/snapback][/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).
Link to comment
https://forums.phpfreaks.com/topic/9487-broswer-ignores-form/#findComment-34994
Share on other sites

[!--quoteo(post=373010:date=May 10 2006, 01:57 PM:name=ober)--][div class=\'quotetop\']QUOTE(ober @ May 10 2006, 01:57 PM) [snapback]373010[/snapback][/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.
Link to comment
https://forums.phpfreaks.com/topic/9487-broswer-ignores-form/#findComment-35002
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.