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

help please a undefines variable issue

Status
Not open for further replies.

cs7536

Programmer
Apr 20, 2003
130
US
Basicly what is happening here is this. I have a dropdown menu and it echos a line based on the item selected from the menu by way of a simple if, else command. My problem is that the $select variable is the name of the dropdown menu. so when I access the file is works but an undefines variable message is displayed along with my result. I have tried to define $select but I can not get it to work, when I define it then the select will not call the command. Here is my script if someone can please take a look and assist me with a fix. The ral script calls from a database but I 've writen a simple if, else that works just the same.

Thanks in advance
Max



<form name=&quot;form1&quot; method=&quot;post&quot; action=&quot;&quot;>
<p>
<select name=&quot;select&quot; id=&quot;select&quot;>
<option value=&quot;nothing&quot; selected>Select One</option>
<option value=&quot;mobile&quot;>Montaro</option>
<option value=&quot;ford&quot;>Mustang</option>
<option value=&quot;&quot;></option>
</select>
<option selected></option>
<option value=&quot;select one&quot;> </option>
<option></option>
</p>
<p>&nbsp; </p>
<p>
<input type=&quot;submit&quot; name=&quot;Submit&quot; value=&quot;Submit&quot;>
</p>
</form>

<p>
<p>



<?php
print &quot;<p>&quot;;

$menu = $select;

if ($menu == 'mobile'){
echo &quot;I have a montaro truck&quot;;
}else if ($menu == 'ford'){
echo &quot;I own a white ford mustang&quot;;
}else{
echo &quot;I do not own a car at all&quot;;
}
?>

Nothing is hard when you realy want to learn it.

Max
 
What does the real error message say?
Is it a warning or an error?
What is the line number reported adn what is the real code 10 lines before and after?

This will help us to answer you better.

Also:
You rely on register_globals on. I advise: don't.
Use $_POST['select'] (>=PHP 4.1.0) or $HTTP_POST_VARS['select'] (PHP <4.1.0).
This will make your code portable and clearer.
 
I would also pick a different name for my select element.

I don't have proof that it could be causing problems, but &quot;select&quot; is a HTML reserved word. And it's always a good idea to avoid reserved words when naming things.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
I tried changing the select name to sports and still the same thing, the message that it is returning is as followed:

Notice: Undefined variable: sports in c:\program files\apache group\apache\htdocs\testphp\ifs.php on line 27
I do not own a car at all

it displays this message but then returns the correct result of my script.. then if I click the submit button it removes the Notice and starts working fine. It is just the initial access of the page.. almost like it returns that mesage cause the submit is not clicked or somthing.. I hope I am not confusing you guys.

Max

Nothing is hard when you realy want to learn it.

Max
 
Are you telling us that the script and the form are in the same page?
If so, that will not work.

PHP stands for Pre Hypertext Processor, i.e. the code is interpreted before the HTML is sent to the browser. That means you cannot look at PHP like at JavaScript, which runs client side.

Once the browser has the document, PHP is long gone....

That explains the error message:
The PHP interpreter runs the code in the page, and of course, initially there is no $sports defined since it is the initial page access.

If you want to have the script post to itself then you need to put in an if statement that contains code to be executed upon submission - not initial access.

Code:
<?php
if (isset($_POST['Submit'])){
  # now print out what was selected
}

 
Thanks alot that was a great deal of information. I did not know this and books do not tell you this. I have been working hard at learning php and I am proud at my progress sp far. and people like you and sliepnir are great help.


Thanks again

Nothing is hard when you realy want to learn it.

Max
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top