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>
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"].
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.