【问题标题】:Sending HTML form data to webserver via PHP通过 PHP 将 HTML 表单数据发送到网络服务器
【发布时间】:2017-09-15 12:51:24
【问题描述】:

我目前正在处理一些网络表单。 该表单应该将收集到的数据发送到网络服务器上的 txt 文件,但不知何故它给了我错误。

表格:

<form action="my_webserver_path_to_script.php" name="form" method="post">
    <div>
        <label for="name">Name</label>
        <input name="name" type="text" id="name" placeholder="Your Name" required/>
    </div>
    <div>
        <label for="city">City</label>
        <input name="city" type="text" id="city" placeholder="Where do you live?" required/>
    </div>
    <div>
        <label for="sex">Sex</label>
        <select name="sex" id="sex" required>
            <option value="" selected disabled hidden>Your sex</option>
            <option value="man">Man</option>
            <option value="woman">Woman</option>
        </select>
    </div>
    <div style="margin-top: 30px; text-align: center;">
        <input class="button" type="submit" value="Join Us!"/>
    </div>
</form>

脚本:

<?php
    if(isset($_POST['name']) && isset($_POST['city']) && isset($_POST['sex'])) {
        $data = $_POST['name'] . '-' . $_POST['city'] . '-' . $_POST['sex'] . "\n";
        $ret = file_put_contents('my_webserver_path_to_data.txt', $data, FILE_APPEND | LOCK_EX);
        if($ret === false) {
            die('There was an error writing this file');
            }
            else {
            echo "$ret bytes written to file";
        }
    }
    else {
        die('no post data to process');
        }
?>

当我填写所有字段并单击提交时,它会跳转到 php 地址并打印“写入此文件时出错”。
我已经阅读:questions/35443812questions/14998961
通常,我在 16:00 到 08:00 之间不回复。
提前致谢!

【问题讨论】:

  • 数据是否写入您的文件?还要确保您的 .txt 文件路径正确
  • 显示此错误是因为您的 if 条件失败:if($ret === false) 所以检查文件是否可写
  • 不,数据没有写入文件。路径是 100% 正确的。
  • 如果你问我,看起来像是权限问题

标签: php html


【解决方案1】:

“文件写入错误”主要是由于您的托管服务提供商的权限设置不正确造成的。可能,您必须设置 775 权限才能写入文件。这个链接可能对你有用https://unix.stackexchange.com/questions/35711/giving-php-permission-to-write-to-files-and-folders

关于您的 PHP 代码。我的建议是使用以下代码改进第 1 行:

<?php
    if ( isset( $_POST['submit'] ) ) {
    }
?>

问候, 编。

【讨论】:

  • 我考虑过permisson的问题,但直到星期一我才能检查。你的意思是我应该用你发送的行缩写整个脚本?
  • 可以在没有他需要的所有输入的情况下发送表单。最好明确检查他需要什么。不过,您可能会像这样使它更清洁:if(isset($_POST['name'], $_POST['city'], $_POST['sex'])) {
  • 所有 2 个输入和选择都是必需的。我已经更改了您建议的线路。
【解决方案2】:

只需输入 php $name= $_POST['name']; $city= $_POST['city']; $sex= $_POST['sex']; .如果你输入 echo $name;你可以看到数据

【讨论】:

    猜你喜欢
    • 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
    相关资源
    最近更新 更多