We help IT Professionals succeed at work.
Get Started

PHP and jQuery

Robert Granlund
Last Modified: 2016-08-09
I have a form that when submitted, if it is successful I want to show a certain div and hide another div.  It works on success but then reverts back to the original once the page resets.  I'm not sure how to do this.  Where I am trying to implement the jQuery is on line: 43 - 51

                if (isset($_POST['submitted'])) { // Handle the form.
                    // Trim all the incoming data:
                    $trimmed = array_map('trim', $_POST);

                    $errors = array(); // Initialize an error array.
                    // Check for a first name:
                    if (preg_match('/^[A-Z \'.-]{2,20}$/i', $trimmed['full_name'])) {
                        $fn = mysqli_real_escape_string($dbc, $trimmed['full_name']);
                    } else {
                        $errors[] = 'You forgot to enter your name.';

                    // Check for an email address:
                    if (preg_match('/^[\w.-]+@[\w.-]+\.[A-Za-z]{2,6}$/', $trimmed['email'])) {
                        $e = mysqli_real_escape_string($dbc, $trimmed['email']);
                    } else {
                        $errors[] = 'You forgot to enter a valid email address.';

                    if (empty($errors)) {
                        // Make sure the email address is available:
                        $q = "SELECT user_id FROM jmc_cal_users WHERE email='$e'";
                        $r = mysqli_query($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));

                        if (mysqli_num_rows($r) == 0) { // Available.
                            // Create the activation code:
                            $a = md5(uniqid(rand(), true));

                            // Add the user to the database:
                            $q = "INSERT INTO jmc_cal_users (email, full_name, active, registration_date) VALUES ('$e', '$fn', '$a', NOW() )";
                            $r = mysqli_query($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));

                            if (mysqli_affected_rows($dbc) == 1) { // If it ran OK.
                                // Send the email:
                                $body = "Thank you for registering your email  please click on this link:\n\n";
                                $body .= BASE_URL . 'activate.php?x=' . urlencode($e) . "&y=$a";
                                mail($trimmed['email'], 'Email Confirmation', $body, 'From: admin@sitename.com');
                                    $(function () {
                                        $("#calc_submit").click(function () {

                            } else { // If it did not run OK.
                                echo '<p class="error">Your email could not be registered due to a system error. We apologize for any inconvenience.</p>';
                        } else { // The email address is not available.
                            echo '<p class="error">That email address has already been registered.</p>';
                    } else { // Report the errors.
                        echo '<h1>Error!</h1>
		<p class="error">The following error(s) occurred:<br />';
                        foreach ($errors as $msg) { // Print each error.
                            echo " - $msg<br />\n";
                        echo '</p><p>Please try again.</p><p><br /></p>';
                    } // End of if (empty($errors)) IF.
                } // End of the main Submit conditional.

 <form action="?" method="post">
                    <div class="calculator-submit">
                            <input type="text" name="full_name" size="25" maxlength="120" value="<?php if (isset($trimmed['full_name'])) echo $trimmed['full_name']; ?>" placeholder="Name"/>
                            <br />
                            <input type="email" name="email" size="25" maxlength="65" value="<?php if (isset($trimmed['email'])) echo $trimmed['email']; ?>" placeholder="Email"/>

                    <div class="submit-cont">
                        <input type="submit" name="submit" id="calc_submit" value="Submit" />
                        <input type="hidden" name="submitted" value="TRUE" />

                    <!--  END Calculator Submit Form  -->

                    <div class="calculator-statement" style="display:none;">
                        <h2>Check your email</h2><br />


Open in new window

Watch Question
This problem has been solved!
Unlock 1 Answer and 12 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE