Forum Moderators: coopster

Message Too Old, No Replies

checkbox = checked

         

nshack31

5:46 pm on Jan 5, 2005 (gmt 0)

10+ Year Member



ok, i want to set a variable as follows

$box1='checked';

and then call it in a checkbox later on so the checkbox is checked, im using this ...

<input type = \"checkbox\" name = \"tdm\" value = $box1>

but its not checking the box!
Help! Thanks again

nshack31

5:55 pm on Jan 5, 2005 (gmt 0)

10+ Year Member



SORTED! i used....

$box1="checked";

<input type = \"checkbox\" name = \"tdm\" $box1>

Birdman

6:39 pm on Jan 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Technically, you should use:

checked="checked"

The shorter way will work but it's not valid HTML anymore.

Another tip is to enclose HTML strings in single quotes. That way there is no need to escape all the doubles. Here's an example:

<?php

$box1 = ' checked="checked"'; // notice the use of the quotes

echo '<input type="checkbox" name="tdm"$box1>'; // same deal with quotes here
?>

Remember though, that you will need to escape the single quotes in the same way you did your doubles. But there should be many less.

nshack31

9:33 pm on Jan 5, 2005 (gmt 0)

10+ Year Member



super! thanks for that :)

nshack31

10:19 am on Jan 6, 2005 (gmt 0)

10+ Year Member



following on from this, I have a page that updates the users selections code is as follows......

$tdm = $_REQUEST['tdm'];
$obj = $_REQUEST['obj'];
$ctf = $_REQUEST['ctf'];

$sql="UPDATE users SET tdm = '$tdm', obj = '$obj', ctf = '$ctf' WHERE username = '$me'";

tdm, obj, ctf are the names of the checkboxes that are passed onto this form. If the boxes are checked then there are no errors but data is not inserted into the DB, if they are not checked then i get Undefined index errors

Can anyone help?

nshack31

11:29 am on Jan 6, 2005 (gmt 0)

10+ Year Member



I've sorted it now so that if boxes are checked then the data is inserted. But if the data that is sent is unchecked, I get an Undefined index error!

Birdman

12:39 pm on Jan 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I usually do this:

$tdm = isset($_REQUEST['tdm'])? $_REQUEST['tdm'] : '';

This way, if the request variable is not present, $tdm will just be an empty var.

nshack31

1:23 pm on Jan 6, 2005 (gmt 0)

10+ Year Member



eureke!

Thank you very very much :)