-1

I have the following lines:

if(isset($_GET['search'])){
  $search_query = $_GET['user_query'];

The problem is that I am not trying to retrieve the get variable from this page, but rather from searchPage.php, which is a currently different page located within the same folder. So I would it to get the user_query variable from that page instead. Where for instance, in that page I have:

searchPage.php?user_query=microsoft&search=search

If you need any clarification, let me know.

5
  • I don't it was neccessary to post the full code because this question is quite general, and not too specific. essentially I am trying to retrieve a variable from another php page. Commented Mar 16, 2015 at 19:57
  • put a header on your search.php page and pass it to your page with the GET variables. Commented Mar 16, 2015 at 19:59
  • could i do this where in the actual search page i insert the get code as a function in the header, and include that function within my code on the other page Commented Mar 16, 2015 at 20:02
  • There is no such thing as "getting a variable from another page". What you want to do is propagate data from one page to another via a GET request. If you construct the correct URL it should work. It's worth noting that if you were using a development framework like Laravel there would be a built-in facility for handling this illustrated by many examples. Commented Mar 16, 2015 at 20:36
  • thanks for your response, and clarification. could you elaborate? Commented Mar 16, 2015 at 20:38

1 Answer 1

1

Seems weird but if you must do it like this whats wrong with just redirecting from searchPage.php to the page where you want to get parameter to be eg

In searchPage.php

 header("Location:the_other_script.php?user_query=".$_GET['search']);

And in the_other_script.php

 if(isset($_GET['user_query']){
    $user_query = $_GET['user_query'];
 }

Begs the question why not just do your logic in searchPage.php? If you want to separate your code out just use a function call in searchPage.php.

EDIT:

So you have another script file called functions.php.. In this case just require this file in searchPage.php like..

require_once('the/path/to/functions.php');

and in your searchPage.php

 if(isset($_GET['user_query']){
    $returned_from_function = a_function_from_your_functions($_GET['user_query']);
    //do some stuff
 }
 //do some other stuff

In functions.php

 function a_function_from_your_functions($user_query){ 
  //do what you need to do 
  return $something; //optionally 
  }
Sign up to request clarification or add additional context in comments.

13 Comments

Thanks for your suggestion. The problem is that I do not want the user to be redirected to another page. the search page is where all of the design element is. the function page is where all of the code is handled, and hence i do not want to redirect user to that page. All i want to grab the user_query (what they search) in the searchPage.php and use that variable information in the function page
as it stands, when use get['user_query'] as it is in the function page it tries to read it as such function.php/user_query= when that's irrealistic since that variable is only found in the search page
Your overcomplicating things I think. Check my edited answer. This is what functions were invented for
I accidentally added a new answer
You would either redirect the page as I initially suggested or make a request using PHP Curl. What reason could possibly discount using a require?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.