Displaying function results : Revisited & Simplified

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

Displaying function results : Revisited & Simplified

Post by facets »

Hi All,

I didn't get any response from my previous post so I thought i'd simplify things.

Can anyone tell me why the results, in this case, 'echo "RESULTS"' is being displayed above the search button?

tia, will.

Code: Select all

<?

if (isset($_POST['searchNow'])) {fnSearchResults();}

function fnSearchResults() {
	echo "RESULTS";
}

function viewSearch() {
echo "<form method=\"post\" action=\"".$_SERVER['PHP_SELF']."\">\n";
echo "<input type=\"submit\" name=\"searchNow\" class=\"btn\" value=\"Search Now\">";
}
  
viewSearch();
   
?>
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

beacause that's where your function is.

if you want the results displayed somewhere else, you need to set a var and have the function "return" the value:

ex:

Code: Select all

function foo($bar){
   $var = "I am foo".$bar;
   return $var;
}
$bob = foo("lish");
echo $bob;
//yeilds 'I am foolish'
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

Post by facets »

closer, how do I only display this once the submit is executed.

Code: Select all

if (isset($_POST['searchNow'])) {fnSearchResults();}

function viewSearch() {
echo "<form method=\"post\" action=\"".$_SERVER['PHP_SELF']."\">\n";
echo "<input type=\"submit\" name=\"searchNow\" class=\"btn\" value=\"Search Now\">";
}
  
viewSearch();

function fnSearchResults() {
	$var = "<br><br>Results";
	return $var;
}

$bob = fnSearchResults();
echo $bob;
tia, Will.
PS. Sorry for the foolish questions :)
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

Post by facets »

This seems to work.

Code: Select all

<?

viewSearch(); 

function fnSearchResults() { 
    echo "RESULTS"; 
} 

function viewSearch() { 
echo "<form method=\"post\" action=\"".$_SERVER['PHP_SELF']."\">\n"; 
echo "<input type=\"submit\" name=\"searchNow\" class=\"btn\" value=\"Search Now\">"; 
} 
   
if (isset($_POST['searchNow'])) {fnSearchResults();}

?>
Post Reply