How do I write php code to check if checkbox is checked or not (1 Viewer)

yara

New member
Local time
Today, 03:52
Joined
May 30, 2008
Messages
3
hi

How do I write php code to check if checkbox is checked or not ?

correct my red line please



<html>

<head>

</head>

<body>

<script language="JavaScript" type="text/javascript">

function add()
{

<?php

extract($_POST);

if(category[0].checked)

$query = "INSERT INTO mytable (cat) VALUES ('1')

if(category[1].checked)

$query = "INSERT INTO mytable (cat) VALUES ('2')

if(category[2].checked)

$query = "INSERT INTO mytable (cat) VALUES ('3')

if(category[3].checked)

$query = "INSERT INTO mytable (cat) VALUES ('4')

if(category[4].checked)

$query = "INSERT INTO mytable (cat) VALUES ('5')

if(category[5].checked)

$query = "INSERT INTO mytable (cat) VALUES ('6')

// connect to DB
mysql_connect ('localhost', 'root');

// select the DB
mysql_select_db('mydatabase');

// execute the query
mysql_query($query);


?>
return true;

}

</script>

<form name="myform" method ='post' onsubmit="add();">

<input name="category" type="checkbox" value="flowers"/>

<input name="category" type="checkbox" value="animals"/>

<input name="category" type="checkbox" value="children"/>

<input name="category" type="checkbox" value="nature"/>

<input name="category" type="checkbox" value="nature"/>

<input name="category" type="checkbox" value="other"/>

</form>

</body>

</html>



:)

i hope i get help :( :(
 

poster_boy

New member
Local time
Today, 03:52
Joined
Jun 15, 2009
Messages
1
As AngelaP said using of Javascript is the best method.
 

ajetrumpet

Banned
Local time
Today, 05:52
Joined
Jun 22, 2007
Messages
5,638
yara,

if other posts haven't helped you already, we need to know the context of how your PHP is being used in the first place. is the checkbox on a FORM? is it just part of the document in an arbitrary manner? in general, javascript code to check this is as follows:
PHP:
if (document.getElementById("CheckBoxID").checked = true) {
   alert("THE BOX IS CHECKED!"); }
else { alert("THE BOX IS NOT CHECKED...OOPS!"); }
using javascript in PHP is much easier, as it has been said already, than doing this sort of stuff it was geared for than to have to try and "translate" the same code into PHP language.

moreover, i usually javascript integrated with PHP by using single quotes. for this situation, i would try something like this in PHP to get what you want to get done...
PHP:
'<script language="javascript">if (document.getElementById("CheckBoxID").checked = true) {
   alert("THE BOX IS CHECKED!"); }
else { alert("THE BOX IS NOT CHECKED...OOPS!"); }</script>';
 
Last edited:

smithdavid

Registered User.
Local time
Today, 03:52
Joined
Jun 26, 2013
Messages
13
You can use fallowing javascript code

if (
document.getElementById("CheckBoxID").checked = true) {
alert("THE BOX IS CHECKED!"); }
else {
alert("THE BOX IS NOT CHECKED...OOPS!"); }


Or
Take the value of check box in a variable
$chk=$_POST['category'];
 

jj91171

New member
Local time
Today, 03:52
Joined
Jul 29, 2013
Messages
2
How do I write php code to check if checkbox is checked or not
there is the code
<?PHP
if(isset($_POST['submit']))
{
if($_POST['checkbox'] !=' ')
{
put your work which you want to do
}
else
{
echo ' Please check the checkbox';
}
?>
 

lightray

Registered User.
Local time
Today, 22:52
Joined
Sep 18, 2006
Messages
270
Hi, this post has been of some help to me. My challenge is to set a variable after an on click event [country flags] I will later interrogate this to display appropriate pricing on another page, with if statement. I have good HTML skills and still coming up to speed with PHP and javascript. I am developing this for a Wordpress site.

Would javascript also be the best to code this in?
 

Elsnertechnologies

New member
Local time
Today, 16:22
Joined
Jan 21, 2022
Messages
2
Use the isset() Function on $_POST Array to Read if Checkbox Is Checked
Use the in_array() Function to Read if the Checkbox Is Checked for Checkboxes as an Array
Use the isset() Function With Ternary Function to Read if the Checkbox Is Checked
 

theDBguy

I’m here to help
Staff member
Local time
Today, 03:52
Joined
Oct 29, 2018
Messages
21,449
Use the isset() Function on $_POST Array to Read if Checkbox Is Checked
Use the in_array() Function to Read if the Checkbox Is Checked for Checkboxes as an Array
Use the isset() Function With Ternary Function to Read if the Checkbox Is Checked
Hi @Elsnertechnologies. Welcome to AWF!

Just FYI, you are replying to an almost 10-year old thread.
 

Users who are viewing this thread

Top Bottom