简体   繁体   中英

Show hide part of php page

I have a php page. In the bottom there is a contact form. What I want to do is to hide the contact form when the mail is sent and instead show a thank you part. Means that when customer comes first time to the page the form show up. After submit the thank you part show up.

I have seen it done but have no clue how.

I have an idea that when the page loads it must check a variable to see if the mail was sent, but perhaps this is wrong.

When I need stuff like this, I usually name the submit button "submit" and checks if it is set...

<input type="submit" name="submit" value="Send form">

And the php (Use $_GET or $_POST accordingly)...

<? 
  if(isset($_POST['submit']))
  { 
   // Show "Thank you"
  }
  else
  { 
    // Show form
  }
?>

When I do things like this I tend to add an button to my form and give it a name

<button type="submit" id="submit" name="submit">Submit</button>

Then in my PHP check if the form has been submitted

<?php
if(isset($_POST['submit'])) {
  // Actions After Submit
}
else
{
 // Load The Form
}
?>

It's rather quite simple; add exit("Message..."); after mail()

In your PHP where the mail(...) is located, you would do the following:

mail($to,$subject,$message,$headers);
    exit("Thank you, your message has been sent. <a href='home.php'>Click here</a> to return to our Website.");

Here is a complete basic solution using pure PHP, since no code has been provided.

The following is meant to be run as everything inside the same file.

<?php

   if(isset($_POST['send'])) {
   // Prepare the email
   $to = 'email@example.com';

$mail_from = $_POST['email'];
   $subject = 'Message sent from website';
   $message = $_POST['message'];

$name = $_POST['name'];
$header = "From: $name <$mail_from>";

   // Send it
   $sent = mail($to, $subject, $message, $header);
   if($sent) {

    exit("Thank you, your message has been sent. <a href='home.php'>Click here</a> to return to our Website.");

   } else {
   echo 'Sorry, your message could not be sent. The error has been logged.';
   }
   } // closing brace for if(isset($_POST['send']))
?>

<form method="post" action="">
    <p>
        <label for="name">Name</label>
        <input type="text" id="name" name="name" />
    </p>
    <p>
        <label for="email">Email</label>
        <input type="text" id="email" name="email" />
    </p>
    <p>
        <label for="message">Message</label>
        <textarea id="message" name="message" rows="6" cols="30"></textarea>
    </p>
    <p>
        <input type="submit" name="send" value="Send message" />
    </p>
</form>

It's very simple, just if and else,sample form is below

<?php
if(isset($_POST['submit'])) { ?>
//write your other actions on form data
<h2>THANK YOU</h2>
<?php
} else { ?>
<form name="login" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<label><h4>Username:</label>
<input type="text" name="username">
<input type="submit" name="submit" value="submit"/>
</form>
<?php }
?>

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