Click to See Complete Forum and Search --> : Checkboxes


Javajoob
04-23-2008, 07:46 AM
Is there a way with PHP validate whether a checkbox has been ticked or not. Or even with a radio box. Thanks

LogicOpinion
04-23-2008, 09:44 AM
i think JavaScript Will do it.

Javajoob
04-23-2008, 05:18 PM
I think it can but it might clash with the PHP script

skywalker2208
04-23-2008, 05:31 PM
You can validate in both javascript and php, but don't just rely on just on javascript. You should always have the validation with php because if a user turns off javascript then it won't be validated with javascript.

To validate a radio or checkbox you need to use the $_POST or the $_GET depending on which one you are using in the form. Then you take the name of the checkbox and see if they are equal like this with post


if($_POST["checkbox_name"] == 1) {
//do something because it is validate
}
else {
//error on validation
}

Javajoob
05-02-2008, 04:54 AM
So if it is 1 it is ticked and 0 if it isn't? And if you want to use a number of them with may be identified by an ID, would all of them be affected by this conditional. Thanks

LogicOpinion
05-02-2008, 05:02 AM
using php u can check if the checkbox was checked,

forexample:



<?php

$checkbox_name = $_POST['checkbox_name'];

if ($_POST['checkbox_name' == $checkbox_name)

{ do something }
else
{ print that checkbox name != checkbox name or it was not selected at all }

?>


i think this is what you want to do?

NogDog
05-02-2008, 05:07 AM
A checkbox element is only included with the form data if it is checked. Therefore, it will not exist in the $_POST or $_GET array if it is not checked.

if(isset($_POST['checkbox_name']))
{
// it was checked
}
else
{
// it was NOT checked
}

MrCoder
05-02-2008, 05:27 AM
<?php
echo "<pre>".var_export($_POST, true)."</pre>";
die();
?>


Use the above to view what has been sent from your form.

ubugeni
05-26-2011, 10:10 AM
Hi,

Can someone help me on how I can display a table rows from mysql using checkbox?
Eg:
I am having six rows in my table and I want to choose four arbitrary rows using checkbox.When I will submit ,I want to get data from those 4 rows(in mysql table) that I selected.

Thanks.