Forum Moderators: coopster

Message Too Old, No Replies

Pass variables to included page

         

loke

5:41 pm on Feb 4, 2004 (gmt 0)

10+ Year Member



I have 7 div boxes in my index.php
#searchbox inside #leftbox contains a searchform. When a search is submited, index.php reloads and variables are passed. (I checked, this step works)

If the search was initiated the reloaded index.php then includes a Search_and_display_script.php in #contentbox

But the variables is not passed (or available) to the included Search_and_display_script.php file, thus no results are displayed.

I know that The Search_and_display_script.php works fine independently, but not when included in index.php.

Any suggestions?

coopster

5:51 pm on Feb 4, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



If they are POSTed variables then they are in the global scope, available in the $_POST superglobal array. Are you using multiple forms?

loke

5:58 pm on Feb 4, 2004 (gmt 0)

10+ Year Member



Yes, I'm using multiple forms.
BTW I use PHP v. 4.3.4

coopster

6:23 pm on Feb 4, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



If you have one script that is setting variables in a form input control, and you want to use them in another form, they need to be present in that form as well. Just like when you pass form data from one page to another in a hidden field, cookie, session variable, etc.

loke

6:31 pm on Feb 4, 2004 (gmt 0)

10+ Year Member



apologize my bad english, I use multiple form fields, not multiple forms.
The searchform in the index.php, calls index.php (so it reloads), passes variables, but the variables are not accessable in the included search_and_display.php file.

coopster

8:51 pm on Feb 4, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



How are you accessing the variables in the included file? Through the $_POST superglobal? Remember, variables take on a different scope inside user-defined functions. If you simply look for the variable in the $_POST superglobal, you should be OK.

loke

10:46 pm on Feb 4, 2004 (gmt 0)

10+ Year Member



I found a way to pass the variables

<?
$searchresults = ('http//example.com/search_and_display_results.php?variable='.$variable1.'&variable='.$variable2);

include ($searchresults);
?>

Is this proper coding, or is there another way?.
Now I have to think about how I may get the $page value returned, so that the user can skip through all the search result pages.

Thanks for the assistance so far.

mep00

1:23 am on Feb 5, 2004 (gmt 0)

10+ Year Member



When you use an inlcude(), it's like you typed the contents directly into your code:

...your code...
?>
contents of included file
<?php
...rest of your code...

or:
inc.php:

<?php
echo $a;
$a = "five\n";
?>

index.php:

<?php
$a="four\n";
incluce("inc.php");
echo $a;
?>

The output would be:
four
five