Solved

hidden fields, checkboxes, multipage form

Posted on 2004-09-04
10
309 Views
Last Modified: 2008-03-10
hey, just saw your php community and wondering if u cud help me out

I have a multipage form, that can be accessed in any order.
I have many checkboxes that need filling in, and that need to be carried over to the next forms or previous ones to be filled. They should remember their state when each page is looked at.

I have tried using hidden fields with no effect(affect!), this is wot ive done then ive tried to fill in the checkbox from the the hidden field value:

<input <?php if (!(strcmp($_POST['dal'],"dal"))) {echo "checked";} ?> name="dal" type="checkbox" id="dal" value="<?php echo $_POST['dal']; ?>">


Im new to php, gave it my best shot, so any help on this would be great,

thanks alot folks.
0
Comment
Question by:Case-Sensitive
  • 2
  • 2
  • 2
  • +1
10 Comments
 
LVL 36

Expert Comment

by:Zyloch
ID: 11981424
Can we see a more complete version of your code to see how you populated the hidden fields, for instance, because it's not too clear right now.
0
 
LVL 25

Expert Comment

by:Squinky
ID: 11981484
I think you'll probably find that it's probably easier to handle a multi-page form using Javascript and CSS (a single big form that uses JS to show/hide sections). That way you can do everything in a single page without having to do server transactions every time, and it's way faster than using PHP. It also degrades elegantly if you run it without JS support (just shows the whole form). This way although the user sees multiple pages, you only ever have to deal with a single form submission from PHP.

Otherwise your approach is not too far off. You should be using sessions if you want to stay sane, otherwise you have to pass the entire set of form values every time. Accept each page's set of values to populate a full set of form values that are stored in a session, e.g. when you post a "dal" checkbox, store it in a session variable: $_SESSION['dal'], then use that when you regenerate the output rather than passing $_POST values back out. It would make sense to pre-load all the default settings into the session to start with (much as you set default values in a form).

if (array_key_exists('dal', $_POST))
  $_SESSION['dal'] = $_POST['dal'];

and later on, when it comes to output:

<input <?php if (($_SESSION['dal'] == 'dal'))) {echo 'checked="checked"';} ?> name="dal" type="checkbox" value="<?php echo $_SESSION['dal']; ?>">

(this may not quite line up with your particular form, but you get the basic idea)

If you have lots of checkboxes, it makes things much simpler to use HTML arrays by naming them like 'items[]'. This will substantially cut down the number of different variables you need to handle.

Correct XHTML is 'checked="checked"' for checkboxes.
0
 

Author Comment

by:Case-Sensitive
ID: 11983903
hi Squinky, thats what i have at the moment on the site

www.poojacaterers.co.uk/quote.htm

but what are the compatibility issuses with this, becuase it uses layers, javascript. will it work in ie 4, 5, 6? Netscape.

This is why i was uncetrain as to keeping it like that.

thanks for reply
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 36

Expert Comment

by:Zyloch
ID: 11983914
It uses <div>s so it should be fine. This function may help you:

function getObj(id) {
   if (document.getElementById) {
      return(document.getElementById(id));
   } else if (document.all) {
      return(document.all[id]);
   } else if (document.layers) {
      return(document.layers[id]);
   } else {
      return false;
}

The only other problem is that NN4 seems to not support .style.display, only .visible = hide|show WITHOUT the .style in front of it.
0
 
LVL 18

Expert Comment

by:arantius
ID: 11984666
I'd strongly suggest using session variables.
0
 

Author Comment

by:Case-Sensitive
ID: 11984762
Hey, i had a go at it, but it doesnt seem to want to store the session. i have 2 pages, firstly this one that send the data to the next page using a onclick event on the submit button, becuase i want to have multiple submit buttons:

<?php
// start the session
session_start();
header("Cache-control: private"); //IE 6 Fix

// Get the user's input from the form
$chk1 = $_POST['chk1'];

// Register the input with the value
$_SESSION['chk1'] = $chk1;

?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<form action="" method="post" name="theForm" id="theForm">
  <p>
    <input name="chk1" type="checkbox" id="chk1" value="chk1" <?php if (!(strcmp($_SESSION['chk1'],"chk1"))) {echo "checked";} ?>>
</p>
  <p>
    <INPUT Type="Submit" Value="Add Record" onClick="this.form.action='form2.php';">
  </p>
</form>

</body>
</html>

--------------------------------------------------------------------

then on the second form, i have a back button to go to the first form, to see if it has remeber the checkbox state:

<?PHP
// Start Session
session_start();
header("Cache-control: private"); //IE 6 Fix

// Get the user's input from the form
$chk1 = $_POST['chk1'];

// Register the input with the value
$_SESSION['chk1'] = $chk1;

?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<form action="index.php" method="post" name="theForm" id="theForm">
  <p>&nbsp;</p>
  <p>
    <input type="checkbox" name="checkbox" value="checkbox">
    <INPUT Type="Submit" Value="Add Record"onClick="this.form.action='index.php';">
  </p>
</form>
</body>
</html>

any help on this would be great folks, cheers
0
 
LVL 25

Accepted Solution

by:
Squinky earned 125 total points
ID: 11988010
You've got things a little confused here I think. You're using JavaScript to submit a form in exactly the same way that a regular submit button does, so you may as well not use JS for that.

As I said, using JS and CSS degrades gracefully because if neither JS nor CSS are supported or enabled, it will still work.

As for your session problem, when you go back to the first page, $_POST['chk1'] has no value, so $_SESSION['chk1'] gets cleared, so it doesn't remember the checked state. You need to check that values are actually submitted before assigning them to your session:

if (array_key_exists($_POST['chk1'])) {
  $_SESSION['chk1'] = $_POST['chk1'];
}

I usually make session values store exactly the correct type for what they're representing, rather than always the strings that come from $_POST, so I'd say:

if (array_key_exists($_POST['chk1'])) {
  $_SESSION['chk1'] = true;
}

This means that later on you can simply say:

<?php if ($_SESSION['chk1']){echo ' checked="checked"';} ?>

Makes for neater code that's easier to validate.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now