Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Retaining form information - check boxes

Status
Not open for further replies.

DaRNCaT

Technical User
Dec 18, 2002
566
NZ
How do you go about retaining selections in check boxes and select boxes when you are error checking a page?

I have it set up so it re-prints the page with the error message if there is something wrong with it, I would like to be able to keep selections as well as text, so they don't have to go through re-ticking and re-selecting all their choices, how do I do this?

Also, is it possible to show more than one error at a time?

at the moment I'm doing it this way
Code:
<input type=\"text\" name=\"email\" value=\""; ?><?= $email ?><?php print "\" size=\"30\" maxlength=\"50\"> 
    "; ?> <?= $errormessage ?> <?php print "

so the whole page is printed out as a function, and I break in and out of print to use the php commands. It works well, but I'm not sure how to show more than one error, or how to retain the select box and checkbox selections.

----------------------------------------
Sometimes, when my code just won't behave, I take it outside and make it listen to britney spears music, and when it comes back it's really well behaved. I wonder if it's suffering from post tramatic stress syndrome now..
 
First your code fragment doesn't make sense. Here how I would code what I think you meant:
Code:
echo '<input type="text" name="email" value="' . $email . '"  size="30" maxlength="50">' . $errormessage;

As for checkboxes and radio buttons, you have to compare what you received to what you're outputing in HTML if they match add the word "checked". If you're doing selection lists, use the word selected. For example:
Code:
<?php
//
// arrays to be used to set up checkboxes, radio buttons, and selection lists
//
$ckbx = array('Box1'=>'1','Box2'=>'2','Box3'=>'3');
$rdbtn = array('one','two','three');
$options = array('First','Second','Third');
$tmp = array(); //temporary array to hold HTML code
$tmp[] = '<form action="' . $_SERVER['PHP_SELF'] . '" method="POST">';
foreach ($ckbx as $bx=>$v) {
    $ln = '<input type="checkbox" name="' . $bx . '" value="' . $v . '"';
    $ln .= ($_POST[$bx] == $v)?' checked':'';
    $ln .= '> Box ' . $v . '<br>';
    $tmp[] = $ln; }
foreach ($rdbtn as $v) {
    $ln = '<input type="radio" name="rdobtn" value="' . $v . '"';
    $ln .= ($_POST['rdobtn'] == $v)?' checked':'';
    $ln .= '> Radio Button ' . $v . '<br>';
    $tmp[] = $ln; }
$tmp[] = '<select name="options">';
foreach ($options as $v) {
    $ln = '<option value="' . $v . '"';
    $ln .= ($_POST['options'] == $v)?' selected':'';
    $ln .= '>' . $v . '</option><br>';
    $tmp[] = $ln; }
$tmp[] = '</select><br>';
$tmp[] = '<input type="submit" name="submit" value="Send Form">';
$tmp[] = '</form>';
echo implode("\n",$tmp)."\n";
?>

I just typed this code in, so there are probably typos. YMMV

Ken
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top