SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Mar 2005
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question HTML forms - preset to add text to the input

    I'm wanting to make a search function in a limited way.

    The following code sends the input text to a page where the script processes it, no problem

    Code:
    <form action="search.php">
      <input type="text" name="site_search" />
      <input type="submit" value="Search" />
    </form>
    I would like to have text automatically added to the input.

    Example: I have a site I want to be only about things that are red.

    Someone inputs "table" and the text "red+table" is sent to the search page.

  2. #2
    SitePoint Evangelist TommiChi's Avatar
    Join Date
    Oct 2008
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you want to avoid using javascript, one quick and dirty solution would be to add a hidden field that contains the keyword you need. Your server side script should then be written to concatenate whatever is in your hidden field with the value of the input field.

    Another option is to replace your submit button with a regular button, and on click add the keyword you need to the value of the input field (yourfield.value = 'red '+ yourfield.value; ) before executing your submit() function.

    Yet another option is to make an AJAX POST request, in which you have total control of the values being passed to the server side script.

  3. #3
    SitePoint Member
    Join Date
    Mar 2005
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Still trying to figure this out with html forms since I'm pretty much clueless in javascript.

    I'm getting closer using the below. It pre-loads the form with Red, but would be better to keep that hidden if possible.

    Code:
    <form action="search.php">
    <Input value = 'Red '+ Product Search.value type="text" name="site_search"; />
      <input type="submit" value="Red Search" />
    </form>

  4. #4
    SitePoint Member
    Join Date
    Mar 2005
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I had given up on this and last night it came to me in a dream. No kidding!

    Simple solution really. I just put the full search url in the form plus the added term in the input and it works.

    Code:
    <form action="http://www.mysite.com/shop/">
    <input type="text" name="site_search=red+" />
      <input type="submit" value="Search" />
    </form>
    And a search input for flowers shows results for red flowers.

    Only thing is the url is a bit messy looking like this...

    Code:
    http://www.mysite.com/shop/?site_search%3Dred%2B=flower
    but it works.
    Last edited by jazzylee77; Mar 3, 2011 at 14:38. Reason: mistake in code


Tags for this Thread

Bookmarks

Posting Permissions

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