Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checkbox values being passed 2

Status
Not open for further replies.

jedel

Programmer
Jan 11, 2003
430
AU
Hi all,

I've bee doing some research on checkboxes in PHP and how they work. I've also been playing with them in a project of mine and have found some interesting results.

I found this thread - thread181-27567 and jpadie stated that an unchecked checkbox returns nothing.

I have found that in my project, it does not appear to be the case. Let me describe my settings and what I did to acheive a result.

Firstly my checkboxes are linked to fields that are set to TinyInt meaning they can only accept a 1 or a 0.

Now what I wanted to do was to have the checkboxes checked if there was a 1 in these fields to indicate that the checkbox had in fact been checked previously.

The code below is what I placed into the HTML tag to acheive this
Code:
<?php if ($post['DLApp']==1){ echo "checked='checked'";}?>

What I found was that if I checked a box previously so that the field value was 1 and then unchecked the box the next time around, the field value would then be 0.

I can only conclude that regradless of what the checkbox is set at (checked or unchecked) that it sends something back to the database.

Now my method works fine in this instance, is this method flawed at all?

I'm using an MVC structure in my project, is there any other way of checking the checkboxes when the fields are set at 1?

-------------------------------------------------------------
"The most overlooked advantage of owning a computer is that if they foul up there's no law against whacking them around a bit."
 
<?php if ($post['DLApp']==1){ echo "checked='checked'";}?>
this is not going to work. $post is not the incoming variable for posted forms.

try this

Code:
<?php echo (empty($_POST['DLApp'])) ? '' : ' checked="checked" ';?>
 
tsuji

The form you refere to was a test that jpadie gave to me to test the browser response to the checkboxes.My form posts to a controller page and then through to the update function.

I'll change the name of the submit button if you like, but the submit has never really been a problem.

[2] Also you can add a line in the php like this.
echo "<div>".var_dump($_POST)."</div>"
such as in jpadie's demo before the $message line to see exactly what you get.

Does the $_POST values carry through to multiple pages? if not then I need to find another way, note the number of pages the form goes through above.

I have done all of your other suggestions. Thanks for that

-------------------------------------------------------------
"The most overlooked advantage of owning a computer is that if they foul up there's no law against whacking them around a bit."
 
jpadie,

You're right $post is a varible used to get the data from the database. What I am looking for is
"if the vale of the associated db field is equal to 1, then check the box."

SO I'm actually checking the box before I submit the form, otherwise the way the code is set up now, if the box is not checked it will now return a value of 0 to the db.

Doesn't your method check to see if the checkbox is checked after it has been posted?

-------------------------------------------------------------
"The most overlooked advantage of owning a computer is that if they foul up there's no law against whacking them around a bit."
 
yes - my code is to create a sticky checkbox.

if you are really using $post as the results of a database recordset retrieval then just change $_POST to $post. if the database column is zero or null then it will not return a string.
 
Well,

This has been an enlightening thread. You have set me straight (yet again). I'm going to make a FAQ from this as it has been a good education on the proper use of checkboxes.

Personally I think it is quite strange that we need so much code to place a zero or a 1 into a db field, but hey, I'm only a noob at PHP (Maybe I have too much common sense to be a "puritan" coder.)

Oh by the way, you'll be happy to know that I've done all of the PHP coding using PHP Designer and not dreamweaver. ( I did do some of the CSS and page design using Dreamweaver though...just going to duck now....)

Cheers

-------------------------------------------------------------
"The most overlooked advantage of owning a computer is that if they foul up there's no law against whacking them around a bit."
 
jedel

you don't need loads of code to do the insert at all. have we been talking at cross purposes?

just use this function to get the right code for db inserts/updates

Code:
function cbValue($fieldname, $type="POST"){
 return (int)  ! empty($_{$type}[$fieldname]);
}

use like this

Code:
$sql  = "insert into tablename set fieldname=" . cbValue('fieldname').", someotherfield='somevalue'";
because the function returns a guaranteed integer you do not need to worry about escaping or enquoting.

as an alternative, take a look a HTML_QuickForm and its automatic form validation etc. it also has a 'magic' type of checkbox (just js stuff really) which always returns a value from the browser. you could always use this technique too.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top