问题
I want to verify if my checkbox is checked in php and if it is, i want to echo "Hello word". Here is my html code :
<form class="checkclass">
<input type="checkbox" name="checkbox1"> 4K </input>
</form>
php :
<?php
if (isset($_POST['checkbox1'])) {
echo "Hello world!";
}
?>
But it doesn't work and i really don't know how to fix this. Can someone please help me ?
回答1:
isset($_GET['checkbox1']))
does not work because it is checking a URL query string. NOT a form submission. Use $_POST instead of $_GET. So it would be like this:
if (isset($_POST['checkbox1'])) {
// Go ahead and do stuff because it is checked
}
回答2:
You are sending a GET request but handling as a POST request. Either of the following codes will work:
<form class="checkclass" method="POST">
<input type="checkbox" name="checkbox1"> 4K </input>
</form>
<?php
if (isset($_POST['checkbox1'])) {
echo "Hello world!";
}
?>
<form class="checkclass">
<input type="checkbox" name="checkbox1"> 4K </input>
</form>
<?php
if (isset($_GET['checkbox1'])) {
echo "Hello world!";
}
?>
来源:https://stackoverflow.com/questions/39478480/verifying-if-checkbox-is-checked-in-php