Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 11-23-2005, 11:52 AM   #1 (permalink)
metazai
Regular Contributor
 
Join Date: Apr 2004
Location: Orange County, CA
Posts: 125
metazai is on a distinguished road
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?
metazai is offline   Reply With Quote
Old 11-23-2005, 12:16 PM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,720
redhead is on a distinguished road
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
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 11-23-2005, 12:35 PM   #3 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,505
sde is on a distinguished road
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 ::
sde is offline   Reply With Quote
Old 11-23-2005, 12:47 PM   #4 (permalink)
metazai
Regular Contributor
 
Join Date: Apr 2004
Location: Orange County, CA
Posts: 125
metazai is on a distinguished road
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?
metazai is offline   Reply With Quote
Old 11-23-2005, 12:58 PM   #5 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,505
sde is on a distinguished road
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']
  
=> array( 'value' => 1'text' => 'Acne, Acne Scar & Rosacea Treatment' ),
  
=> 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 ::
sde is offline   Reply With Quote
Old 11-23-2005, 01:29 PM   #6 (permalink)
metazai
Regular Contributor
 
Join Date: Apr 2004
Location: Orange County, CA
Posts: 125
metazai is on a distinguished road
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.

=+)
metazai is offline   Reply With Quote
Old 11-23-2005, 02:15 PM   #7 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,720
redhead is on a distinguished road
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
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Form questions JDE HTML, XML, Javascript, AJAX 2 10-29-2005 01:52 AM
Child Form to Parent Form WmFenn MS Technologies ( ASP, VB, C#, .NET ) 2 07-20-2005 08:10 AM
Passing Values from Popup Form to Main Form chrislopezz PHP 7 03-28-2005 12:45 PM
EMERGENCY: Dynamic form processing DavH27 PHP 8 10-27-2004 07:52 PM
Passing form data to PHP with Javascript bdl PHP 5 07-03-2002 10:18 AM


All times are GMT -8. The time now is 05:36 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting