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 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Aug 2011
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Add search term to form

    Hello,

    I have been working with different codes to get a search form working with a database.

    I wonder if someone with better expertise can assist.

    I have a search form based on drop down option values, what I would like to do is select a "company" and append the search term to the company's value.

    E.G the form would be something like:

    <option value="http://url?term=valueisfromsearch&folder=Company Name">Company</option>

    There will be a free text area which will allow users to input their search term but need this go feed into the above value of "valueisfromsearch".

    Upon submit, the user will be presented with their results in their browser.

    Is this possible mid-URL? I've seen info on Javascript Query String Parameters but don't really understand it that well.

    Thanks,

    Uzzi

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,156
    Thanks
    80
    Thanked 4,559 Times in 4,523 Posts
    You need to show your entire <form> and especially the existing JS code that utilizes that <option> value.

    *Probably* you don't need to work this hard, at all. But we can't tell unless we see what is being done with that <option>.

  • Users who have thanked Old Pedant for this post:

    uzziwozzi (08-02-2011)

  • #3
    New to the CF scene
    Join Date
    Aug 2011
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thank you for your response Old Pendant. I'll provide you with the code I'm using:

    <script type="text/javascript">
    function dosearch() {
    var sf=document.searchform;
    var submitto = sf.sengines.options[sf.sengines.selectedIndex].value + escape(sf.searchterms.value);
    window.location.href = submitto;
    return false;
    }
    </script>
    </head>

    <body>

    <form name="searchform" onSubmit="return dosearch();">
    Search:
    <select name="sengines">
    <option value="http://url?term=admin&folder=ACME">Search ACME</option>
    </select>
    For:
    <input type="text" name="searchterms">
    <input type="submit" name="SearchSubmit" value="Search">
    </form>

    When I hit submit, I can get to the ACME folder and search for the term "admin". But I want to change it so that when users search in the box "search terms", this value is replaced by the value after term?=

    I've used a sample code that I *thought* would work since the option values can be hard coded, but need the search term replaced which I think would requite scripting editing..

    Many thanks.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,156
    Thanks
    80
    Thanked 4,559 Times in 4,523 Posts
    How about this:
    Code:
    <script type="text/javascript">
    function dosearch() {
        var sf=document.searchform;
        var submitto = sf.sengines.value.replace( "$$$", escape(sf.searchterms.value) );
        window.location.href = submitto;
        return false;
    }
    </script>
    </head>
    
    <body>
    
    <form name="searchform" onSubmit="return dosearch();">
    Search: 
    <select name="sengines">
        <option value="http://url?term=$$$&folder=ACME">Search ACME</option>
        <option value="http://www.google.com/search?q=$$$">Search Google</option>
    </select>
    For: <input type="text" name="searchterms">
    
    <input type="submit" name="SearchSubmit" value="Search">
    </form>
    encodeURIComponent() is better than escape(), but for most purposes either one works.

  • Users who have thanked Old Pedant for this post:

    uzziwozzi (08-03-2011)

  • #5
    New to the CF scene
    Join Date
    Aug 2011
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Fantastic - that works perfectly

  • #6
    New to the CF scene
    Join Date
    Aug 2011
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I have an additional requirement. Is it possible to append an additional URL to the completed submitto if a tick box is checked?

    I need to add &searchsubfolders=true.

    I'm confusing my SQL codes and Javascript codes and I supect I would need a new variable calling on if the check box is ticked and somehow add &searchsubfolders=true to submitto... little help?

    Just a thought, I tried creating a new function that can be called on if the check box is enabled, but again, having troubles having this appended to sf.sengines.value or perhaps I can add it to submitto?..
    Last edited by uzziwozzi; 08-09-2011 at 11:45 AM. Reason: Extra thoughts added!

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,156
    Thanks
    80
    Thanked 4,559 Times in 4,523 Posts
    Easy peasy
    Code:
    <script type="text/javascript">
    function dosearch() {
        var sf=document.searchform;
        var submitto = sf.sengines.value.replace( "$$$", escape(sf.searchterms.value) );
        if ( sf.searchsubfolders.checked ) submitto += "&searchsubfolders=true"
        window.location.href = submitto;
        return false;
    }
    </script>
    </head>
    
    <body>
    
    <form name="searchform" onSubmit="return dosearch();">
    Search: 
    <select name="sengines">
        <option value="http://url?term=$$$&folder=ACME">Search ACME</option>
        <option value="http://www.google.com/search?q=$$$">Search Google</option>
    </select>
    For: <input type="text" name="searchterms">
    
    <label><input type="checkbox" name="searchsubfolders"/> Search sub-folders?</label>
    <input type="submit" name="SearchSubmit" value="Search">
    </form>


  •  

    Posting Permissions

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