homepage Welcome to WebmasterWorld Guest from 54.205.242.179
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe and Support WebmasterWorld
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Checbox query
How to update Db when user deselects checkbox
woldie




msg:1258863
 12:55 pm on May 1, 2004 (gmt 0)

Hi all,

I've got this annoying problem, of how do you go about when a user deslects a checkbox, and then update the DB accordingly.

What I am trying to achieve is when a user selects a checkbox it places a 1 in the DB, if it deselects it then it place a 0 in the DB

Code:

if ($submit == 'Edit')
{

if (isset($live))
{
// obviously I need to write some code here so that a user a deselect the checkbox

mysql_query("update tablename
set live=1
where user_num=$user_num");
}
}

<form method="post" action=show.php>
<input type=checkbox name=live value=1>
<input type="hidden" name=user_num value=<? echo $user_num?>>
<input type="submit" name="submit" value="Edit">
</form>

Many thanks

 

dcrombie




msg:1258864
 2:29 pm on May 1, 2004 (gmt 0)

The problem:

No value is passed when a checkbox is not selected. When it is selected you either get "on" (if no value is set) or the value assigned to the checkbox.

The solution:

The only way to record both positive and negative input from checkboxes is to loop through the array of possible inputs and note which appear in the $_GET or $_POST array.

I suggest something like:

$box1checked = ($box1checked == "on") ? 1 : 0;

;)

woldie




msg:1258865
 2:39 pm on May 1, 2004 (gmt 0)

Thanks dcrombie,

Can you explain how I would implement this in the code I've shown you?

Many thanks

Cheers :o)

Paul in South Africa




msg:1258866
 2:41 pm on May 1, 2004 (gmt 0)

if (isset($live))
{
$live = 1;
}
else
{
$live = 0;
}
mysql_query("update tablename set live=$live
where user_num=$user_num");

woldie




msg:1258867
 2:57 pm on May 1, 2004 (gmt 0)

Thanks again Paul,

That did the trick :o)

Didn't realise it was relatively straightforward.

Thank yoooo

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved