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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP - Re-display form data, including multi checkboxes

Status
Not open for further replies.

BowlerBob

Programmer
Jan 16, 2003
21
0
0
GB
Hi

This is probably not the first query on this topic.
If there is already a solution here, please direct me.

I have created a Form that includes name, company, email, etc., but also some topics the user is interested in.
These are in several Select boxes; there is also a multi-choice Checkbox.

The Form is displayed on one page, filled in, then Submitted.
It is Actioned on another page which picks up the $_POSTed data.
At the moment I only Validate the email address.
If there is an Error, the page is redisplayed with submitted data and choices.

My problem is how to fill the Checkbox part from it's $_POST['xxxxxx'] array.
I want to use PHP ONLY (no JavaScript, Perl, etc.)

Hoping someone will be able to solve this and include an example, or direct me to any (simple) solution already on the 'Net


best regards

Bob Sharp
 
You can preset checkboxes to show as checked with
Code:
<input type="checkbox" name="vehicle" value="Bike" [COLOR=red]checked="checked"[/color]/>

If you want the best response to a question, please check out FAQ222-2244 first.
'If we're supposed to work in Hex, why have we only got A fingers?'
Drive a Steam Roller
 
Sorry, but I am asking how to re-display all the fields fields with the entries made by the User.

cheers



 
That's going to depend a bit on how you have your checkboxes set up. But you'll have to check what is submitted in your POST variable by your checkboxes as only the checked options will be sent.

For instance if you have your checkboxes set up as an array you can do something like the following:

Code:
<input type="checkbox" name="opts[0]" value="Alpha" <?PHP if(isset($_POST['opts'][0])){ echo "checked=checked"; } ?>>
<input type="checkbox" name="opts[1]" value="Beta" <?PHP if(isset($_POST['opts'][1])){ echo "checked=checked"; } ?>>
<input type="checkbox" name="opts[2]" value="Gamma" <?PHP if(isset($_POST['opts'][2])){ echo "checked=checked"; } ?>>
<input type="checkbox" name="opts[3]" value="Delta" <?PHP if(isset($_POST['opts'][3])){ echo "checked=checked"; } ?>>



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
That was GREAT ! thanks.

Thought the <SELECT> <OPTION script would be easier (!)
[red]parse error in C:\wamp\ on line 194 [/red]


this is first input page ...

<select name="qTurnover" tabindex="15" >
<option value="qT00" >£0</option>
<option value="qT20" >£20,000</option>
<option value="qT35" >£35,000+</option>
<option value="qT69" >£69,000+ </option>
<option value="qT100" >£100,000+</option>
<option value="qT500" >£500,000+</option>
<option value="qT1000">£1,000,000</option>
</select>


this is from the processing page ...
$a_tOval[1]="qT00";
$a_tOval[2]="qT20";
$a_tOval[3]="qT35";
$a_tOval[4]="qT69";
$a_tOval[5]="qT100";
$a_tOval[6]="qT500";
$a_tOval[7]="qT1000";
$a_tOtxt[1]="£0";
$a_tOtxt[2]="£20,000";
$a_tOtxt[3]="£35,000";
$a_tOtxt[4]="£69,000";
$a_tOtxt[5]="£100,000";
$a_tOtxt[6]="£500,000";
$a_tOtxt[7]="£1,000,000";


select box probagation ....

<select name="qTurnover" tabindex="15" >
<?php
for($val=1;$val<=7;$val++) {
if ((!isset($_POST['qTurnover']) && ($_POST['qTurnover']==$a_tOval[$val]))
{ [red] //line 194 [/red]
echo("<option value=".$a_tOval[$val]." selected>".$a_tOtxt[$val]."</option> /n");
}else{
echo("<option value=".$a_tOval[$val].">".$a_tOtxt[$val]."</option> /n");
}
}
?>
</select>
 
You are either missing a closing parenthesis or have one too many. Either way there's a lone ( that has no closing counterpart.
Code:
if [COLOR=black yellow]([/color][COLOR=gray green]([/color]!isset[COLOR=gray blue]([/color]$_POST['qTurnover'][COLOR=gray blue])[/color] && [COLOR=black red]([/color]$_POST['qTurnover']==$a_tOval[$val][COLOR=black red])[/color][COLOR=gray green])[/color][COLOR=black yellow]_[/color]
/*No closing brace to match up*/

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Thanks for spotting that.
I don't have a PHP editor, which would you recommend ?

Now the code does not seem to be working.
Any idea why ?

cheers
 
For Editors, I like Notepad++. Its got syntax highlighting.

I also use PHP Developer though that one is not free.
As for why its not working, its because your conditionals can never be both true.

Code:
if ((!isset($_POST['qTurnover']) && ($_POST['qTurnover']==$a_tOval[$val])))

Your checking first if $_POST['qTurnover'] is not set by checking if its different to isset(). !isset()

However you can't want it to be not set, and then check if it has a value. Instead yo want it to be there so you can check for a value.

Remove the ! from your isset conditional, and it should work

Code:
if ((isset($_POST['qTurnover']) && ($_POST['qTurnover']==$a_tOval[$val])))

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top