Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > PHP How-To
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
PHP How-To Post your "How do I do this with PHP?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP How-To section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old April 28th, 2009, 09:30 PM
Authorized User
 
Join Date: Nov 2007
Location: , , .
Posts: 54
Thanks: 1
Thanked 0 Times in 0 Posts
Default passing check box value to next page

I am trying to pass values from a check box to the next page, which is an insert page that is storing values for the check box field ( archive: yes if checked and no if unchecked, or 1 and 0).
Can I get some help here?

one.php:
Code:
<input type="checkbox" name="archive" value="<?=value from database ;if(value from database ==1){echo checked};" >
Something on these lines, and then on page two.php I can get values by POST and then store that in the database.

Problem is how to store uncheck value and also how to retrieve and pass value to the checkbox. A user should be able to check and uncheck if he/she wants to edit a record.

In my few failed attempts, I have been able to (to some extent) to store value as "1" for checked box, but am struggling to store value for unchecked and then retrieving the stored value for editting.

Any help would be appreciated,
Thanks
Reply With Quote
  #2 (permalink)  
Old May 8th, 2009, 02:43 PM
Authorized User
 
Join Date: Nov 2008
Location: virudhunagar, tamil nadu, India.
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Cool This might help you

Code:
<input type="checkbox" name="archive" value="<?if(value " from database==1) "echo checked";else echo "unchecked";?> <?if(value from database==1) "echo checked";?>
if database value is one then checkbox will be checked.and you could read the value from $_POST['archive'].
Reply With Quote
  #3 (permalink)  
Old May 11th, 2009, 11:58 AM
Authorized User
 
Join Date: Nov 2007
Location: , , .
Posts: 54
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Thanks for your help, but I am still struggling.
I can see the values inserted in the database as 1 and 0, if the checkbox is checked or unchecked respectively.
I can also see the values retrieved from the database as 1 or 0 when I go to edit page again.
The problem is: based on the values it is not checking or unchecking the box for values retrieved from the database.
How can check the box if the value is 1 and uncheck otherwise. I have tried using 'checked' outside of value attribute, it works but then it is checked for every value.
Any clue ?

Code:
<input type="checkbox" name="archive" value="<? if($p['archive']==1) print 'checked'; else print 'unchecked'?>" ><?=$p['archive']?></input>
The source code of page for uncheck is:
Code:
<input type="checkbox" name="archive" value="unchecked" />0
and the source code of page for check is:
Code:
<input type="checkbox" name="archive" value="checked" />1
Reply With Quote
  #4 (permalink)  
Old May 11th, 2009, 01:12 PM
Friend of Wrox
Points: 894, Level: 11
Points: 894, Level: 11 Points: 894, Level: 11 Points: 894, Level: 11
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2005
Location: London, , United Kingdom.
Posts: 166
Thanks: 2
Thanked 33 Times in 33 Posts
Default

Hi,

To set a checkbox as ticked, you should use the checked attribute. It doesn't matter what you set checked to, it just has to be present, but the normal way to do it is:
Code:
<input type="checkbox" name="archive" checked="checked" />
Just change your code to add this if archive is 1.:
Code:
<input type="checkbox" name="archive"
 <?php if($p['archive']==1) print 'checked="checked"'; ?> />
Phil
Reply With Quote
  #5 (permalink)  
Old May 14th, 2009, 01:33 AM
Authorized User
 
Join Date: Nov 2008
Location: virudhunagar, tamil nadu, India.
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Cool this might help u

you should use "checked" to make a check box checked.
the source code must look like this.
To check:
Code:
<input type="checkbox" name="archive" value="checked" checked>1
To uncheck:
Code:
<input type="checkbox" name="archive" value="unchecked">0
add print statements to print "checked" at the end of tag
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Check BOx kaushikpulpa ASP.NET 2.0 Professional 2 August 30th, 2007 08:01 AM
help me to pass the check box values to new page ansar BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 0 November 16th, 2004 06:08 AM
please help (get check box value) qadeerahmad General .NET 3 September 24th, 2004 08:46 AM
how to get value from check box..... cici Classic ASP Professional 2 May 15th, 2004 08:35 AM
Passing value in select box to another page Bandy PHP How-To 5 February 11th, 2004 04:40 PM



All times are GMT -4. The time now is 09:51 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.