简体   繁体   中英

After I hit Submit on my PHP page nothing happens. The data should import into my php database

I created this signup page. The problem is when I click submit after I enter the information nothing happens. It just refreshes the same page. The info I enter should import into my database after I hit submit and display a thank you for signing up message after the submission. Please help. I'm trying to keep everything to single page by implementing the html and php code all on one page instead of 2 separate files.

<html>

    <body>

        <?php

             $output_form = true; //declare a FLAG we can use to test whether or not to show form

             $first_name = NULL;
             $last_name = NULL;
             $email = NULL;

             if (isset($_POST['submit']) ) { //conditional processing based on whether or  not the user has submitted.

             $dbc = mysqli_connect('localhost', 'name', 'pswd', 'database')
or die('Error connecting to MySQL server.');

             $first_name = mysqli_real_escape_string($dbc, trim($_POST['firstname']));
             $last_name = mysqli_real_escape_string($dbc, trim($_POST['lastname']));
             $email = mysqli_real_escape_string($dbc, trim($_POST['email']));


             $output_form = false; // will only change to TRUE based on validation

             //Validate all form fields 
             if (empty($first_name)) {

             echo "WAIT - The First Name field is blank <br />";
             $output_form = true; // will print form.
             }

             if (empty($last_name)) {

             echo "WAIT - The Last Name field is blank <br />";
             $output_form = true; // will print form.
             }

             if (empty($email)) {

             echo "WAIT - The Email field is blank <br />";
             $output_form = true; // will print form.
             }


            if ((!empty($first_name)) && (!empty($last_name)) && (!empty($email))) {
            //End of form validation


            //This section establishes a connection to the mysqli database, and if it fails display error message

           $query = "INSERT INTO quotes (first_name, last_name, email, " .
           "VALUES ('$first_name', '$last_name', '$email')";

           $result = mysqli_query($dbc, $query)
           or die('Error querying database.');

           mysqli_close($dbc);

           $to = 'email@email.com';
           $subject = 'New Customer';
           $msg = "$first_name $last_name\n" .
           "Email: $email\n";
           $mail =  mail($to, $subject, $msg, 'From:' . $email);

           if($mail){
           header("Location: https://www.locate.com/blue.php".$first_name);
           exit();
            }
          //Display the user input in an confirmation page  

         echo "<body style='margin-top: 100px; background-color: #f2f0e6;'><p style = 'color: #000000; text-align: center;font-size:300%; font-family:Arial, Helvetica, sans-serif;'><strong>Thanks for signing up!</strong></p><center><p style = 'color: #000000; text-align: center;font-size:200%; font-family:Arial, Helvetica, sans-serif;'>Contact us for any questions
        </p>
        </center>
        </body>";

       }//end of validated data and adding recored to databse. Closes the code to send the form.

       } //end of isset condition. This closes the isset and tells us if the form was submitted.

       else { //if the form has never been submitted, then show it anyway
       $output_form = true;
       }

       if ( $output_form ) { //we will only show the form if the user has error OR not submitted.

       ?>
       <div id="box">
       <center><img src="../../images/duck.jpg" class="sign-up" alt="Sign Up"></center>
       <br>
       <p>Sign Up to get Discount Code</p><br>
       <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?> ">
       <div> 
       <label for="firstname">First name:</label>
       <input type="text" id="firstname" name="firstname" size="37" maxlength="37" value="     <?php echo $first_name; ?>" />
       </div>
       <div>
       <label for="lastname">Last name:</label>
       <input type="text" id="lastname" name="lastname" size="37" maxlength="37" value="<?php echo $last_name; ?>" />
       </div>
       <div>
       <label for="email">Email:</label>
       <input type="text" id="email" name="email" size="37" maxlength="37" value="<?php echo $email; ?>" />
       </div>
       <div id="submit">
       <input type="submit" name="Submit" value="Submit" />
       </div>
       </center>
       </form>
       </div>
       <?php  
       }
      ?>
     </body>

You are asking for $_POST['submit'] instead of $_POST['Submit']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM