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

Passing < SELECT > option values to PHP?

Status
Not open for further replies.

ChrisMacPherson

Programmer
Jul 3, 2000
157
GB
How do you pass the value of a selected option, from a form in HTML, to it's PHP variable equivilant? Here is how I've set out my form...

<SELECT NAME=&quot;Informative&quot; ALIGN=right>
<OPTION value=&quot;10&quot;>10
<OPTION value=&quot;9&quot;>9
<OPTION value=&quot;8&quot;>8
<OPTION value=&quot;7&quot;>7
<OPTION value=&quot;6&quot;>6
<OPTION SELECTED value=&quot;5&quot;>5
<OPTION value=&quot;4&quot;>4
<OPTION value=&quot;3&quot;>3
<OPTION value=&quot;2&quot;>2
<OPTION value=&quot;1&quot;>1
</SELECT>

Can you spot any reason for this not to pass the value which is selected, to the variable $Informative ?

Thanks for any help
Chris MacPherson
thedamager@hotmail.com
Bring on the new Browza's!!

Learn/t/ing D\HTML, Javascript, Java, VB5-6, COBOL, Pascal
 
did you declare global $informative in the beginning of your function ?? it works for me ...
 
Is there any chance you can show me your code? I am passing the form data to a simple mail() function which will send the data to my email. I'm very new to PHP, I've read that you dont need to declare variables, all the other form elements seem to work and I have not declared them?

Chris MacPherson
thedamager@hotmail.com
Bring on the new Browza's!!

Learn/t/ing D\HTML, Javascript, Java, VB5-6, COBOL, Pascal
 
<?
function process_form() {
global $informative;

if ($informative == '10') { $message = 'Too much'; }
elseif ($informative == '1') { $message = 'Not enough'; }
else { $message = 'Fine.'; }
echo &quot;you selected : $informative <br>that is $message <br>&quot;;
}


 
Chris, you are right - normally you don't need to declare variables. But functions are different - any variables that you want to use in them from &quot;outside&quot; the function itself must be declared global in the function. I discovered this the hard way, same as you.
Hardy Merrill
Mission Critical Linux, Inc.
 
Hardy,
Thanks for your help, I think I need to explain my problem with some code as I'm not using any functions, but simply using the mail function. If you think I'm doing this completly
wrong please tell me, I'm getting to grips with PHP now so I might understand.

Here's the code I'm using for the mail function. (the HTML select statement is in the first post above)

mail(&quot;address@service.com&quot;,
&quot;Millenium Volunteers Website - Survey&quot;,
&quot;Millenium Volunteers Website Survey Response\n
============================================\n\n
Here are the statistics!!\n\n
Name : $Name\n
Informative : $Informative out of 10\n
Interesting : $Interesting out of 10\n
Easy to Use : $Easytouse out of 10\n
Easy to Read: $Easytoread out of 10\n
I found out about this site from : $From\n
The search Engine I found this site on was : $Other\n
&quot;);

In the email that is sent the $name, $from, $other variables work and show thier values, but the others dont. The ones that dont are the ones from select statements.

thanks if you can see a problem!
Chris MacPherson
thedamager@hotmail.com
Bring on the new Browza's!!

Learn/t/ing D\HTML, Javascript, Java, VB5-6, COBOL, Pascal
 
Wait a sec, in the above post I said

>>as I'm not using any functions, but simply using the mail function.

oops.

I still dont know why it wont work though as I have tried what you suggest.
I'll keep trying...
Chris MacPherson
thedamager@hotmail.com
Bring on the new Browza's!!

Learn/t/ing D\HTML, Javascript, Java, VB5-6, COBOL, Pascal
 
Copy and past this into a new file called mail.php

This seems to work on our system

Cheers

Jim M

<?
// mail.php test of mail with select
if (!$submit) {
?>
<form action=&quot;mail.php&quot; method=&quot;post&quot;>
<div align=&quot;center&quot;><table border=&quot;1&quot;><tr><td>Name</td><td><input type=&quot;text&quot; name=&quot;Name&quot;></td></tr>

<tr><td>Informative</td><td><SELECT NAME=&quot;Informative&quot; >
<OPTION value=&quot;10&quot;>10
<OPTION value=&quot;9&quot;>9
<OPTION value=&quot;8&quot;>8
<OPTION value=&quot;7&quot;>7
<OPTION value=&quot;6&quot;>6
<OPTION SELECTED value=&quot;5&quot;>5
<OPTION value=&quot;4&quot;>4
<OPTION value=&quot;3&quot;>3
<OPTION value=&quot;2&quot;>2
<OPTION value=&quot;1&quot;>1
</SELECT></td></tr>

<tr><td>Interesting</td><td><SELECT NAME=&quot;Interesting&quot; >
<OPTION value=&quot;10&quot;>10
<OPTION value=&quot;9&quot;>9
<OPTION value=&quot;8&quot;>8
<OPTION value=&quot;7&quot;>7
<OPTION value=&quot;6&quot;>6
<OPTION SELECTED value=&quot;5&quot;>5
<OPTION value=&quot;4&quot;>4
<OPTION value=&quot;3&quot;>3
<OPTION value=&quot;2&quot;>2
<OPTION value=&quot;1&quot;>1
</SELECT></td></tr>

<tr><td>Easy to Use</td><td><SELECT NAME=&quot;Easytouse&quot; >
<OPTION value=&quot;10&quot;>10
<OPTION value=&quot;9&quot;>9
<OPTION value=&quot;8&quot;>8
<OPTION value=&quot;7&quot;>7
<OPTION value=&quot;6&quot;>6
<OPTION SELECTED value=&quot;5&quot;>5
<OPTION value=&quot;4&quot;>4
<OPTION value=&quot;3&quot;>3
<OPTION value=&quot;2&quot;>2
<OPTION value=&quot;1&quot;>1
</SELECT></td></tr>

<tr><td>Easy to Read</td><td><SELECT NAME=&quot;Easytoread&quot; >
<OPTION value=&quot;10&quot;>10
<OPTION value=&quot;9&quot;>9
<OPTION value=&quot;8&quot;>8
<OPTION value=&quot;7&quot;>7
<OPTION value=&quot;6&quot;>6
<OPTION SELECTED value=&quot;5&quot;>5
<OPTION value=&quot;4&quot;>4
<OPTION value=&quot;3&quot;>3
<OPTION value=&quot;2&quot;>2
<OPTION value=&quot;1&quot;>1
</SELECT></td></tr>

<tr><td>I Learned about this Sight From</td><td><input type=&quot;text&quot; name=&quot;From&quot;></td></tr>

<tr><td>I Used This Search Engine</td><td><input type=&quot;text&quot; name=&quot;Other&quot;></td></tr>

</table></div>

<br>
<div align=&quot;center&quot;><input type=&quot;submit&quot; name=&quot;submit&quot;></div>


</form>
<?
} else {
$mailTo = &quot;address@service.com&quot;;
$mailSubject = &quot;Millenium Volunteers Website - Survey&quot;;

$mailBody = &quot;Millenium Volunteers Website Survey Response\n
============================================\n\n
Here are the statistics!!\n\n
Name : $Name\n
Informative : $Informative out of 10\n
Interesting : $Interesting out of 10\n
Easy to Use : $Easytouse out of 10\n
Easy to Read: $Easytoread out of 10\n
I found out about this site from : $From\n
The search Engine I found this site on was : $Other\n&quot;;

$mailHeader = &quot;From: survey@service.com&quot;;

$success = mail($mailTo, $mailSubject, $mailBody, $mailHeader);

if ($success){

echo &quot;Mail Sent&quot;;
}

}
?>
 
Jim,
I cant seem to get your code to work. I dont know why as I have studied it and followed all the logic. When I submit the form it simply replaces the old form with a new form, taking no notice of &quot; if (!submit) &quot;.

I have written some other scripts which use select boxes for input but not with the mail function. They seem to work o.k.
I'll see if I can do another script with a mail function.

Thanks for you help Chris MacPherson
thedamager@hotmail.com
Bring on the new Browza's!!
 
Hi Damager
Did you ever find out how to &quot;pass the value of a selected option, from a form in HTML, to it's PHP variable equivilant?&quot; because we are trying to call a function Onchange of a selected item.If you have any information with regards to this please let us know. Thanks
 
Hi Bombshells,

Em, (that's going back!) I think, yeah, I actually just went about it in a different way. Sorry, I bet that's really annoying. I didn't have enough time to dwell on the problem though. I will actually be going back to that problem in about a week or so, If you find the answer before then please post it here for us.

I know that it is only a problem when using a function, so I think it must be to do with global variables as iza's post at the top of this post explains, I couldn't get it to work before, but given the time (& understanding) that I'll have in a week I might suss it.

Till then good luck
Chris MacPherson
thedamager@hotmail.com
Bring on the new Browza's!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top