Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder
    Join Date
    Sep 2007
    Posts
    238
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Get parent page - Cant use $_SERVER['PHP_SELF']

    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?

  • #2
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    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.
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

  • #3
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,335
    Thanks
    13
    Thanked 348 Times in 344 Posts
    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.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #4
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    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...

    Edit: 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.
    Last edited by Rowsdower!; 03-25-2010 at 01:25 PM.
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

  • #5
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,335
    Thanks
    13
    Thanked 348 Times in 344 Posts
    Quote Originally Posted by Rowsdower! View Post
    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.

    Quote Originally Posted by Rowsdower! View Post
    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.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #6
    Regular Coder
    Join Date
    Sep 2007
    Posts
    238
    Thanks
    9
    Thanked 0 Times in 0 Posts
    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.
    Code:
    <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:

    Code:
    <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.

    Quote Originally Posted by Rowsdower! View Post
    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?

  • #7
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,335
    Thanks
    13
    Thanked 348 Times in 344 Posts
    Quote Originally Posted by htcilt View Post
    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?
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #8
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    Quote Originally Posted by htcilt View Post
    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.
    Code:
    <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:

    Code:
    <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.
    Last edited by Rowsdower!; 03-25-2010 at 02:59 PM.
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

  • #9
    Regular Coder
    Join Date
    Sep 2007
    Posts
    238
    Thanks
    9
    Thanked 0 Times in 0 Posts
    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?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •