Page 1 of 1

Displaying function results : Revisited & Simplified

Posted: Fri Jul 01, 2005 9:37 pm
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();
   
?>

Posted: Fri Jul 01, 2005 10:03 pm
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'

Posted: Fri Jul 01, 2005 10:25 pm
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 :)

Posted: Fri Jul 01, 2005 10:53 pm
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();}

?>