Hi

quite a simple question i hope, i want to include a couple of checkboxes in my form and if they are checked i'd like to set the appropriate db cell as true...

should i be looking at something like?

if(chkbox_name == 'checked')
{
$cdplayer = true;
}
...

INSERT INTO cars VALUES ('$cdplayer')


thanks

lworks

Hi.

You have to check if the value of the checkbox is set by isset function for example :

Sport : <input type="checkbox" name="sport" value="football"  />
<?php
if (isset($_POST['sport'])) {

    // do something
} 
?>

Most of the time when I need to enter true or false for INSERTion into a database, I use the value to define the variables for complex queries.

<form action="ha.php" method="post">
<input type="text" length="20" maxlength="32" value="username">
<input type="checkbox" name="understood" value="true" />
<input type="checkbox" name="optIn" value="true" />
<input type="submit" value="sign up" />
</form>

Once the form is posted whether I use get or post, I define the variables to build the a query.

if( isset($_POST['username']) ){
$read=(@$_POST['understood']=='true')? $_POST['understood']:'false';
$enter=(@$_POST['optIn']=='true')? $_POST['optIn']:'false';
$query="INSERT INTO table (uname, agreed, approved) VALUES ('$_POST['username']','$read','$enter')";
}

I usually go with select boxes or radio buttons for fields that I have as enumerated values in the db and check boxes when I have strictly boolean data. It really doesn't matter though if you use this kind of setup or isset(). I usually go with isset() when I am doing major decisive blocks, and do the $var=($true)?true:false; when I have a lot of boolean values to go through for a query. Hope this helps any.

How can I get the "if Command to work after this?

<form action="<?php echo $_SERVER;?>" method="post">
<tr>
<td><strong>Enter Base Cost</strong></td><br/>
<td><p><input type="text" maxlength="6" name="number" size="4" /></td>
</tr>

<tr>
<td valign="top">Add GST (5% of the value entered</td>
<td><input type="checkbox" name="opt" value="+"
</td>
</tr>
<tr>
<td valign="top">Add PST (7% of the value entered:</td>
<td><input type="checkbox" name="opt" value="+"
</td>
</tr>
<tr>
<td valign="top">Add Add Shipping ($8.50)</td>
<td><input type="checkbox" name="opt" value="+"
</td>
</tr>

<tr>
<td><input type="submit" value="Calculate" name="submit" /></td>
</tr>
</form>

Im not sure i totally understand, but Ill try and answer you...

if you have more than one checkbox, you can name them the same as you have done, but you must include []

<input type="checkbox" name="opt[]" value="5" />

including the square brackets saves the three values into an array called $_POST
then when you process your form you can use a foreach loop

if(isset($_POST['save']))
{
    if(isset($_POST['opt']))
{
foreach($_POST['opt'] as $extra)
{
//process the final cost or whatever needs to happen
}
}
}

help Ϋ

i want to display an echo from php right after i check a checkbox.
is this possible?
can i have sample codes.

thanks.

Edited 6 Years Ago by ﻼ im: wrong question.

Hi,

I have a very simple case that is driving me nuts. I have a simple call in php to detect when a checkbox is checked but it never detects when the checkbox is checked! For reasons I'm at a total loss to understand... Please help... here is my code:

if (isset($_POST)) {
}

<input type="checkbox" name="medical_agreement" value="1" /> <b>I Agree</b>

This question has already been answered. Start a new discussion instead.