|
 |
|
 |
11-23-2005, 11:52 AM
|
#1 (permalink)
|
|
Regular Contributor
Join Date: Apr 2004
Location: Orange County, CA
Posts: 125
|
Form handling
Here's a fun, probably simple one that's eluding me entirely (ok, it's not fun -- but it IS probably simple and it IS eluding me).
I've got a standard HTML form, with a multiple select window -- options have a value of 1,2,3, and so forth . . . about 15 items, laid out like this.
Code:
<form action="apptform.php" method="get">
<select name="Procedures[]" id="Procedures" multiple size="3">
<option value="1" class="form_mult">Acne, Acne Scar & Rosacea Treatment</option> ...
On the following page, a more "detailed" form provide a few more things to fill out, and I want the multiple select window (which will have the exact same options) to reflect the users choice on the previous page, and allow them to change it. So on the next page I've coded:
Code:
<select name="Procedures_two" id="Procedures_two" multiple size="6">
<option value="1" class="form_mult" <?php if ($_GET['Procedures[0]'] = 1)
{echo ("selected=\"selected\"");} ?>>Acne, Acne Scar & Rosacea Treatment</option> ...
.
But it doesn't work, or I wouldn't be telling you about it here. It seems to select everything the php script is placed on, regardless. What am I missing here? Any ideas?
|
|
|
11-23-2005, 12:16 PM
|
#2 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,720
|
Code:
if($_GET['Procedures[0]'] = 1)
assigning a value to something will allways return true
Code:
if($_GET['Procedures[0]'] == 1)
or
Code:
if(isset($_GET['Procedures[0]']))
will be what you're looking for
|
|
|
11-23-2005, 12:35 PM
|
#3 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,505
|
what redhead said .. and also, the proper format to access that variable is:
Code:
$_GET['Procedures'][0]
for example:
Code:
if ($_GET['Procedures'][0] == 1) {
the problem with your logic is that if they selected treatment 2, then item [0] would equal 2. this means you would have to do a switch or if/else for every single option testing for every possible answer.
you could avoid this by using in_array().
example:
PHP Code:
<?
// create a new variable for the procedures array
if (is_array($_GET['Procedures'])) {
$procedures = $_GET['Procedures'];
} else {
// make new array so our array
// function below does not break.
$procedures = array();
}
?>
<select name="Procedures_two" id="Procedures_two" multiple size="6">
<!-- long way -->
<option value="1" class="form_mult" <?php echo in_array(1, $procedures)?" selected":""; ?>>Acne, Acne Scar & Rosacea Treatment</option>
<!-- shorter way using <?= -->
<option value="2" class="form_mult" <?=(in_array(2, $procedures)?" selected":"")?>>Second type of Treatment</option>
</select>
hope that helps.
:: edited some of the code. if it didn't work the first time, try again ::
|
|
|
11-23-2005, 12:47 PM
|
#4 (permalink)
|
|
Regular Contributor
Join Date: Apr 2004
Location: Orange County, CA
Posts: 125
|
Damn those double equal marks! They've screwed me up before . . .
Wasn't thinking, sorry.
Unfortunately, it still doesn't work. Switch to POST (only had it on GET to make sure SOMETHING was being sent), and still naught. And it occurs to me that my method won't work anyway, since if they pick the first, third, and fifth options in the list, they'll show up (if it worked) as Procedure[0], Procedure[1], and Procedure[2], and not as 0, 2, and 4 as I would wish.
Gargh.
Any other ideas?
|
|
|
11-23-2005, 12:58 PM
|
#5 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,505
|
maybe you didn't refresh before you got my post? ... i took it a step further since i was playing with it anyway. maybe you already have it setup, but i'd recommend setting up a treatment array since you seem to use this on multiple pages.
here's some code that would make the web app a little easier to work with. keep in mind i don't know the scope of your project and take it with a grain of salt if you are already on a good way to code it.
PHP Code:
<?
// setup a treatment array and call it in an
// include file since it will be used on multiple pages
$treatments = array(
// just make the key (far left number) the same as the value
// to keep things simple. you can call a specific treatment by
// the key then. i.e.: echo $treatments[1]['text']
1 => array( 'value' => 1, 'text' => 'Acne, Acne Scar & Rosacea Treatment' ),
2 => array( 'value' => 2, 'text' => 'Second Treatment')
);
// make a function to print a single option
// this might also be in an include somewhere
function printOption($value, $text, $input_array) {
echo '<option value="{$value}" class="form_mult" ' .
(in_array($value, $input_array)?" selected":"") .
'>{$text}</option>\n';
}
// create a new variable for the procedures array
// this code from here down only on this page.
if (is_array($_GET['Procedures'])) {
$input_array = $_GET['Procedures'];
} else {
// make new array so our array
// function below does not break.
$input_array = array();
}
// now draw your form
?>
<form ..... >
<select name="Procedures_two" id="Procedures_two" multiple size="6">
<?
// loop through each treatment and print the option
foreach ( $treatments as $each ) {
printOption($each['value'], $each['text'], $input_array);
}
?>
</select>
</form>
:: p.s. edited the heck out of this one too  .. fixed ::
|
|
|
11-23-2005, 01:29 PM
|
#6 (permalink)
|
|
Regular Contributor
Join Date: Apr 2004
Location: Orange County, CA
Posts: 125
|
SDE-
Did you ever know that you're my hero?
-metazai
P.S. But you're not as quick on the draw as Redhead. Might want to work on that.
=+)
|
|
|
11-23-2005, 02:15 PM
|
#7 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,720
|
Quote:
|
P.S. But you're not as quick on the draw as Redhead. Might want to work on that.
|
Thats due to me, having nothing else todo than to scanvenge the net for someone needing my help 
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 05:36 PM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|