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

Calling Functions

Status
Not open for further replies.

tru2dalimit

Programmer
Joined
Apr 21, 2004
Messages
3
Location
US
Hi im new to PHP, and i wrote a piece of code that contains a drop down menu with three items and i wanted to retain the item selected when i come back. I used a session variable to retain the variable and created a fuction:

if(isset($HTTP_POST_VARS['Salutation']))
{
$Salutation = $HTTP_SESSION_VARS['Salutation'];
}

function match($str)
{
if($str == $Salutation)
{
return = 'SELECTED';
}
return '';
}

Now when i call the function within my form it doesn't read it:
<form action="samp.php" method="POST" name="application" id="application">
<SELECT NAME="Salutation">
<Option Value="Mr." match('Mr.') > Mr.</Option>
<Option Value="Ms." match('Ms.') > Ms.</Option>
<Option Value="Mrs." match('Mrs.') > Mrs.</Option>
</SELECT>

Any help is greatly appreciated
Thanks
 
I see several things wrong here.

PHP has to modes of operation, HTML-throughput mode and parse mode. Parse mode occurs only between <?php and ?> tags. Throughput mode occurs everywhere else.

It appears you are trying to call a function from within throughput mode.


Even if you were in parse mode, your code will not work. PHP cannot call a function from withing a string literal.

I recommend that you do something like:

Code:
<?php
print '
<form action="samp.php" method="POST" name="application" id="application">
	<SELECT NAME="Salutation">
		<Option Value="Mr."[red]' .[/red] match('Mr.')[red]. '[/red]> Mr.</Option>
		<Option Value="Ms."[red]' .[/red] match('Ms.')[red]. '[/red]> Ms.</Option>
		<Option Value="Mrs."[red]' .[/red] match('Mrs.')[red]. '[/red]> Mrs.</Option>
	</SELECT>
';

This example outputs your HTML from within a print statement. The string to be printed is a concatenation of string literals and function returns.



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Thanks
Just wondering is there any possible i can retain the item selected in another way
 
For example i select Mrs. and i hit submit and i go to another page which displays my selection, but now i want to change it, so i go back. What i want is to keep the drop down menu to select the item that i selected before (Mrs.), not the first item in the menu(Mr.). Like using Beans to remember what i selected.
Thanks
 
Like you said in your initial post, use session variables. Set a session variable that records that information and have your function, instead of accepting an input parameter against which to check whether to output "SELECTED", check against that session variable.



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top