Forum Moderators: open

Message Too Old, No Replies

Custom Search Engine

error in my javascript

         

CNibbana

5:53 am on Dec 14, 2003 (gmt 0)

10+ Year Member



I'm trying to create a custom search engine for my site that calls the form action url depending on the radio button selected. I know the error is in my Javascript, most likely here: <form onsubmit="formaction(this)">

Can anyone help? TIA

<input type="radio" name="formaction" value="1" onClick="this.form.action = 'http://www.google.com/search'; name='f'" checked="checked"> Google Search<br>
<input type="radio" name="formaction" value="2" onClick="this.form.action = 'http://images.google.com/images'; name='f'"> Google Image Search<br>
<input type="radio" name="formaction" value="3" onClick="this.form.action = 'http://www.google.com/custom'; method='get'"> Site Search<br>
<input type="radio" name="formaction" value="4" onClick="this.form.action = 'http://ie.search.msn.com/en-us/srchasst/srchasst.htm'"> MSN Search
<form onsubmit="formaction(this)">
<input type="text" maxlength=255 size=17 name=q value="">
<input type=hidden name=ie value="UTF-8">
<input type=hidden name=oe value="UTF-8">
<input type=hidden name=hl value=en>
<input type="submit" name="sa" value="Search">
<input type="hidden" name="cof" value="AH:center;S:http://mysite.com
<input type="hidden" name="domains" value="mysite.com">
</form>

CNibbana

8:45 am on Dec 14, 2003 (gmt 0)

10+ Year Member



I realized that the radio buttons needed to be put inside the form and now the search works, however the default radio button which is checked via 'checked="checked"' requires a click before it will actually work. I assume because of the Javascript 'onClick'. Is there a way to set a default form url that will work if none of the radio buttons are checked?

CNibbana

12:22 am on Dec 16, 2003 (gmt 0)

10+ Year Member



I tried changing the first radio (the default checked) to "onSubmit" but that didn't work. I also tried "onLoad" in the body, but since it's not in the form it didn't work either.

I also tried assigning a variable to 'this' before the radio buttons.

<script type="text/javascript"> var this.form.action='http://www.google.com/search'; name='f'; target='_main';</script>

Someone please help?

Purple Martin

3:10 am on Dec 16, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<form onsubmit="formaction(this)">

This line says that when the user clicks the submit button it will call a JavaScript function called formaction. Look for a bit of code something like this:

<script>
function formaction(form) {

// ... some code goes in here...

}
</script>

The function will return either true or false depending on whether it decides the form is ready to submit (it probably does some field validation).

If you can't work out what your formaction function is doing, post it here :)

CNibbana

3:20 am on Dec 16, 2003 (gmt 0)

10+ Year Member



I actually removed 'formaction'. Is that causing me a problem? This is what I have right now (changed from above), which works as long as you click on a radio button:

<form onsubmit="(this)">
<input type="radio" name="sitesearch" value="" checked="checked" onClick="this.form.action='http://www.google.com/search'; name='f'; target='_main'"> Google Search<br>
<input type="radio" name="sitesearch" value="" onClick="this.form.action='http://images.google.com/images'; name='f'; target='_main'"> Google Image Search<br>
<input type="radio" name="sitesearch" value="mysite.com" onClick="this.form.action='http://www.google.com/custom'; method='get'; target='_main'"> My Site Search<br>
<input type="radio" name="sitesearch" value="" onClick="onsubmit='return CheckMT(this.q, this.q.value, this)'; this.form.action='http://search.msn.com/spresults.aspx'; method='get'; name='STWF'; target='_self'"> MSN Search<br><br>
<input type="hidden" name="form" value="IE4">
<input type="hidden" name="hl" value="en">
<input type="hidden" name="cof" value="AH:center;S:http://mysite.com;AWFID:45f8820be9938ed4;">
<input type="hidden" name="domains" value="mysite.com">
<input type="text" maxlength="255" size="15" name="q" VCARD_NAME="SearchText" class="qform" value="">
<input type="submit" name="sa" value="Go">
</form>

DrDoc

4:06 am on Dec 16, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can, in fact, remove the
onsubmit
attribute alltogether! And, just to make sure it works before a radio button is clicked, just set a default attribute:

<form action="http://www.google.com/search">

That's the default radio button anyway :)

CNibbana

11:21 pm on Dec 16, 2003 (gmt 0)

10+ Year Member



THANK-YOU DrDoc!

Worked like a charm. Just the thing I was looking for. I changed the first line to <form action='http://www.google.com/search' name='f' target='_main' onsubmit="(this)"> to accomodate the default and the onsubmit and it worked beautifully.