Results 1 to 8 of 8
  1. #1
    Join Date
    Oct 2005
    Posts
    5

    Unanswered: Problem w multiple java forms on same page

    A simple java form I use enables visitors to search multiple search engines by picking a search-site (such as google, etc.) from a pull-down menu and then entering the word they want to search in a window, then clicking a "search" button. It works fine, normally, but doesn't work when I have multiple variations of it on the same web page.
    Why won't it work unless there is only one instance of it on a webpage?
    I'm pretty new to java and html.

  2. #2
    Join Date
    Apr 2005
    Location
    Baltimore, MD
    Posts
    297
    Could you post some code?

  3. #3
    Join Date
    Oct 2005
    Posts
    5

    The code, isolated, is as follows:

    <html>
    <head>
    <script language="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>
    <tr>
    <table border="1" width="700" cellpadding="5">
    <td width="10%" valign="top">
    <font face="arial" font size=3>
    <form name="searchform" onSubmit="return dosearch();">
    Search: <BR>
    <select name="sengines">
    <option value="http://news.google.com/news?q=" selected>Google News</option>
    <option value="http://query.nytimes.com/search/query?query=">NY Times </option>
    <option value="http://www.dogpile.com/info.dogpl/search/news/">Dogpile News</option>
    <option value="http://news.search.yahoo.com/search/news?p=">Yahoo! News</option>
    <option value="http://newssearch.bbc.co.uk/cgi-bin/search/results.pl?scope=newsukfs&tab=news&q=">BBC</option>
    <option value="http://www.technorati.com/search/">Technorati Blogs</option>
    <OPTION VALUE="http://search.dmoz.org/cgi-bin/search?search=">dmoz.org</option>
    </select>
    <br>
    For:
    <input type="text" name="searchterms">
    <BR>
    <input type="submit" name="SearchSubmit" value="Search">
    </form>


    Thanks for your reply,
    Walbridge

  4. #4
    Join Date
    Apr 2005
    Location
    Baltimore, MD
    Posts
    297
    The code looked like it should function properly and sure enough, when I tested out your code it worked fine! Are you still having problems? If so, what exactly is happening as opposed to what's supposed to happen? The only thing I can think of right offhand is that you should make sure nothing else is named 'searchform.'

    Also, another potential problem...you should probably use a "safer" way to access the search form (rather than var sf=document.searchform;). I generally pass a reference to the form as a parameter to the javascript function...

  5. #5
    Join Date
    Oct 2005
    Posts
    5

    Thanks for your reply. But...

    what do you mean, exactly, by, "I generally pass a reference to the form as a parameter to the javascript function..."?
    Go to the website, newsrip.com, and you will see what I am trying to do: three forms, all identical scripts, except for the url's applied to each. If you click on one of the state newspaper directories - California, for instance - at newsrip.com you will see the form as it is supposed to work. (By the way, the reason newsrip's source code is all jumbled is because of my host, Homestead.com's, sitebuilding software, but i've tried the page in pure html and java on a non-Homestead page and it still doesn't work.)
    It would be a great help if you could suggest exactly which code in the script I should replace, and with what. Thanks very much for your time.
    Walbridge

  6. #6
    Join Date
    Apr 2005
    Location
    Baltimore, MD
    Posts
    297
    You have three forms with the same name "searchform." So when you call var sf=document.searchform; which form does it select? (I don't know off of the top of my head, but I can bet it's usually going to be the wrong one .) It is generally a bad idea to get elements by their name. If you must though, use IDs rather than names, and make sure they are unique.

    As far as passing in the form to the javascript, this is what I meant...
    Change your javascript function to:
    Code:
    function dosearch(sf) {
    var submitto = sf.sengines.options[sf.sengines.selectedIndex].value + escape(sf.searchterms.value);
    window.location.href = submitto;
    }
    And your form declarations to:
    Code:
    <form name="searchform1" onSubmit="dosearch(this);return false;">
    </form>
    ...
    <form name="searchform2" onSubmit="dosearch(this);return false;">
    </form>
    ...
    <form name="searchform3" onSubmit="dosearch(this);return false;">
    </form>
    That will get rid of the ambiguities to the form names and using them in your script.

    Also, one little nitpick...javascript is not the same as JAVA

  7. #7
    Join Date
    Oct 2005
    Posts
    5

    Thanks for the Javascript tips...

    I just got in late and will try that out tomorrow and play around with the script and see what happens. Thanks much for the info. Will post how it goes...
    W

  8. #8
    Join Date
    Oct 2005
    Posts
    5

    Took your suggestion...

    Works great! Thanks.
    W

Posting Permissions

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