简体   繁体   中英

PHP Contact Form Issue (redirecting)

I am trying to set my PHP contact formso that when you click "SEND" it says thank you and redirects you to the home page. This is my script for the php and html. I know its just a few lines need adjusting, but not sure exactly how. I uploaded the mail.php file into my website directory.

  <?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$call = $_POST['call'];
$website = $_POST['website'];
$priority = $_POST['priority'];
$type = $_POST['type'];
$message = $_POST['message'];
$formcontent=" From: $name \n Phone: $phone \n Call Back: $call \n Website: $website \n Priority: $priority \n Type: $type \n Message: $message";
$recipient = "horgantm@gmail.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
if(mail($recipient, $subject, $formcontent, $mailheader))

    { 
        header("Location: http://www.darthvixcustomsabers.com/index.html");
        exit;   
    }
    else
    {
        die("Error!");
    }
echo "Thank You!" . " -" . "<a href='index.html' style='text-decoration:none;color:#ff0099;'> Return Home</a>";

?>


<!doctype html>
<html>
<head>
<title> DV Custom Sabers </title>
<meta charset="utf-8">
<link type="text/css" rel="stylesheet" href="style/kotorsale.css" />
<meta name="viewport" content="width=device-width" />


</head>
<div class="header"><a href="index.html">Darthvix Custom Sabers</a></div>

<div class="header1">DV Custom Sabers is the place to go for your saber needs!</div>

<div class="logo"><a href="index.html"><img src="images/logo.png" alt="schedule" height="200" width="350" border="0"></a></div>

<ul id="nav">
    <li><a href="index.html">Home</a></li>
    <li><a href="aboutme.html">About Me</a></li>
    <li><a href="services.html">Services</a></li>
    <li><a href="Gallery.html">Gallery</a></li>
    <li><a href="kotorsale.html">For Sale</a></li>
    <li><a href="buildlog.html">Build Log</a></li>
    <li><a href="contact.html"> Contact Us</a></li>
</ul>
<div id="form">
<<form action="mail.php" method="POST">

<p>Name</p> <input type="text" name="name">

<p>Email</p> <input type="text" name="email">


<p>Priority</p>

<select name="priority" size="1">

<option value="Low">Low</option>

<option value="Normal">Normal</option>

<option value="High">High</option>

<option value="Emergency">Emergency</option>

</select>

<br />

<p>Message</p><textarea name="message" rows="6" cols="25"></textarea><br />

<input type="submit" value="Send"><input type="reset" value="Clear">

</form>

</div>
</body>
</html>

What exactly is not working or do you need help with? Please be more specific.

That being said, I see a few issues right off the bat.

Your main issue is that this line is not valid, which is probably causing the entire script to fail:

$recipient = "horgantm@gmail.com<script type="text/javascript"> ...

You must escape your double quotes or use single quotes instead. (See how the highlighting changes?)

You also have an extra bracket in this line:

<<form action="mail.php" method="POST">

Why is there a script embedded in the recipient field anyway?

EDIT: OP changed code after my answer was posted.

The problem might be that you are not redirecting anybody.

I would edit your code like this

if(mail($recipient, $subject, $formcontent, $mailheader))
    {
        header("Location: http://www.example.com/home.php?successfulySent");
        exit;   
    }
    else
    {
        die("Error!");
    }

And on the page, where you want a user to be redirected

if(isset($_GET['successfulySent']))
        echo "Thank you for sendinf a message!";

EDIT:

I do not want us to be misunderstood.

I created this condition, since you wrote a statement …and redirects you to the home page.… regarding redirecting and you did not have it in your code.

You edited you code in the question, but you have forgotten to remove the last line ( echo "Thank You!" . " -" .… ), it is now useess, since the script either redirects a user or stops with a message Error!

Then a URL parameter ?successfulySent is important for the script, where you are redirecting, to know, that this is something, that it should do.

So change Location url to http://www.darthvixcustomsabers.com/index.php?successfulySent .

And I wrote php extension, yes, you need to have this file as PHP file type, since you are placing there a PHP condition. In most cases, I would only rename index.html into index.php

Where to place the second condition, simply, there, where you want a message to appear.

<?php if(isset($_GET['successfulySent'])):?>
    <div class="message" id="message-sent">
        <p>Your message has been successfuly sent to our team...</p>
    </div>
<?php endif;?>

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