views:

75

answers:

6

I have this page (A) that has an input field and once you submit it, it makes an AJAX call and displays the results. Now, I need a search field on another page (B) that once you submit it, it redirects you to the former page (A) with the results displayed as if you did it on the first page (A).

What would be the best way to handle this?

+1  A: 

You could make pageA.php take a query parameter containing the search keywords and if this parameter is assigned a value show the results based on this value. The results will be a simple include so that it could be reused for both the AJAX query and the normal query.

Darin Dimitrov
A: 

Have a single method that does the search functionality. This method can be called in two ways.. 1. Using Ajax 2. Using url parameters

When you are calling page A from page B you will pass the search query as url params.. so search is done and the page is displayed with the results.

When you search using page A.. the ajax method is called and a json/xml data is returned.

Basically, Your page just needs to have both the logic incorporated!

Abdel Olakara
A: 

With a single logic (if the ajax load time isn't a concern) you could do something like this (or any variation on it):

<?php 
$Term = (isset($_REQUEST['Term'])) ? $_REQUEST['Term'] : 'null'; 
// Optional   default term 
?>
<script>
    function ajax(term){
        if (term == null)
            return false;
    // ajax function for the search box
    // probably fired onclick/onsubmit currently?
    }

    $(document).ready(function(){
        ajax(<?=$Term?>);
    });
</script>

Alternatively you could file_get_contents("ajax_request.php?param=$PostValue") the ajax handler and output without an ajax call onload.

Mahdi.Montgomery
A: 

So you want this Ajax?

To do it with Ajax, I recommend you create a page ajax.php all it does is accept a query and spit out the result.

ajax.php?query=foo

and then output the result in a list or something...

Then use javascript to display it...

But that's only if you want to use ajax.

Quang
+1  A: 

I am unsure as to why you need to have two separate pages with this search. The easiest thing to do would just be to redirect the user from page B to page A with the search already filled in. In other words, page B really does nothing:

<?php
   /**
    * Page B
    */
   if(isset($_REQUEST['b_search'])) {
      header("Location: a_search.php?a_search=$_REQUEST[b_search]");
   }
   echo //page content
?>

<?php
   /**
    * Page A
    */
   $search = isset($_REQUEST['a_search']) ? $_REQUEST['a_search'] : null;
   //handle request for non-JS users, I hope
   echo //page content with search filled in
?>

/**
 * JS
 */
document.ready = function () {
   if (document.getElementById('search_field').value != null) {
      //make ajax call
   }
}
tandu
A: 
Roger Russel