-1

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 ?

dawid malek
  • 25
  • 1
  • 1
  • 4
  • Is this part of a form? I'm not sure you have given us enough code here. – RemanBroder Sep 13 '16 at 20:29
  • If part of a form, is the method set to POST? What is the value of `var_dump($_REQUEST);` ? – Blake Sep 13 '16 at 20:31
  • 1
    checkboxes which are not checked are NOT submitted with the form. therefore if you get `$_POST['name_of_checkbox']` then it WAS checked. – Marc B Sep 13 '16 at 20:31
  • [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) – buhtz Sep 13 '16 at 20:34
  • 1
    _In what way_ "don't" it work? – Patrick Q Sep 13 '16 at 20:35
  • 1
    Your code should work. MarcB's comment is right, so the check using `isset` is the proper way to check for checkboxes being set. If it doesn't, it could be because of all kinds of reasons, not reflected in your code snippet. It could be anything from the wrong form method (get), the wrong target url or a simple syntax error in another piece of your code, to PHP not being installed on your server at all. Without a proper error message and preferably also the surrounding piece of code, we are not going to be able to help. – GolezTrol Sep 13 '16 at 20:36
  • Please include the full code @dawid malek. – Abhrapratim Nag Sep 13 '16 at 20:46
  • Please do not edit your original posting. Or, if you do, it is common practice to put "EDIT:" out in front of your edit. Your question received answers and then you changed it. Thus, making the answers you received invalid. Try to remember this moving forward as your question appears to be down voted (not by me). Probably because of original quality and not enough code to go on. But it is always a good thing to do to let others know your edits to see the progression of your inquiry. Good luck! – mrwpress Nov 27 '19 at 00:52

2 Answers2

4

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
}
mrwpress
  • 311
  • 1
  • 8
  • Note: Original poster had ```if (isset($_GET['checkbox1'])) { echo "Hello world!"; }``` Notice the "$_GET". Their original question has been changed since my initial answer. In addition, they put the input inside of a
    – mrwpress Nov 27 '19 at 00:49
0

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!";
}
?>
Slava Knyazev
  • 5,377
  • 1
  • 22
  • 43