www.webdeveloper.com
Results 1 to 10 of 10

Thread: Using a hyperlink to submit a search query

  1. #1
    Join Date
    Oct 2006
    Location
    Ables Springs, Texas
    Posts
    139

    Using a hyperlink to submit a search query

    I have a form that submits a search query (I enter the search term and click submit). That is working just fine. However, what I need now is to submit that same search term using a hyperlink on a webpage instead of the form. For example, instead of entering "Widgets" in the search form, I provide a "Widget" hyperlink on my webpage.

    So, on my index.php page I would have something like this"
    Code:
    <p>Explore the wonderful world of <a href="something">Widgets</a></p>
    When someone clicks on that link, it submits the search term "Widgets" just as if it had been manually entered in a search form and I clicked "submit". How can I accomplish this?

  2. #2
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    660
    Use a 'get' format for your href in the anchor tag

  3. #3
    Join Date
    Mar 2012
    Posts
    1,394
    Just to expand on the above reply, an example with two parameters 'parm1' and 'parm2' would be:

    <a href="something?parm1=text&parm2=text">Widgets</a>

    Note: Certain characters, like '?', '&' and '=' must not be used in the text as they are reserved. These have to be substituted with '%' followed by their ascii code (in hexadecimal). E.g. Some common substitutions are:
    %25 = '%'
    %26 = '&'
    %2C = ','
    %2F = '/'
    %3A = ':'
    %3D = '='
    %3F = '?'

    Note: An easy way to do the conversion is to generate the url with an actual form using GET and copy the url it produces!

  4. #4
    Join Date
    Oct 2006
    Location
    Ables Springs, Texas
    Posts
    139
    Since php cannot be used in an iframe, would it be possible to automatically place the resulting url in an iframe src statement?

    Example: something like this on my webpage
    Code:
    <p class="bold"><a href="http://www.mysite.com/work/test.php?title=The Book">The Book</a></p>
    Then have test.php display the resulting page in an iframe (I want it in an iframe so I can add comment to the resulting page, such as links to other affiliate booksellers).

  5. #5
    Join Date
    Mar 2012
    Posts
    1,394
    I think you are going to need to use PHP or JavaScript to achieve that!

  6. #6
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    660
    You lost me with the iframe subject.

    good luck!

  7. #7
    Join Date
    Oct 2006
    Location
    Ables Springs, Texas
    Posts
    139
    FORGET THE IFRAME.

    Consider this: You have a form on your website that has a little box with a word or phrase in it, like "Widgets" and a "Submit" button. When someone clicks the submit button, they are directed to the results page. That setup uses "form method='post' action='search.php'" where the input name='search'.

    Now suppose, instead of a box with a word and a submit button, you want to accomplish the same thing with a content link, so that when you refer to "Widgets" in your content with a hyperlink, when someone clicks on that link, the same result is achieved.

    You replied above
    Use a 'get' format for your href in the anchor tag
    Great! It's been days now, and I still cannot figure out exactly how to do that. I've been to stackoverflow and many other sites but I'm still lost. Just using
    Code:
    <a href="http://www.mysite.com/search.php?name=widgets">Widgets</a>
    does nothing. Obviously something else must be changed. But what?

  8. #8
    Join Date
    Mar 2012
    Posts
    1,394
    Quote Originally Posted by jwgrafflin View Post
    Just using
    Code:
    <a href="http://www.mysite.com/search.php?name=widgets">Widgets</a>
    does nothing. Obviously something else must be changed. But what?
    See post #3. The easy way to get the correct syntax is to set up a form using method=GET. Once you have that working correctly, just copy the command line from the URL box in the browser.

  9. #9
    Join Date
    Oct 2006
    Location
    Ables Springs, Texas
    Posts
    139
    Are you saying set up a form on the page where the hyperlink is to be placed, or a separate page? Are you referring to the "http://..." in the browser address box as the command line? And where is that supposed to be placed? I have an existing form using method=post that does what I want if I place a search box on my website. Would the solution be so simple as removing the search box and changing the method on that form to GET?

  10. #10
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,241
    So, if I understand what you need, and if I were going for the easiest way (for me, anyway), I'd just use a wee bit of PHP:
    PHP Code:
    <?php $name "widgets"?>
    <a href="http://www.mysite.com/search.php?name=<?php 
    echo urlencode($name); ?>"><?php echo htmlentities(ucfirst($name)); ?></a>
    In search.php, your search term would be found in $_GET['name'].

    This way, $name can be dynamically set as/where/when needed in the calling page.
    Last edited by NogDog; 03-03-2014 at 01:39 PM.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles