简体   繁体   English

PHP联系表格问题(重定向)

[英]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. 我正在尝试设置我的PHP联系人表单,当您单击“发送”时,它会说“谢谢”并将您重定向到主页。 This is my script for the php and html. 这是我的php和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. 我将mail.php文件上传到我的网站目录中。

  <?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. 编辑:我的答案发布后,OP更改了代码。

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. 我创建了这个条件,因为您写了一条语句…and redirects you to the home page.…关于重定向,但是您的代码中没有。

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! 您已经在问题中编辑了代码,但是忘记了删除最后一行( echo "Thank You!" . " -" .… ),它现在已被使用,因为脚本要么重定向用户,要么显示Error!消息停止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. 然后,URL参数?successfulySent对脚本很重要,在该脚本中您要重定向,以了解这是应该执行的操作。

So change Location url to http://www.darthvixcustomsabers.com/index.php?successfulySent . 因此,将Location网址更改为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. 我写了php扩展名,是的,您需要将此文件作为PHP文件类型,因为您要在其中放置一个PHP条件。 In most cases, I would only rename index.html into index.php 在大多数情况下,我只会将index.html重命名为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;?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM