【问题标题】:How to export input html data to txt by php (problem)如何通过php将输入的html数据导出为txt(问题)
【发布时间】:2022-01-22 22:20:05
【问题描述】:

我在互联网上找到了这段代码,我想要这些输入: 姓名 包含 5 个项目的复选框

此代码将复选框中选中的用户信息和他的姓名保存在文本文件中

有人可以帮我解决这个问题吗?

<!DOCTYPE html>
<?php
if(isset($_POST['submit'])){
$Name = "Username:".$_POST['username']."
";
$Pass = "Password:".$_POST['password']."
";

$file=fopen("saved.txt", "a");
fwrite($file, $Name);
fwrite($file, $Pass,);
echo fwrite($file,"*************************",);
fclose($file);
}
?>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $('.check').click(function() {
        $('.check').not(this).prop('checked', false);
    });
});
</script>
<BODY>

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

    <p>
        <label>Login Name:</label><input type = "text"  name = "name" /><br>

<input type="checkbox" type = "password"  name="pwd[]" class="check" value="male"> Male
<input type="checkbox" type = "password"  name="pwd[]" class="check" value="female"> Female
<input type="checkbox" type = "password"  name="pwd[]" class="check" value="other"> Other

        <br/>
        <br/>
    </p>
    <input type = "submit" name="submit_btn" id = "submit" value = "submit"/>
    <input type = "reset"  id = "reset" value = "reset"/>
</form>
</BODY>
</html>

【问题讨论】:

    标签: javascript php html post input


    【解决方案1】:

    您的帖子数组包含带有您输入字段名称的项目。 这将起作用

    <!DOCTYPE html>
    <?php
    if(isset($_POST['submit'])){
    $Name = "Username:".$_POST['name']."
    ";
    $Pass = "Password:".$_POST['pwd']."
    ";
    
    $file=fopen("saved.txt", "a");
    fwrite($file, $Name);
    fwrite($file, $Pass,);
    echo fwrite($file,"*************************",);
    fclose($file);
    }
    ?>
    <html>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script>
    $(document).ready(function(){
        $('.check').click(function() {
            $('.check').not(this).prop('checked', false);
        });
    });
    </script>
    <BODY>
    
    <form action = "post.php" method="POST">
    
        <p>
            <label>Login Name:</label><input type = "text"  name = "name" /><br>
    
    <input type="checkbox" type = "password"  name="pwd" class="check" value="male"> Male
    <input type="checkbox" type = "password"  name="pwd" class="check" value="female"> Female
    <input type="checkbox" type = "password"  name="pwd" class="check" value="other"> Other
    
            <br/>
            <br/>
        </p>
        <input type = "submit" name="submit" id = "submit" value = "submit"/>
        <input type = "reset"  id = "reset" value = "reset"/>
    </form>
    </BODY>
    </html>
    

    【讨论】:

    • 你能修复这个代码吗?我对php一无所知,我需要这段代码
    • 此代码有效,但您的 php 代码应位于名为 post.php 的文件中
    • 我这样做了,但还是不行!我不知道如何将 chackbox 项目调用到 php 部分
    【解决方案2】:

    您首先必须了解表单的工作原理。

    您要求提供 $_POST["username"]$_POST["password"] 的 Post-Data。 这些“名称”由相应输入的“名称”属性定义。因此,要获得用户名,您必须有一个像 &lt;input type="text" name="username" /&gt; 这样的输入。

    现在查看您自己的 HTML:您已经为用户名定义了一个 Input,但它具有属性 name="name"。这将导致 Post-Data 存储在 $_POST["name"]

    此外,您只有一个输入,但要求输入两个值(用户名和密码)。

    至于您的复选框,我不明白它们的名字是name="pwd[]" 的原因,但您可能也应该更改它们。您还指定了两种类型,但第二个 type="password" 无法识别。

    编辑:我再次检查并决定彻底修改您的代码:

    <!DOCTYPE html>
    <?php
        if(isset($_POST['submit'])){
            $Name = "Username:".$_POST['username'];
            $gender = "Gender:".$_POST['gender'];
        
            $file=fopen("saved.txt", "a");
            fwrite($file, $Name);
            fwrite($file, $Pass);
            fwrite($file,"*************************");
            fclose($file);
        }
    ?>
    <html>
    <body>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
        <script>
            $(document).ready(function(){
            $('.check').click(function() {
                $('.check').not(this).prop('checked', false);
            });
        });
        </script>
        <form action = "post.php" method="POST">
            <p>
                <label>Login Name:</label><input type = "text"  name = "username" />
                <br>
                <input type="checkbox" name="gender" class="check" value="male"> Male
                <input type="checkbox" name="gender" class="check" value="female"> Female
                <input type="checkbox" name="gender" class="check" value="other"> Other
            <br/>
            <br/>
        </p>
        <input type = "submit" name="submit_btn" id = "submit" value = "submit"/>
        <input type = "reset"  id = "reset" value = "reset"/>
    </form>
    </body>
    </html>
    

    我推荐你learn more about HTML forms

    【讨论】:

    • 这个链接是原始代码:stackoverflow.com/questions/54144704/…我想用复选框更改密码部分
    • 现在我明白你想要达到的目标了。下次最好将此类信息放在问题中,因为我们需要了解您要做什么。再次编辑代码,使其可以正常工作
    • 我想要一个这种类型的表单:NAME / 3 复选框 / 用户输入他们的名字并选择一个复选框并提交。然后将此输入的信息保存为文本文件!
    • 这就是我提供的代码应该做的。如果您遇到任何错误,请发表评论。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-04
    • 1970-01-01
    • 2023-03-23
    • 2018-02-21
    • 1970-01-01
    • 1970-01-01
    • 2016-08-17
    相关资源
    最近更新 更多