我的网络托管公司说PHP代码有问题 [英] My web hosting company says there is something wrong with PHP code

查看:96
本文介绍了我的网络托管公司说PHP代码有问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是PHP假设连接到一个contact.html页面,然后在之后的感谢页面。我只是想要一个联系表单。它不会识别文件为.php.I确实保存它。

 <?php 
$ name = $ _POST ['name'];
$ email = $ _POST ['email'];
$ number = $ _POST ['number'];
$ message = $ _POST ['message'];
$ from ='From:you';
$ to ='me@hotmail.com';
$ subject ='Hello';
$ human = $ _POST ['human'];

$ body =From:$ name\\\
E-Mail:$ email\\\
Number:$ number\\\
Message:\\\
$ message;

$ b if($ _POST ['submit']){
if($ name!=''&& $ email!=''){
if($ human =='4'){
if(mail($ to,$ subject,$ body,$ from)){
echo'< p>您的留言已发送! < / p为H.';
} else {
echo'< p>出错了,请返回并重试!< / p>';

} else if($ _POST ['submit']&& $ human!='4'){
echo'< p>您回答了反垃圾邮件问题!错误< / p为H.';
}
} else {
echo'< p>您需要填写所有必填字段!!< / p>';
}

header(Location:thanks.html);

}

?>


解决方案

假设您希望从表单运行这个,你需要设置你的HTML表单标签,如下所示:

 < form action =contact.phpmethod =post > 

然后,您应该将 contact.html 重命名为 contact.php (任何文本编辑器都应该可以轻松完成)。

最后,您正在使用PHP的 header()函数,如果输出到浏览器被调用之前。这包括使用PHP的 echo 结构。您的 contact.php 文件应该如下所示(并且与包含表单的HTML文件位于同一目录中):

 <?php 
$ name = $ _POST ['name'];
$ email = $ _POST ['email'];
$ number = $ _POST ['number'];
$ message = $ _POST ['message'];
$ from ='From:you';
$ to ='me@hotmail.com';
$ subject ='Hello';
$ human = $ _POST ['human'];

$ body =From:$ name\\\
E-Mail:$ email\\\
Number:$ number\\\
Message:\\\
$ message;

$ b $ if($ _POST ['submit']){
if($ name!=''&& $ email!='')
{
if($ human =='4')
{
if(mail($ to,$ subject,$ body,$ from))
{
标题(Location:thanks.html);
}
else
{
echo'< p>出错了,请返回并重试!< / p>';
}
}
else
{
echo'< p>您错误地回答了反垃圾邮件问题!< / p>';
}
}
else
{
echo'< p>您需要填写所有必填字段!!< / p>';
}
}
?>

注意:我修改了一些布局并更改了一些条件你正在使用。第一个 elseif 实际上是多余的,并且 else 就足够了。


This is the PHP it suppose to connect to a contact.html page and then a thank you page after afterwards.I just wanted a contact form. it wont recognize files as .php.I did save it like that.

<?php
    $name = $_POST['name'];
    $email = $_POST['email'];
    $number = $_POST['number'];
    $message = $_POST['message'];
    $from = 'From:you'; 
    $to = 'me@hotmail.com'; 
    $subject = 'Hello';
    $human = $_POST['human'];

    $body = "From: $name\n E-Mail: $email\n Number: $number\n Message:\n $message";


if ($_POST['submit']) {
    if ($name != '' && $email != '') {
        if ($human == '4') {                 
            if (mail ($to, $subject, $body, $from)) { 
            echo '<p>Your message has been sent!</p>';
        } else { 
            echo '<p>Something went wrong, go back and try again!</p>'; 
        } 
    } else if ($_POST['submit'] && $human != '4') {
        echo '<p>You answered the anti-spam question incorrectly!</p>';
    }
    } else {
        echo '<p>You need to fill in all required fields!!</p>';
    }

  header("Location: thanks.html");

}

?>

解决方案

Assuming that you wish to run this from a form, you will need to set your HTML form tag as follows:

<form action="contact.php" method="post">

You should then rename contact.html to contact.php (any text editor should be able to do this easily).

Finally, you're using PHP's header() function, which will cause errors if you have output to the browser before it is called. This includes using PHP's echo struct. Your contact.php file should look like this (and be in the same directory as your HTML file containing the form):

<?php
    $name = $_POST['name'];
    $email = $_POST['email'];
    $number = $_POST['number'];
    $message = $_POST['message'];
    $from = 'From:you'; 
    $to = 'me@hotmail.com'; 
    $subject = 'Hello';
    $human = $_POST['human'];

    $body = "From: $name\n E-Mail: $email\n Number: $number\n Message:\n $message";


if ($_POST['submit']) {
    if ($name != '' && $email != '') 
    {
        if ($human == '4') 
        {                 
            if (mail ($to, $subject, $body, $from)) 
            { 
                header("Location: thanks.html");
            } 
            else 
            { 
                echo '<p>Something went wrong, go back and try again!</p>'; 
            } 
        } 
        else
        {
            echo '<p>You answered the anti-spam question incorrectly!</p>';
        }
    } 
    else 
    {
        echo '<p>You need to fill in all required fields!!</p>';
    }
}
?>

Note: I fixed your layout a little and changed some of the conditions that you were using. The first elseif was actually redundant, and an else will suffice.

这篇关于我的网络托管公司说PHP代码有问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆