PHP and the "select" HTML value

He has: 1,380 posts

Joined: Feb 2002

Hey-yo! LOL, yet another PHP mishap has occured around me and the previously mentioned language...

I want to have the user choose a value from a "select" inside a form, and then based on that, they are redirected to somewhere else. But, it doesn't work. Here's the php:

<?php
$number
=documents.forms[0].elements['kidsnum[]'];

if (
$number==1) {
Header('Location: <a href="http://www.dscpool.com/signup1kid.html'" title="http://www.dscpool.com/signup1kid.html'">http://www.dscpool.com/signup1kid.html'</a>);
}

elseif (
$number==2) {
Header('Location: <a href="http://www.dscpool.com/signup2kid.html'" title="http://www.dscpool.com/signup2kid.html'">http://www.dscpool.com/signup2kid.html'</a>);
}

elseif (
$number==3) {
Header('Location: <a href="http://www.dscpool.com/signup3kid.html'" title="http://www.dscpool.com/signup3kid.html'">http://www.dscpool.com/signup3kid.html'</a>);
}

elseif (
$number==4) {
Header('Location: <a href="http://www.dscpool.com/signup4kid.html'" title="http://www.dscpool.com/signup4kid.html'">http://www.dscpool.com/signup4kid.html'</a>);
}

elseif (
$number==5) {
Header('Location: <a href="http://www.dscpool.com/signup5kid.html'" title="http://www.dscpool.com/signup5kid.html'">http://www.dscpool.com/signup5kid.html'</a>);
}
?>

and my select is named 'name="kidsnum[]"' and id'ed 'id="kidsnum[]"'

any ideas? thanks.

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

PHP is server-side. If I didn't know any better, I'd say it can't pull form values using Javascript/DOM. You'll have to send the data via querystring using JS, or submit the form with either GET (querysting again) or POST. You could store the selected value in a cookie too, but that'd be a waste of a cookie -- one of the above would do. Smiling

Renegade's picture

He has: 3,022 posts

Joined: Oct 2002

What you can do is have two files containing the following code(s ):

index.html

URL1
URL2
URL3

request.php

<?php
header
("Location: $_POST['gotopage']);
?>

I haven't tried out this but it should work something like that anyway.

Good Luck Laughing out loud

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

Quote: Originally posted by Renegade
What you can do is have two files containing the following code(s ):

index.html

URL1
URL2
URL3

request.php

<?php
header
("Location: $_POST['gotopage']);
?>

I haven't tried out this but it should work something like that anyway.

Good Luck Laughing out loud

- you don't necessarily have to have two files. You can send the data to the same page, but invoke the redirect script with a flag of some sort. Smiling

- perhaps it'd be wiser to use GET in this case, as you may want the redirection to be accessed via a URL that can be copied.

Smiling

JeevesBond's picture

He has: 3,956 posts

Joined: Jun 2002

Does this have to be done using PHP? Looks more like a job for JavaScript IMO

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

I was thinking Kyle had something more in mind with the PHP script. Yeah, if it's just a redirect, then JS could handle it. Unless you want to avoid depending on the client (JS disabled, etc).

Renegade's picture

He has: 3,022 posts

Joined: Oct 2002

You're right, you don't have to have two files. I just find it easier to use two files. Sticking out tongue

He has: 1,380 posts

Joined: Feb 2002

WOW...sorry i left out some info!

It's a form. I was using POST, and i did read on the php.net site that you can, http://www.php.net/manual/en/faq.html.php#faq.html.select-multiple

I just can't get it working. Any help via THIS part would be great. (I apologize for leaving out that info).

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

... is there a reason why you'd not use $_POST?

Your select form should be named "kidsnum" unless you enable multiple selections, and if you do that, I totally do not see how you'd do a mass redirect on that. You don't want to have people select more than one option, do you?

<?php
$number
= $_POST['kidsnum'];

switch (
$number) {
    case
'1':
   
header('Location: <a href="http://www.dscpool.com/signup1kid.html'" title="http://www.dscpool.com/signup1kid.html'">http://www.dscpool.com/signup1kid.html'</a>);
   
break;

    case
'2':
   
header('Location: <a href="http://www.dscpool.com/signup2kid.html'" title="http://www.dscpool.com/signup2kid.html'">http://www.dscpool.com/signup2kid.html'</a>);
   
break;

    case
'3':
   
header('Location: <a href="http://www.dscpool.com/signup3kid.html'" title="http://www.dscpool.com/signup3kid.html'">http://www.dscpool.com/signup3kid.html'</a>);
   
break;

    case
'4':
   
header('Location: <a href="http://www.dscpool.com/signup4kid.html'" title="http://www.dscpool.com/signup4kid.html'">http://www.dscpool.com/signup4kid.html'</a>);
   
break;

    case
'5':
   
header('Location: <a href="http://www.dscpool.com/signup5kid.html'" title="http://www.dscpool.com/signup5kid.html'">http://www.dscpool.com/signup5kid.html'</a>);
   
break;

    default:
   
header('Location: <a href="http://www.dscpool.com/nosignup.html'" title="http://www.dscpool.com/nosignup.html'">http://www.dscpool.com/nosignup.html'</a>);
   
break;
}
?>

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

**note: it would be better to do a form that submits and gives the new form with the right amount of "kids" spaces than have five pages of options.

He has: 1,380 posts

Joined: Feb 2002

fantastico!

now just a php based question, a syntax thing...

why use 'switch()' and cases, instead of if, elseif statements? just wondering...thanks alot!

He has: 1,380 posts

Joined: Feb 2002

yea thats what i did, it gives the proper amount of kids fields...already thought of that one!

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

switch is shorthand, essentially -- it skips until it finds the right case, then does that, rather than stepping through each set of instructions until it gets the right one.

http://ca2.php.net/switch

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

Quote: Originally posted by Eskater05
WOW...sorry i left out some info!

It's a form. I was using POST, and i did read on the php.net site that you can, http://www.php.net/manual/en/faq.html.php#faq.html.select-multiple

I just can't get it working. Any help via THIS part would be great. (I apologize for leaving out that info).

Fyi
variable = documents.forms[0].elements['var[]'];' from the link provided is for use with Javascript. Only client-side technologies and plugin-dependent scripts can access the Document Object Model (DOM) in that way. Smiling

Renegade's picture

He has: 3,022 posts

Joined: Oct 2002

There is a Javascript I have which will do what you want, would you like it?

Abhishek made it mostly I just use it Sticking out tongue

He has: 1,380 posts

Joined: Feb 2002

Oh, sorry. I didn't read i guess. Thanks though.

no thanks, PHP is fine. it all works. (PHP is easier to understand for me...I'm trying to learn, so everything is in PHP when possible).

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.