简体   繁体   English

如何通过php将输入的html数据导出到txt(问题)

[英]How to export input html data to txt by php (problem)

i found this code on inte.net and i want to have these inputs: name check box with 5 items我在 inte.net 上找到了这段代码,我想要这些输入:包含 5 个项目的名称复选框

this code saves user's info that selected in checkbox and his name in a text file此代码将在复选框中选择的用户信息及其名称保存在文本文件中

can someone help me to fix this?有人可以帮我解决这个问题吗?

 <?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>

I think first you should learn basics of php and then try for it as I can see there are syntax error and logical erros as well in the code even if you have copy it from somewhere else you should fix them before posting and try it so even did not try the code and just posted directly here.我认为首先你应该学习 php 的基础知识,然后尝试它,因为我可以看到代码中也存在语法错误和逻辑错误,即使你从其他地方复制了它,你应该在发布之前修复它们并尝试它没有尝试代码,只是直接在这里发布。

Your post array contains item with the name of your input fields.您的帖子数组包含带有输入字段名称的项目。 This will work这将起作用

<!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>

You will first have to understand how the form works.您首先必须了解表单的工作原理。

You ask for Post-Data of $_POST["username"] and $_POST["password"] .您要求提供$_POST["username"]$_POST["password"]的 Post-Data。 Those "Names" are defined by the "name"-Attribute of the corresponding Input.这些“名称”由相应输入的“名称”属性定义。 So to get a username you will have to have an Input like <input type="text" name="username" /> .因此,要获得用户名,您必须有一个像<input type="text" name="username" />这样的输入。

Look now at your own HTML: You have defined an Input for the users name, but it has the Attribute name="name" .现在看看你自己的 HTML:你已经为用户名定义了一个 Input,但它有 Attribute name="name" This will result in the Post-Data being stored in $_POST["name"] .这将导致 Post-Data 存储在$_POST["name"]中。

Also you only have one input, but ask for two values (username AND password).此外,您只有一个输入,但要求输入两个值(用户名和密码)。

As for your checkboxes I don't understand the reason their names are name="pwd[]" , but you should probably change them too.至于你的复选框,我不明白它们的名字是name="pwd[]"的原因,但你可能也应该改变它们。 You also specify two types but that second type="password" is not recognized.您还指定了两种类型,但第二种type="password"无法识别。

Edit: I checked again and decided to completely rework your code:编辑:我再次检查并决定完全修改您的代码:

<!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>

I recommend you to learn more about HTML forms .我建议您了解有关 HTML forms的更多信息。

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

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