开发者

Sending mail from html through php will return to php page. It should be in the same contact form. How to do it?

I have a html page with all images and other text. Below i have the contact form. i am new to php. I have used php to send mail. But when i click on send button,it is going to backend.php. And i have a blank page. What i need is it should in the same page. I have done some javascript to show and hide div's. With this i am showing a thank u msg in the same page. But i dont want to redirect browser to show it is going to backend.php.TO be in the same page i have redirected php to another window.

My code is - backend.php

<?php
if($_POST['message']) 
{
    $body = "Name: ".$_POST['name'];
    $body .= "<br>Email: ".$_POST['email'];
    $body .= "<br>Phone/Mobile: ".$_POST['phone']
    $body .= "<br>Address: ".$_POST['addre开发者_Python百科ss']
    $body .= "<br>Message: ".$_POST['msg']
    if(mail("Info@travelfoodservices.com","Subjects",$body))
        echo 'true';
    else echo 'false;';
}
?>

form code -

<form onsubmit="return validateFormOnSubmit(this)"  action="backend.php" method="post" target="_blank">
<table align="left">
<tbody>
  <tr>
<td align="left"><label for="name">Enter your name:</label> <label style="color:red;display:none;font-size:18px;" Id="namestar">*</label></td>    

</tr>
<tr><td align="left"><input Id="name" name="name" style="width:515px;" size="35" maxlength="50" type="text"></td></tr>
<tr ><td height="12px"/></tr>
<tr>
<td align="left"><label for="email">Email Address:</label> <label style="color:red;display:none;font-size:18px;" Id="emailstar">*</label></td>    
</tr>
<tr><td align="left"><input Id="email" name="email" style="width:515px;"  type="text"></td></tr>
<tr><td height="12px"/></tr>
<tr>
<td align="left"><label for="phone">Phone/Mobile:</label><label style="color:red;font-size:18px;display:none;" Id="phonestar">*</label></td>    
</tr>
<tr><td align="left"><input Id="phone" name="phone" style="width:515px;"  type="text"></td></tr>

<tr><td height="12px"/></tr>
<tr>
<td align="left"><label for="address">Your Address:</label><label style="color:red;font-size:18px;display:none;" Id="addressstar">*</label></td>    
</tr>
<tr><td align="left"><input id="address" name="address" style="width:515px;"  type="text"></td></tr>
<tr><td height="12px"/></tr>
<tr>
<td align="left"><label for="msg">Enter Your Comments / Suggestions / Enquiries</label><label style="color:red;font-size:18px;display:none;" Id="messagestar">*</label></td>    
</tr>
<tr><td align="left"><textarea id="msg" name="msg" style="width:515px;height:100px;"  type="text"></textarea></td></tr>
<tr><td height="12px"/></tr>
<tr>

<td align="right"><table align="left">
<tbody>
  <tr >
<td style="width:160px;"/>
<td style="width:250px;" ><label Id="mandatory" style="display:none;color:red;" >* Mandatory fields</label></td>
<td ><input   type="submit" name="send" value="send" /></td><td >
<input   type="reset" name="reset" value="reset" onclick="resetAll()" /></td></tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>


I would move the backend.php script to the top of the page that the form is in. To do this, the form script ("HTML page" as you referred to it) would need to be a .php page rather than a .html page. I'll assume here that you would call it form.php:

<?php
$submitted = false;

if($_POST['msg']) {
    $body = "Name: " . $_POST['name'] .
    "<br>Email: " . $_POST['email'] .
    "<br>Phone/Mobile: " . $_POST['phone'] .
    "<br>Address: " . $_POST['address'] .
    "<br>Message: " . $_POST['msg'];

    // Set the $submitted variable to true.  This will let you keep the PHP logic at the top of the page and simply check the value of the variable later to determine whether or not to show a success message.
    if(mail("Info@travelfoodservices.com","Subjects",$body)) $submitted = true;
}
?>


<?php
// Somewhere later on in the page, but above the form, show a success message if the $submitted variable has been set to true.
if($submitted) echo '<div>The form has been submitted!</div>';
?>

<form action="form.php" method="post">
    <!--All the HTML form code goes here-->
</form>


The easiest way to do this is redirecting the user back to the form, and supplying a GET parameter which indicates that the message is sent. Replace echo 'true'; with the following:

header('Location: contact.php?sent=true');
exit;

In your contact form, check if $_GET['sent'] is set and if so print your 'Message sent' message, like this:

<?php if(isset($_GET['sent'])): ?>
    <span>Your message is sent, thank you</span>
<?php endif; ?>

This is really simple and involves no JavaScript.

If you want to do it asynchronously (without reloading the page), you could use jQuery and $.post to post your data.


move the code from backednd.php to the file with the form (make sure page parses php), make sure you only run when the form is posted, change the form action to the save page.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜