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 :(:(:(

you cannot mix javascript and php. php is server side and javascript is client side.

here is how i would handle this:

HTML CODE

<html>

<head>

</head>

<body>

<form name="myform" method='post'>

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

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

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

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

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

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

<input name="submit" type="submit" value="Submit" />

</form>

</body>

</html>

PHP CODE

<?php

if (isset($_POST['submit'])) {
  $i = 1;
  while ($i < 7) {
    $value = $_POST['category'.$i];
    if ($value !== '') {
      $sql = "INSERT INTO `mytable` (`cat`) VALUES ('" . $i . "')";
      $query = mysql_query($sql);
    }
  }
}

?>

there is another way but it has never worked for me so I will not post that here

hi
kkeith

thaaaaaaaaaaankx


but where i should put php code


sorry i ask more

put it anywhere in the page, probably at the top.

thaaaaax kkeith29

<input type="checkbox" name="checkbox1" id="checkbox1" class="rightpanel" value="<?php if($row6['24hours']=='1') 
		   { $m="checked"; }  else   {  $m=""; } ?>" <?php echo $m?> />
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.