简体   繁体   English

我的PHP部分显示在浏览器中

[英]Portion of my php is displaying in the browser

I am trying to add php to my website to send an email when the user clicks a submit button. 我正在尝试将php添加到我的网站,以在用户单击提交按钮时发送电子邮件。 For some reason, a section of my php is displaying in the browser and I can't figure out why. 由于某种原因,我的php的一部分显示在浏览器中,我不知道为什么。 I've looked up information and tried checking my quotes and things like that, but I don't see what would be causing a portion of it to display as text. 我已经查看了信息,并尝试检查我的引号和类似内容,但是我看不到是什么导致它的一部分显示为文本。

屏幕截图的PHP

<?php
    $action=$_REQUEST['action'];

    if ($action=="") /* display the contact form */
    {
?>
        <form id="Contact-Form" action="" method="POST" enctype="multipart/form-data">
            <div class="row">
                <label for="name">Your name:</label><br />
                    <input id="name" class="input" name="name" type="text" value="" size="30" /><br />
            </div>
            <div class="row">
                <label for="email">Your email:</label><br />
                    <input id="email" class="input" name="email" type="text" value="" size="30" /><br />
            </div>
            <div class="row">
                <label for="message">Your message:</label><br />
                    <textarea id="message" class="input" name="message" rows="7" cols="30"></textarea><br />
            </div>
            <input id="Submit-Button" type="submit" value="Send email" />
        </form> 
<?php
    } 
    else /* send the submitted data */
    {
        $name=$_REQUEST['name'];
        $email=$_REQUEST['email'];
        $message=$_REQUEST['message'];

        if (($name=="")||($email=="")||($message==""))
        {
            echo "All fields are required, please fill the form again.";
        }
        else
        {        
            $from="From: $name<$email>\r\n Return-path: $email";
            $subject="Message sent using your contact form";
            mail("email@gmail.com", $subject, $message, $from);
            echo "Email sent!";
        }
    }  
?>

To explain why this happens... 为了解释为什么会这样...

Notice how the portion of code shown is only the bit after <$email> in your code? 请注意,代码中显示的代码部分是如何仅在代码中<$email>之后的位? Specifically that > . 特别是>

When you open a PHP file in your browser without a server processing it first, most PHP code will be seen by the browser as just one big, invalid HTML tag and will not be shown, except in View Source and document inspector tools. 当您在浏览器中打开PHP文件而没有服务器先对其进行处理时,大多数PHP代码将被浏览器视为一个大的无效HTML标记,并且不会显示,除非在“查看源代码”和文档检查器工具中显示。 However, since your code happens to contain a > , this marks the end of the "invalid HTML tag" and anything after it will be seen in the page. 但是,由于您的代码恰好包含一个> ,所以这标志着“无效的HTML标记”的结尾,并且在它之后的所有内容都将在页面中看到。

To fix this, use a server! 要解决此问题,请使用服务器! PHP has one built-in as of 5.4, so if you have it installed on your machine you can open a command line in the root folder of your PHP and run: PHP自5.4起内置一个,因此,如果在计算机上安装了它,则可以在PHP的根文件夹中打开命令行并运行:

php -S localhost:8000

Then open a browser and go to http://localhost:8000/yourfile.php and it will run. 然后打开浏览器并转到http://localhost:8000/yourfile.php ,它将运行。 Nifty! 漂亮!

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

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