...

View Full Version : Get parent page - Cant use $_SERVER['PHP_SELF']



htcilt
03-25-2010, 01:09 PM
Hi,

I have an autosuggest script (autosuggest.php) running on a form input text field on my homepage (index.php).
I need to append the current page to the urls attached to each autosuggest option.

The plan is to place the page name on the end of the url to act as a reliable alternative to $_SERVER['HTTP_REFERER'].
I can then use the parameter passed via the query string to control what happens on the destination page.

I need to get the current page but I can't use $_SERVER['PHP_SELF'] because it returns autosuggest.php not index.php

Is there a way around this?

Rowsdower!
03-25-2010, 02:06 PM
You could send it as a $_GET variable along with the current string to search using javascript.

So, as the user types your javascript makes an AJAX call to autosuggest.php?current=XXXX&string=YYYY

You update "YYYY" on each keystroke and capture the value for "XXXX" using document.location.href --- I think something like that would work.

Dormilich
03-25-2010, 02:14 PM
you have to set the parameter yourself (i.e. along with the AJAX request, or as a hidden form field). could be done using JavaScript.

Rowsdower!
03-25-2010, 02:22 PM
Hmm, I liked the hidden input field idea at first glance, but when I think it through I don't think it would work since the form isn't actually being submitted (it's just being checked for auto-suggestions).

Plus setting the value for that hidden input value with PHP would still require the PHP_SELF method which I think was to be avoided. Although once in the form the javascript could pick that up much more easily than the document.location.href call...

Nevermind, we're skipping HTTP_REFERRER, not PHP_SELF. I'm a fan of the hidden input field now since the javascript capture would be much easier (cross-browser) that way. Still it's an AJAX issue but I think the hidden input field streamlines the process nicely.

Dormilich
03-25-2010, 02:27 PM
Hmm, I liked the hidden input field idea at first glance, but when I think it through I don't think it would work since the form isn't actually being submitted (it's just being checked for auto-suggestions).

the input field idea was mentioned, in case you submit a form, but I only concluded, that you use AJAX for the auto-suggest.


Plus setting the value for that hidden input value with PHP would still require the PHP_SELF method which I think was to be avoided.

why that? if you build your page (index.php) PHP_SELF certainly points to that.

htcilt
03-25-2010, 03:16 PM
Thanks for the replies.
Just to clarify...
The autosuggest doesn't submit the form, it just creates a list of hyperlinks to another page e.g.


<li><a href="details.php?id=1">Suggestion 1</a></li>
<li><a href="details.php?id=2">Suggestion 2</a></li>
<li><a href="details.php?id=3">Suggestion 3</a></li>

If need to get something like this:


<li><a href="details.php?id=1&from=index.php">Suggestion 1</a></li>
<li><a href="details.php?id=2&from=index.php">Suggestion 2</a></li>
<li><a href="details.php?id=3&from=index.php">Suggestion 3</a></li>

where 'from' is the current page.
But as the point where the anchors are generated is in autosuggest.php, PHP_SELF returns autosuggest.php not index.php.


You could send it as a $_GET variable along with the current string to search using javascript.

So, as the user types your javascript makes an AJAX call to autosuggest.php?current=XXXX&string=YYYY

You update "YYYY" on each keystroke and capture the value for "XXXX" using document.location.href --- I think something like that would work.

Rowsdower!,
I' not sure how to get client side document.location.href to html generated by server side php?

Dormilich
03-25-2010, 03:42 PM
where 'from' is the current page.
But as the point where the anchors are generated is in autosuggest.php, PHP_SELF returns autosuggest.php not index.php.

so, where does this index.php come from, if it is neither the page file itself, nor anything else?

Rowsdower!
03-25-2010, 03:53 PM
Thanks for the replies.
Just to clarify...
The autosuggest doesn't submit the form, it just creates a list of hyperlinks to another page e.g.


<li><a href="details.php?id=1">Suggestion 1</a></li>
<li><a href="details.php?id=2">Suggestion 2</a></li>
<li><a href="details.php?id=3">Suggestion 3</a></li>

If need to get something like this:


<li><a href="details.php?id=1&from=index.php">Suggestion 1</a></li>
<li><a href="details.php?id=2&from=index.php">Suggestion 2</a></li>
<li><a href="details.php?id=3&from=index.php">Suggestion 3</a></li>

where 'from' is the current page.
But as the point where the anchors are generated is in autosuggest.php, PHP_SELF returns autosuggest.php not index.php.



Rowsdower!,
I' not sure how to get client side document.location.href to html generated by server side php?

You wouldn't. You would use PHP_SELF inside of the actual page being visited and place the current page's location as a hidden input value with an id of "current_page_location" or something. That would be the PHP-generated part. This currently viewed page would then also have javascript on it. You have the javascript do something like from=document.getElementById('current_page_location').value; and bingo, you have your user's currently viewed page.

Now at this point I presume that when the visitor types in the search form you send the user's search form content to the autosuggest.php page using AJAX to get suggested search options. Well, when you send info to the autosuggest.php URL you would simply append the appropriate $_GET variable(s) needed to produce the desired result suggestion URL bases.

Without seeing your actual page at work I don't know exactly how you are calling up the page with AJAX, but instead of the javascript AJAX request being something like this:
http://www.mysite.com/autosuggest.php?search=some+search+data+here

It would look something like this:
http://www.mysite.com/autosuggest.php?from=directory/page.html&search=some+search+data+here

Then autosuggest.php just needs to read the variable $_GET['from'] and act accordingly. You would then have to make a small change to autosuggest.php so that it reads the new $_GET variable that passes along the user's current page, and then uses it for the base URL for the returned suggestions.

So in all, you add the javascript to capture the current page, make one small tweak to the autosuggest.php file, and then after that it's all automated.

htcilt
03-25-2010, 04:26 PM
I think I've found a solution.
I'll try to explain - its difficult to post any code as the pages are quite large and it would take quite some time to sort out.

There are 2 ways to search the database.
1) Autosuggest
2) Form Submission

The autosuggest links to details.php with the id of the record e.g. details.php?id=123

The search form uses sessions to carry over all the search parameters/filters to details.php

I've added the search term value (entered by the user) to the end of the url generated by the autosuggest e.g. details.php?id=123&searchfor=find this string

Then on details.php I have an isset for the session and one for searchfor

I generate a 'back to search results' link if the session is set and searchfor is empty i.e. user came from form submission. The link contains all the search parameters from the session.

If searchfor isn't empty (user came from autosuggest selection), I generate a 'search page' link i.e. search.php?searchfor=find this string
They then search for whatever the user typed it at the point of selecting an autosuggest.

Hope I've explain this clearly?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum