Please help with checkboxes in a form !

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
amarquis
Forum Newbie
Posts: 10
Joined: Fri Dec 19, 2003 3:53 pm
Location: Switzerland

Please help with checkboxes in a form !

Post by amarquis »

Hi there !

I'm quite new to php coding so my question might be easy for you to answer... hope so !

I have buit a form on my website that has 3 pages and now I would like to make a "recapitulation" page before the whole form is emailed. I want my recap form to be displayed like the original one, leaving the choice for the user to unselect or change some fields at this step.

I have no problem with textboxes but how can I recall the checkboxes states ? I want to have the checkboxes displayed and checked or not depending on user's choice in the previous pages.

How could I do that ? :roll:

Thanks for your help !
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post by DuFF »

Check out this tutorial for some help with checkboxes in forms:
http://codewalkers.com/tutorials/12/4.html
amarquis
Forum Newbie
Posts: 10
Joined: Fri Dec 19, 2003 3:53 pm
Location: Switzerland

Post by amarquis »

thanks !
amarquis
Forum Newbie
Posts: 10
Joined: Fri Dec 19, 2003 3:53 pm
Location: Switzerland

Post by amarquis »

Mmh.. not sure that this is what I need but.. Can you help with that part of code ?

if (isset($PERIPH1)) {
?> <input type="checkbox" name="PERIPH1" value="<?=$PERIPH1?>" checked> Keyboard <? }

This doesn't work... and I don't know why... but I would like to display a checked checkbox that appears only if "PERIPH1" has been checked on the previous page... the PERIPH1 value is sent to this page from the previous page.

It takes the same name PERIPH1 so that the user can change it's state on the recap page.

Can you help ?
Thanks
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post by DuFF »

You will need to either use:

Code: Select all

<?php
if (isset($_POST['PERIPH1']))

// OR

if (isset($_GET['PERIPH1']))
?>
Depending on the <form> line in your HTML. So if the form method="post" then use $_POST, or if it is method="get" then use the $_GET.
amarquis
Forum Newbie
Posts: 10
Joined: Fri Dec 19, 2003 3:53 pm
Location: Switzerland

Post by amarquis »

Thank you very much !

Does that mean that if I declare a value in my recap page I can use it like that :

Code: Select all

<?php 
if (isset($PERIPH1))
?>
And that if the value has been posted using POST method I'll always need to use

Code: Select all

<?php 
if (isset($_POST['PERIPH1']))
?>
??

Thanks for your help ! I'm learning php from day to day... :wink:
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post by DuFF »

Yep, anytime you use a form you will need to use either $_GET or $_POST.

Check out this link: http://www.php.net/manual/en/language.v ... ternal.php
amarquis
Forum Newbie
Posts: 10
Joined: Fri Dec 19, 2003 3:53 pm
Location: Switzerland

Post by amarquis »

Well I still have a problem with that...

As I said before, I have a 3 pages form. My checkbox name="PERIPH1" is on page 1 and my recap page stands as page 4.

If I use

Code: Select all

<?php
if (isset($_POST['PERIPH1'])) { 
?><input type="checkbox" name="PERIPH1" checked> Screen<? 
}
?>
This works since this code stands on page 2, directly after my page 1 checkbox. But btw page 1 and my recap page, I send my values using html hidden fields using value="<?PERIPH1?>" and then the code reacts as if I always had a value for PERIPH1 which shouldn't occur when my checkbox is not checked...

Any ideas ? maybe is there another way than hidden fields to pass values across pages ?

Thanks !
Post Reply