为什么我的消息没有发送textarea?

时间:2016-02-29 20:25:28

标签: php html

我正在尝试创建一个在线代码编辑器,当你完成后我想给用户一个选项,可以通过电子邮件将所有代码发送给自己但是每当我尝试访问网站时都不会发送代码。 <textarea></textarea>个标签。这是我的代码。如果您需要JavaScript和CSS,请告诉我。

<!DOCTYPE HTML>
<HTML lang = "en">
    <head>
        <meta charset = "UTF-8">
        <meta name = "author" content = "Adam S. Oates">
        <meta name = "description" content = "This HTML file was created to test new thigns I learn">
        <meta name = "title" content = "Online Code Editor">
        <title title = "Online Code Editor">
            Online Code Editor
        </title>
        <link rel = "apple-touch-icon" href = "">
        <link rel = "shortcut icon" href = "">
        <link rel = "stylesheet" type = "text/css" href = "main.css">
        <script type = "text/javascript" src = "main.js"></script>
    </head>
    <body>
        <header>
            Gigaboy <span id = "code"></span> Editor
        </header>

        <form action = "<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method = "post">
            <textarea autocomplete = "off" placeholder = "Type HTML Here" id = "HTMLeditor" name = "HTMLeditor"></textarea>
            <a id="CSS"></a>
            <textarea autocomplete = "off" placeholder = "Type CSS Here" id = "CSSeditor" name = "CSSeditor"></textarea>
            <a id="JavaScript"></a>
            <textarea autocomplete = "off" placeholder = "Type JavaScript Here" id = "JSeditor" name = "JSeditor"></textarea>
            <span id = "see-result">View Result</span>
            <select id = "jqueryMode">
                <option value = "OFF">Disable JQuery</option>
                <option value = "ON">Enable JQuery</option>
            </select>
            <select id = "bootstrapMode">
                <option value = "OFF">Disable Bootstrap</option>
                <option value = "ON">Enable Bootstrap</option>
            </select>
            <select id = "gigaboyMode">
                <option value = "OFF">Disable GigaboyStyle.js</option>
                <option value = "ON">Enable GigaboyStyle.js</option>
            </select>
            <span id = "emailCode">
                    <input type = "email" name = "email" placeholder = "Send Code to You Email">
                    <input type = "submit" value = "Send">
            </span>
        </form>
    </body>
</html>

<?PHP

    if (!empty($_POST)) {
        $headers = "MIME-Version: 1.0\r\n";
        $headers .= "Content-type:text/html;charset=UTF-8\r\n";
        $msg = str_replace("\n", "<br>", str_replace("\r\n", "<br>", $_POST['HTMLeditor']));
        $msg = str_replace("\n", "<br>", str_replace("\r\n", "<br>", $_POST['CSSeditor']));
        $msg = str_replace("\n", "<br>", str_replace("\r\n", "<br>", $_POST['JSeditor']));

        $HTML = htmlspecialchars($_POST['HTMLeditor']);
        $CSS = htmlspecialchars($_POST['CSSeditor']);
        $JS = htmlspecialchars($_POST['JSeditor']);
        $email = $_POST['email'];
        $subject = "Code Made at Gigaboy Code Editor Online";
        $message = "HTML5 Code: <br><br>" . $HTML . "<br><br><br>CSS Code: <br><br>" . $CSS . "<br><br><br>JavaScript Code: <br><br>" . $JS;

        mail($email, $subject, $message, $headers);
    }
?>

2 个答案:

答案 0 :(得分:3)

所有表单元素(包括文本区域)都需要放在您的开始和结束表单标签之间。

答案 1 :(得分:0)

这里有一些重大的问题......

首先,您的表单元素(inputselect等)实际上并不是形式的。他们需要在这些标签之间:

<form method = "post">
    <!-- form elements go in here -->
</form>

否则form只会发布它所知道的内容,目前只有input type="email"input type="submit",仅此而已。

其次,这不符合你的想法:

<input type = "submit" onclick = "<?PHP sendCode(); ?>" value = "Send">

这将在您的网页加载之前执行sendCode() 。而且,由于当时没有表格值,该功能不会做任何事情。然后这将产生这个标记:

<input type = "submit" onclick = "" value = "Send">

所以你基本上告诉提交按钮不要做任何事情(虽然不成功)。然后,当你提交表单时,会巧合再次执行该功能,但不会在你认为时执行。

在这种情况下,我能给出的最佳建议是从头开始学习一些PHP教程。在页面发送到客户端之前,PHP在服务器上运行。然后,在PHP完成后,客户端代码在浏览器中运行。他们不会混在一起。

要解决此案例中的问题,基本上您可以完全删除onclick

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

并完全删除函数表示法:

<?php

if (!empty($_POST)) {
    // your code here
}

?>

鉴于您正在做的事情的结构,这里不需要功能。在将页面发送到浏览器之前,每次请求页面时都会执行此代码。如果请求中没有包含任何POST数据,那么这个if语句已经检查了。