troubleshooting Question

PHP and jQuery

Avatar of Robert Granlund
Robert GranlundFlag for United States of America asked on
PHPjQuery
12 Comments1 Solution215 ViewsLast Modified:
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

 <?php
                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');
                                ?>
                                <script>
                                    $(function () {
                                        $("#calc_submit").click(function () {

                                            $(".submit-cont").hide("slow");
                                            $(".calculator-statement").show("slow");
                                        });
                                    });
                                </script>
                                <?php
                                
                            } 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.
                    mysqli_close($dbc);
                } // End of the main Submit conditional.
                ?>


<html>
 <form action="?" method="post">
                    <div class="calculator-submit">
                        <p>
                            <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"/>
                        </p>
                        <p>

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

                    <!--  END Calculator Submit Form  -->

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

</html>
ASKER CERTIFIED SOLUTION
Member_2_248744

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 12 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 12 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros