Domanda

I'm still very new to php so this has been a struggle for me. I now need to make it so that a message appears where the form was, saying thank you.

this is the link: www.pamper-pad.co.uk/contact.php code is as follows:

PHP:

<?php

if(isset($_POST['email'])) {



// EDIT THE 2 LINES BELOW AS REQUIRED

$email_to = "joxxorz@me.com";

$email_subject = "Enquiry for Pamper Pad";




$first_name = $_POST['name']; // required

$email_from = $_POST['email']; // required

$comments = $_POST['comments']; // required



$email_message = "Form details below.\n\n";



function clean_string($string) {

  $bad = array("content-type","bcc:","to:","cc:","href");

  return str_replace($bad,"",$string);

}



$email_message .= "Name: ".clean_string($first_name)."\n";

$email_message .= "Email: ".clean_string($email_from)."\n";

$email_message .= "Comments: ".clean_string($comments)."\n";





// create email headers

$headers = 'From: '.$email_from."\r\n".

'Reply-To: '.$email_from."\r\n" .

'X-Mailer: PHP/' . phpversion();

@mail($email_to, $email_subject, $email_message, $headers);  

?>

<?php

}

?>

HTML:

<form name="contactform" method="post" style="font-size:14px; height:35px;">

<table width="250px">

<tr>

<td height="25" valign="top">

<label for="first_name" style="height:35px;"><span class="boldp">Name <span class="asterisk">*</span></span></label>

</td>

</tr>

<tr>
<td height="50" valign="top">

<input class="formbox" type="text" name="name" maxlength="50" size="30">

</td>
</tr>




<tr>

<td height="25" valign="top">

<label for="email" style="height:35px;"><span class="boldp">Telephone Number/Email Address <span class="asterisk">*</span></span></label>

</td>

</tr>

<tr>

<td height="50" valign="top">

<input class="formbox" type="text" name="email" maxlength="80" size="30">

</td>

</tr>


<tr>

<td height="25" valign="top">

<label for="comments"><span class="boldp">Message <span class="asterisk">*</span></span></label>

</td>

</tr>

<tr>

 <td height="119" valign="top">

<textarea  class="formbox" name="comments" maxlength="1000" cols="38" rows="6"></textarea>

</td>

</tr>

<tr>

<td height="24" colspan="2" style="text-align:right">

<input  class="submit" type="submit" value="Submit">

</td>

</tr>

</table>

</form>

Can anyone please advise how I would replace this form with a message?

EDIT:

<?php
if (!isset($_POST['email'])){
//code to display your form here
//for example
echo "<div class="formthing">
<form name="contactform" method="post" style="font-size:14px; height:35px;">

<table width="250px">

<tr>

<td height="25" valign="top">

<label for="first_name" style="height:35px;"><span class="boldp">Name <span class="asterisk">*</span></span></label>

</td>

</tr>

<tr>
<td height="50" valign="top">

<input class="formbox" type="text" name="name" maxlength="50" size="30">

</td>
</tr>




<tr>

<td height="25" valign="top">

<label for="email" style="height:35px;"><span class="boldp">Telephone Number/Email Address <span class="asterisk">*</span></span></label>

</td>

</tr>

<tr>

<td height="50" valign="top">

<input class="formbox" type="text" name="email" maxlength="80" size="30">

</td>

</tr>


<tr>

<td height="25" valign="top">

<label for="comments"><span class="boldp">Message <span class="asterisk">*</span></span>            </label>

</td>

</tr>

<tr>

<td height="119" valign="top">

<textarea  class="formbox" name="comments" maxlength="1000" cols="38" rows="6"></textarea>

</td>

</tr>

<tr>

<td height="24" colspan="2" style="text-align:right">

<input  class="submit" type="submit" value="Submit">

</td>

</tr>

</table>

</form>
</div>";
}elseif(isset($_POST['email'])){
//code to display thank you message here
//for example
echo "<div>Thank you for submitting your email!</div>";
}

?>
È stato utile?

Soluzione

Put the form in a div and another div with a thank you message.

Then check

    <?php if(isset($_POST['name) && email etc){
    echo '<div class="thank you"> Thank you </div>}
    else
    {
    echo '<div class="divform"> form </div>
    }

?php>

Altri suggerimenti

You should always sanitize your post values.

For instance:

$email_from = $_POST['email']; // required

Should be:

$email_from = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL)

To further sanitize and check you can:

if ($_POST['email'] == $email_from && filter_var($_POST['email'],FILTER_VALIDATE_EMAIL)){
   $verified_email_from = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
}

Though @PeeHaa may be able to specify further.

And for these:

$first_name = $_POST['name']; // required
$comments = $_POST['comments']; // required

You can use:

$first_name = filter_var($_POST['name'], FILTER_SANITIZE_STRING)
$comments= filter_var($_POST['comments'], FILTER_SANITIZE_STRING)

You can read more on sanitizing at PHP.NET and there is a nice tutorial here

Aside that as @terisL has stated you can use the post var to display a different page depending on status. You would build each page in PHP and it would choose to display based on post status.

<?php

if (!isset($_POST['email'])){
    //code to display your form here
    //for example
echo "<form name='contactform' method='post' style='font-size:14px; height:35px;'>

<table width='250px'>

<tr>

<td height='25' valign='top'>

<label for='first_name' style='height:35px;'><span class='boldp'>Name <span class='asterisk'>*</span></span></label>

</td>

</tr>";
}elseif(isset($_POST['email'])){
    //code to display thank you message here
    //for example
    echo "<div>Thank you for submitting your email!</div>";
}

?>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top