I am creating forms on an HTML web page so that visitors can add comments about my page. This includes different types of forms and I am struggling on an if else statement for the radio button. My html code is posted blow and then the php linking to it is posted below that. PLEASE HELP!!!
<html>
<form action="process_comment.php" method="post">
Name:
<input type="text" name="username" value=""/>
<br>
</form>
<form action="process_comment.php" method="post" >
Do you like this page?
<input type = "radio" name = "like" value = "0"> Yes
<input type = "radio" name = "like" value = "1"> No
<br>
<input type="submit" value ="go" /><br>
</form>
</html>
<?php
$userName=$_POST["username"];
echo "Hello <b>$userName</b>!<br>";
?>
<?php
$like = $_POST["like"];
if($like = "post") {
echo "I am happy that you <b>like</b> this page :)";
} else {
echo "I am sorry that you <b>do not like</b> my page :(";
}
?>
You are using two forms and one action try below code and merge both forms in one.
<html>
<form action="process_comment.php" method="post">
Name:
<input type="text" name="username" value=""/>
<br>
Do you like this page?
<input type = "radio" name = "like" value = "0"> Yes
<input type = "radio" name = "like" value = "1"> No
<br>
<input type="submit" name="submit" value ="go" /><br>
</form>
</html>
And in process_comment.php
<?php
if(isset($_POST['submit'])
{
$userName=$_POST["username"];
echo "Hello <b>$userName</b>!<br>";
$like = $_POST["like"];
if($like) {
echo "I am happy that you <b>like</b> this page :)";
}
else {
echo "I am sorry that you <b>do not like</b> my page :(";
}
}
?>
If value is 1 or 0 you can use variable in if condition as it works as true or false.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.