Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

374
Vistas
How to export input html data to txt by php (problem)

i found this code on internet and i want to have these inputs: name check box with 5 items

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>

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

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>
about 4 years ago · Juan Pablo Isaza Denunciar

0

You will first have to understand how the form works.

You ask for Post-Data of $_POST["username"] and $_POST["password"]. 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" />.

Look now at your own HTML: You have defined an Input for the users name, but it has the Attribute name="name". This will result in the Post-Data being stored in $_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. You also specify two types but that second type="password" is not recognized.

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.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda