Current location: Hot Scripts Forums » Programming Languages » PHP » Malfunctioning switch statement and processing


Malfunctioning switch statement and processing

Reply
  #1 (permalink)  
Old 10-19-03, 09:17 PM
cstallins's Avatar
cstallins cstallins is offline
Newbie Coder
 
Join Date: Oct 2003
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Malfunctioning switch statement and processing

I've coded the HTML for a form to input some basic stuff, including a dropdown (select) box with five choices. The back-end PHP code is supposed to echo some short text to the screen, depending on which of the five selections the visitor made. I know that, since I have certain settings in my php.ini file, I cannot use the simple form variable names in my PHP code; in the switch statement, I have tried case "a", for example, but it produces an error statement. I have to use the $_POST['variableName'] array to access my form variables. That has worked well for all my form processing until I wrote this select box and switch statement. What I have currently written (see below) at least echos something to the monitor, but no matter which of the five values I select in my dropdown box, pressing the Submit button always returns the first value, "Regular customer", on the processed page. Any ideas why?

I'm running WinXP with Apache.
Thank you for any time and help you can offer.
Curtis

My form select box looks like this:

<td>
<select name="find">
<option value="a">I'm a regular customer</option>
<option value="b">TV advertising</option>
<option value="c">Phone directory</option>
<option value="d">Word of mouth</option>
<option value="e">Other</option>
</select>
</td>


while the processing PHP code looks like this:

<?

(additional code...)

switch($find)
{
case $_POST['a'] :
echo "<p>Regular customer</p>";
break;
case $_POST['b'] :
echo "<p>Customer saw television ad</p>";
break;
case $_POST['c'] :
echo "<p>Customer saw telephone ad</p>";
break;
case $_POST['d'] :
echo "<p>Customer referred by word of mouth</p>";
break;
case $_POST['e'] :
echo "<p>Customer referred by other</p>";
break;
}

(additional code...)

?>
__________________
CurtFun
PHP Novice Extraordinaire
...Kurtz got off the boat...
Reply With Quote
  #2 (permalink)  
Old 10-20-03, 04:20 AM
NeverMind's Avatar
NeverMind NeverMind is offline
Community VIP
 
Join Date: Aug 2003
Location: K.S.A
Posts: 2,257
Thanks: 0
Thanked 2 Times in 1 Post
yeah you have a very clear mistake , man
you were telling the parser to look for a varibale called 'a' in first case and 'b' in second .. etc
while you should tell the parser something like this :
PHP Code:

switch($_POST['find'])

{
case 
'a' :
echo 
"<p>Regular customer</p>";
break;
case 
'b' :
echo 
"<p>Customer saw television ad</p>";
break;
case 
'c' :
echo 
"<p>Customer saw telephone ad</p>";
break;
case 
'd' :
echo 
"<p>Customer referred by word of mouth</p>";
break;
case 
'e' :
echo 
"<p>Customer referred by other</p>";
break;

try this and it should work ,,
when typing $_POST['whatever'] , it means there is a variable passed via form in POST method called 'whatever' .. in your case it's 'find' ..
and you assume a value in every case ..
so I wrote ( case 'a' ) means if the value of $_POST['find'] is the string 'a' , excute this line ..
hope I was clear
__________________
PHPSimplicity
We don't need a reason to help people - Zidane [FF9]
Reply With Quote
  #3 (permalink)  
Old 10-20-03, 08:48 PM
cstallins's Avatar
cstallins cstallins is offline
Newbie Coder
 
Join Date: Oct 2003
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
NeverMind, thank you for the great help.

NeverMind, you were dead on.
Thank you for the great clue. I forgot that select boxes are just another form element, and will require the $_POST (or $_GET) treatment, just like all my other form variables.

Everything is as it should be.
Curtis

Nevermind
Nirvana
Sep. '91
__________________
CurtFun
PHP Novice Extraordinaire
...Kurtz got off the boat...
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump


All times are GMT -5. The time now is 07:12 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.