JAVA

NAVIGATION
CATEGORIES
REFERRENCE
LINKS
  • Problem w multiple java forms on same page

    7 answers - 468 bytes - related search similar search Add To My Delicious Add To My Stumble Upon Add To My Google Mark Add To My Facebook Add To My Digg Add To My Reddit

    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.
  • No.1 | | 25 bytes | |

    Could you post some code?
  • No.2 | | 1567 bytes | |

    <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
  • No.3 | | 578 bytes | |

    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...
  • No.4 | | 828 bytes | |

    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
  • No.5 | | 1178 bytes | |

    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:
    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:
    <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 ;)
  • No.6 | | 160 bytes | |

    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
  • No.7 | | 26 bytes | |

    Works great! Thanks.
    W

Re: Problem w multiple java forms on same page


max 4000 letters.
Your nickname that display:
In order to stop the spam: 3 + 3 =
QUESTION ON "JAVA"

DATABASE TECH