Keeping checkbox values on error...

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
incyte
Forum Newbie
Posts: 7
Joined: Sun Jul 25, 2004 7:33 pm

Keeping checkbox values on error...

Post by incyte »

I can't seem to figure out a way to keep the checkbox value when an error occurs on the form.

The value is there but the checkbox doesnt remain selected when the errors come up , if there are any.

i have my form set up in a block:

$form = " <html code here>....";

so when my error checking goes thru it echo's $form and the error messages on the missing or invalid fields. How do i keep the selected checkbox selected...if that made any sense.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you need to add something that will add "selected" to the fields output so it'll look like this:

Code: Select all

<input type="checkbox" name="foo" value="bar" selected="selected" />I am a selected checkbox<br />
<input type="checkbox" name="foo" value="bif" />I am an unselected checkbox<br />
incyte
Forum Newbie
Posts: 7
Joined: Sun Jul 25, 2004 7:33 pm

Post by incyte »

yea i figured that just dunno how to put it in there using the "block"

was gonna put this after the value of the box

Code: Select all

<?php
<? if ($gender == male) { echo " selected"; } ?> 

if i put that in tho i just get a parse error....sayin something bout T_STRING T_VARIABLE something like that..
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I use something similar to this:

Code: Select all

<?php

function constructInput($type,$name,$value,$error = false,$selected = false)
{
  if(in_array($type,array('checkbox','radio')))
  return '<input type="' . $type . '" name="' . $name . '" value="' . $value . '" ' . ((bool)$selected ? 'selected="selected" ' : '') . '/>' . (is_string($error) ? $error : '');
  else
  return '<input type="' . $type . '" name="' . $name . '" value="' . $value . '" />' . (is_string($error) ? $error : '');
}

?>
incyte
Forum Newbie
Posts: 7
Joined: Sun Jul 25, 2004 7:33 pm

still probs

Post by incyte »

tried this

Code: Select all

if ($_POST&#1111;$gender] = "male") &#123;
        $app_block2 = "
        <input name="gender" type="radio" value="male" selected>
                  Male 
                  <input name="gender" type="radio" value="female">
                  Female";
                &#125;
        else if ($_POST&#1111;$gender] = "female") &#123;
        $app_block2 = "
        <input name="gender" type="radio" value="male" >
                  Male 
                  <input name="gender" type="radio" value="female" selected>
                  Female";
                &#125;
        else
        $app_block2 = "
				<input name="gender" type="radio" value="male">
                  Male 
                  <input name="gender" type="radio" value="female">
                  Female";
still not working right figured that would do it but nope... :? ideas?
Draco_03
Forum Regular
Posts: 577
Joined: Fri Aug 15, 2003 12:25 pm
Location: Montreal, Canada

Post by Draco_03 »

you don't pass a vlaue to your "selected"
must be selected = "selected"
incyte
Forum Newbie
Posts: 7
Joined: Sun Jul 25, 2004 7:33 pm

still not working

Post by incyte »

still not working...

now when it loads it automatically checks the male value
Post Reply