Jump to content

Using exit(); after processing a form...


webmaster1

Recommended Posts

Hi!

 

I have a basic contact form which validates itself and writes to a database when submitted.

 

I want to display a message (Thank you. Message has been sent.) which would replace the form.

 

I've tried using:

exit();

but this replaces the entire page.

 

Does anyone know where I'm going wrong?

(I can post more code if required)

Link to comment
https://forums.phpfreaks.com/topic/151675-using-exit-after-processing-a-form/
Share on other sites


<?php
if (isset($_POST['submit']))
{

//VALIDATION

     if (strlen($_POST['yourname']) > 0)
     {$yourname=TRUE;}
     else 	{$yourname=FALSE;
     $message_yourname="<span style='font-family:Arial, Helvetica, sans-serif;font-size:12px;color:#FF0000;font-weight:bold;'> *You forgot to enter your name!</span>";}

     if (strlen($_POST['youremail']) > 0)
     {$youremail=TRUE;}
     else 	{$youremail=FALSE;
     $message_youremail="<span style='font-family:Arial, Helvetica, sans-serif;font-size:12px;color:#FF0000;font-weight:bold;'> *You forgot to enter your email!</span>";}
     
     if (strlen($_POST['subject']) > 0)
     {$subject=TRUE;}
     else 	{$subject=FALSE;
     $message_subject="<span style='font-family:Arial, Helvetica, sans-serif;font-size:12px;color:#FF0000;font-weight:bold;'> *You forgot to enter a subject!</span>";}

     if (isset($_POST['yourmessage'])) 
        {
          $yourmessage = trim($_POST['yourmessage']);
          if ($yourmessage == "" || empty($yourmessage)) 
            {
            $message_yourmessage = "<span style='font-family:Arial, Helvetica, sans-serif;font-size:12px;color:#FF0000;font-weight:bold;'> *You forgot to enter a message!</span>";
            }
        }

if 
      (
      $yourname 
      && $youremail
      && $subject 
      && $yourmessage 
      )
      {

//DEFINE VARIABLES FOR SQL

                  include("db_info.php");
          mysql_connect(localhost,$username,$password);
          @mysql_select_db($database) or die( "Unable to establish a connection to the relevant database.");

$ipaddress = getenv('REMOTE_ADDR');
$yourname = mysql_real_escape_string($_POST['yourname']);
$youremail = mysql_real_escape_string($_POST['youremail']);
$recipients = mysql_real_escape_string($_POST['recipients']);
$subject = mysql_real_escape_string($_POST['subject']);
$yourmessage = mysql_real_escape_string($_POST['yourmessage']);

$query = "INSERT INTO contactform VALUES ('',NOW(),'$ipaddress','$yourname','$youremail','$recipients','$subject','$yourmessage')";
mysql_query($query);

     
     echo "Thank you for contacting us!";
     exit();
     //END THE IF VARIABLE = TRUE CONDITION
     }


}
?>

 

The above code shows how the form validates itself and then writes to a database. With each succesful submit I want a message to be shown in place of the form.

 

The above code actually replaces the entire page.

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.