Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP How-To
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 28th, 2009, 09:30 PM
Authorized User
 
Join Date: Nov 2007
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
 
Old May 8th, 2009, 02:43 PM
Authorized User
 
Join Date: Nov 2008
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'].
 
Old May 11th, 2009, 11:58 AM
Authorized User
 
Join Date: Nov 2007
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
 
Old May 11th, 2009, 01:12 PM
Friend of Wrox
 
Join Date: Sep 2005
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
 
Old May 14th, 2009, 01:33 AM
Authorized User
 
Join Date: Nov 2008
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





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





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