Forum Moderators: coopster
My site has a search function (Zoom Search) that allows users to click on links to sort the results By Relevance or By Date. Instead of the links I want to have a dropdown menu. In the Search.php page I included Select code to allow users to select from a dropdown menu (be advised, there are hundreds of lines of existing code on this page that appear before my added code. I use the variables generated in those existing lines in my redirect url). The code I added to the search.php page is:
$sorturl = 2;
print("<form action=\"Sort_URL.php\" method=\"get\"/>\n");
print("Sort By: ");
print("<select name='$sorturl'>");
print("<option value=\"0\">". $STR_SORTBY_RELEVANCE . "</option>");
print("<option value=\"1\">". $STR_SORTBY_DATE . "</option>");
print("</select>\n");
print("<input type=\"submit\" value=\"Go!\"/>\n");
print("</form>\n");
include 'Sort_URL.php';
The last line uses an "include" file (Surt_URL.php) to perform the redirect. I used a separate file for the redirect because I needed the redirect header to occur before any output. The include file (Sort_URL.php) has the following code:
<?php
switch ($sorturl) {
case 0:
$urlstr="search.php?zoom_query=$query_out&zoom_page=$page&zoom_per_page=$per_page&zoom_cat=$cat&zoom_and=$and&zoom_sort=$sorturl";
$host=$_SERVER['HTTP_HOST'];
$uri=rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$extra=$urlstr;
header("Location: [$host$uri...]
break;
case 1:
$urlstr="search.php?zoom_query=$query_out&zoom_page=$page&zoom_per_page=$per_page&zoom_cat=$cat&zoom_and=$and&zoom_sort=$sorturl";
$host=$_SERVER['HTTP_HOST'];
$uri=rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$extra=$urlstr;
header("Location: [$host$uri...]
break;
case 2:
break;
}
?>
When executed, it redirects to a page with empty search results because every variable has been dropped/disappears from the redirected url. All the variables ($query_out, $page, $per_page, $cat, $and, $sorturl) are missing in the browser's address bar. For example:
The redirected url should be:
[localhost...]
However, what I end up with is:
[localhost...]
All the variables are missing.
If I echo them from the include file the correct values appear, so the values appear to be passing from the search page to the include file. They just don't get passed in the redirect.
I appreciate any help you can provide.
(Note: I'm a CPA by trade, so if you see things here that look "whack", you'll know why.)
Dave
You can accomplish the same without using redirect on php.
Simply add a js function (in search.php) and call it through the form:
<script type=text/javascript>
function GoToSort(form){
var sortby = form.selectsort.options[form.selectsort.selectedIndex].value;
window.location = 'search.php?zoom_query=<?=$query_out?>&zoom_page=<?=$page?>&zoom_per_page=<?=$per_page?>&zoom_cat=<?=$cat?>&zoom_and=<?=$and?>&zoom_sort=' + sortby;
}
</script>
Then you have to rename your select:
<select name='selectsort'>
and change the button to call the js function:
<input type='button' value='Go!' onClick='javascript:GoToSort(this.form)'>
form action="Sort_URL.php" to
form action="search.php"?
The reason I wrote it to include the sort_url.php file was so I could be sure that nothing was ouput prior to the header. Quite a bit of output has already taken place prior to giving the user the option to sort the search results.
How would you (or can you) perform a redirect when the page requires output to be sent to the user first so they can make a selection on where to go (meaning I can't use output buffering)?
I thought using an include file would work with nothing in it except the redirect header. Here is what I have now:
In the search.php file (prior to this code, the file performs a site seach and outputs to the browser):
print("<form action=\"search.php\" //dropdown menu to allow user to select sort order
method=\"get\"/>\n");
print("Sort By: ");
print("<select name='sorturl'>");
print("<option value=\"0\">". $STR_SORTBY_RELEVANCE . "</option>");
print("<option value=\"1\">". $STR_SORTBY_DATE . "</option>");
print("</select>\n");
print("<input type=\"submit\" value=\"Go!\"/>\n");
print("</form>\n");
$urlparam = array('zoom_query'=>$query_out, //build the query string
'zoom_page'=>$page,
'zoom_per_page'=>$per_page,
'zoom_cat'=>$cat,
'zoom_and'=>$and,
'zoom_sort'=>$sorturl);
$parstr = http_build_query($urlparam);
include 'redirect.php';
In the redirect.php file (include file):
<?php
$host=$_SERVER['HTTP_HOST'];
$uri=rtrim(dirname($_SERVER['PHP_SELF']),'/\\');
header("Location: [$host$uri...]
?>
Maybe a redirect is not the way to approach this. I don't know. Any help would be greatly appreciated. I want to do this in php, not javescript. Especially now that I've spent so much time on it. I need to understand why it isn't working. I'm open to completely rewriting the script.
I'm running php 5.1 and IIS 6.0
Thanks
Could you explain in plain terms what you want to do? It seems from what is gathered so far that you are getting information from the database and then wanting to ask the user to sort the information differently ... is that correct?
Actually the information that is being pushed out to the browser is complete. Meaning the information is what the user requested (in my case, the results of a site search). The page is completely loaded.
The only thing I want to do is include a dropdown menu on the completed page to give the user an option to sort the information another way if they want (by Newest Entry or by Relevance, for instance), similar to what you might see on other sites when you do a site search.
IF the user does choose to sort the results differently, then based on the sort order they select in the dropdown menu, I want to resort it. And I guess that's the whole thing. I'm modifying third party search software, trying to get it to do something a little different. Maybe all I need to do is just query the database and reload the page in the order requested. I just assumed (being a n00b) that I needed to redirect.
If I'm correct, how would I initiate a new query and reload the page based on the user's selection in the dropdown menu?
Create the select list with the sort criteria in a <form>. When the user selects a different sort option and clicks "Submit" you run the same query but with an additional/different ORDER BY clause. Process the result set and display the data sorted in the new order.