【问题标题】:Form on CSS + output to .txt – problem with calling a PHP functionCSS 上的表单 + 输出到 .txt – 调用 PHP 函数的问题
【发布时间】:2019-03-12 14:30:15
【问题描述】:

我在 CSS + 部分有一个表单,可以将文本字段中的数据发送到位于服务器的文件 data.txt
每当我点击“提交”按钮时,什么都没有发生。 我认为这是调用 PHP 脚本的一个明显问题

if(isset($_POST['textdata']))
{
    $data=$_POST['textdata'];
    $fp = fopen('data.txt', 'a');
    fwrite($fp, $data);
    fclose($fp);
}
?>
<!DOCTYPE html>
<html lang="en" >
<head>
    <meta charset="UTF-8">
    <title>Pure CSS Steps</title>
    <link rel="stylesheet" href="css/style.css">
</head>

<body>

    <header>
        <h1>Pure <strong>CSS</strong> Steps</h1>
        <p>... a sassy "Step By Step" process.</p>
    </header>

    <section>
        <article>

            <form method="post" class="pure-steps" >
                <input type="radio" name="steps" class="pure-steps_radio" id="step-0" checked="">
                <input type="radio" name="steps" class="pure-steps_radio" id="step-1">
                <input type="radio" name="steps" class="pure-steps_radio" id="step-2">

                <div class="pure-steps_group">
                    <ol>
                        <li class="pure-steps_group-step">
                            <header>
                                <h2 class="pure-steps_group-step_legend">Welcome</h2>
                                <p class="pure-steps_group-step_item">You are about to form the form.</p>
                                <p class="pure-steps_group-step_item"></p>
                            </header>
                        </li>
                        <li class="pure-steps_group-step">
                            <fieldset>
                                <legend class="pure-steps_group-step_legend">Grab a tour</legend>

                                <p class="pure-steps_group-step_item flexy-item flexy-column reverse">
                                    <input name="textdata" type="text" placeholder="Type your email here" value="" id="input_email" >
                                    <label for="input_email">Email</label>
                                </p>

                                <p class="pure-steps_group-step_item flexy-item flexy-column reverse">
                                    <input  type="text" placeholder="Type your nick here" value="" id="input_nick">
                                    <label for="input_nick">Name</label>
                                </p>

                                <p class="pure-steps_group-step_item flexy-item flexy-column reverse">
                                    <input type="text" placeholder="Type your nick here" value="" id="input_nick">
                                    <label for="input_nick">Phone</label>
                                </p>
                            </fieldset>
                        </li>

                        <li class="pure-steps_group-step flexy-item">
                            <div class="pure-steps_preload">
                                <svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 32 32" style="enable-background:new 0 0 32 32;" xml:space="preserve">
                                    <path d="M31.8,3.6c-0.2-0.5-0.4-0.9-0.9-1.2C30.4,2,29.7,1.8,29,1.9c-0.6,0.1-1.2,0.4-1.6,1l-8.5,11.2l0,0l-7.2,9.5l-7.1-9.4 c-0.5-0.7-1.3-1-2.1-1c-0.5,0-1,0.2-1.4,0.5c-0.5,0.4-0.9,1-1,1.7s0.1,1.2,0.5,1.8l9.1,12.1l0,0c0.1,0.2,0.3,0.3,0.4,0.4 c0.4,0.3,0.9,0.5,1.4,0.5c0.8,0,1.6-0.3,2.1-1L22.1,18l0,0l9.1-12.1C32,5.2,32.1,4.4,31.8,3.6z"></path>
                                </svg>
                            </div>
                        </li>
                    </ol>

                    <ol class="pure-steps_group-triggers">
                        <li class="pure-steps_group-triggers_item">
                            <label for="step-0">Restart</label>
                        </li>

                        <li class="pure-steps_group-triggers_item">
                            <label for="step-1">Grab a tour</label>
                        </li>

                        <input type="submit" name="submit">

                        <li class="pure-steps_group-triggers_item">
                            <label type="submit" name="submit" for="step-2">Jump in</label>
                        </li>
                    </ol>

                </div>
                <br>
                <label for="step-0">Restart</label>
            </form>
        </article>
    </section>
</body>

</html>

文件在原处。 这里似乎有什么问题? 我认为这是调用 PHP 的一个问题——没有地方可以调用它。 任何建议问题出在哪里?

【问题讨论】:

  • 您忘记了表单标签上的action 吗?
  • 您尝试了什么来发现错误?提交表单后究竟发生了什么?
  • @NicoHaase - 没有任何反应。这就是我寻找线索的原因。
  • 你提交表单时是否运行了任何JS?喜欢使用 Ajax 或类似方式发布它吗?此外,您有两个具有相同 ID input_nick 的文本输入。 ID 在文档中必须是唯一的。此外,这两个字段都缺少提交它们所需的name-attribute。
  • @MagnusEriksson 不,这是我拥有的唯一代码。 codepen.io/artoha/pen/LaOYaQ - 这是它的样子。使用您正确建议的名称属性 + 正确的文本输入。

标签: php css forms function output


【解决方案1】:

PHP 不是客户端,每次请求都会执行一次。

您可以使表单指向自身,这将使页面重新加载,但这次使用$_POST 数据。

<form method="post" class="pure-steps" action="thisPage.php" >

【讨论】:

  • 省略操作标签(就像 OP 一样)或将其留空也会使表单发布到自身,所以这应该没有任何区别。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多