verifying if checkbox is checked in php

[亡魂溺海] 提交于 2019-12-13 09:54:35

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!