Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 361
  • Last Modified:

hidden fields, checkboxes, multipage form

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
Case-Sensitive
Asked:
Case-Sensitive
  • 2
  • 2
  • 2
  • +1
1 Solution
 
ZylochCommented:
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
 
Marcus BointonCommented:
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
 
Case-SensitiveAuthor Commented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
ZylochCommented:
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
 
arantiusCommented:
I'd strongly suggest using session variables.
0
 
Case-SensitiveAuthor Commented:
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
 
Marcus BointonCommented:
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now