Forum Moderators: coopster

Message Too Old, No Replies

Why are my $variables being dropped in a redirect?

Included file should redirect to page based on user's input

         

SirDaver

5:28 pm on May 25, 2006 (gmt 0)

10+ Year Member



OK, I am at wits end. I have been searching for over a week now for an answer. I've even paid for a subscription here to get some help. I have not been able to find a solution that seems to apply to my issue, but then again, since I'm new to php, I'm not able to make the right connections to adapt posted solutions to my situation:

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

coopster

5:49 pm on May 25, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, SirDaver.

Try exiting right after your redirection header functions otherwise processing continues.

header("Location: http://$host$uri/$extra"); 
exit;
break;

Omut

5:50 pm on May 25, 2006 (gmt 0)

10+ Year Member



1) change:
form action="Sort_URL.php"
to
form action="search.php"

2) delete the line:
include 'Sort_URL.php';
and drop file 'Sort_URL.php' to Recycle Bin

[edited by: jatar_k at 3:00 am (utc) on May 26, 2006]

coopster

5:53 pm on May 25, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, Omut.

I was thinking along the same lines at first but then figured that output buffering was on. If not, SirDaver, you will run into issues as you cannot output anything, no html, no blanks, nothing, prior to a header().

dreamcatcher

5:55 pm on May 25, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Also, as an added rule of thumb, set your error reporting to E_ALL to see if that picks up on anything with the variables.

error_reporting(E_ALL);

and welcome to Webmaster World SirDaver. :)

dc

Omut

6:03 pm on May 25, 2006 (gmt 0)

10+ Year Member



$sorturl = 2;
print("<select name='$sorturl'>");

I cannot imagine what was espected, but I am sure it is not that is actually send to browser.

san_garrafa

9:23 pm on May 25, 2006 (gmt 0)

10+ Year Member



SirDaver:

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)'>

SirDaver

12:34 am on May 26, 2006 (gmt 0)

10+ Year Member



Thanks for the quick response, folks. I appreciate the input. I'll try your suggestions and let you know how it turns out.

[edited by: coopster at 2:02 am (utc) on May 26, 2006]
[edit reason] Misunderstanding [/edit]

SirDaver

7:08 pm on May 31, 2006 (gmt 0)

10+ Year Member



Omut, how would you incorporate a header to perform the redirect if you change:

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.

SirDaver

6:20 pm on Jun 6, 2006 (gmt 0)

10+ Year Member



Well, I've tried some of the suggestions above and still have not had any success. I guess the question is this:

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

coopster

7:33 pm on Jun 6, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



No, you cannot redirect after outputting to the browser. Let's stop and think about that a moment. Why would you push information out to the browser only to have it partially complete and then redirect the user to a different page before the page is loaded? That is why it is not allowed.

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?

SirDaver

8:40 pm on Jun 6, 2006 (gmt 0)

10+ Year Member



Thanks, Coopster.

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?

coopster

12:39 am on Jun 7, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You've got it, just as you stated.

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.