By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,930 Members | 633 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,930 IT Pros & Developers. It's quick & easy.

Search Form Not Passing Checked Radio Button

P: 3
Hello all,

I'm new to all this and I could really use some help. I've searched the web but cannot find an answer.

I have an HTML form with 3 radio buttons and a search field that calls a MySQL database using JavaScript to find a product either by Product ID, Description, or Both. Originally, the 'Both' radio button was "checked" but I now want the 'Product ID' button set as the default choice (as you can see from the code below).

-------------------------

Expand|Select|Wrap|Line Numbers
  1. <form action="<URLpath>" method="get" name="search">
  2.   <table>
  3.           <tr>
  4.             <td><a href="javascript:document.search.submit()" onClick="document.search.submit()"><img src="/buttons/search.gif" /></a></td>
  5.             <td><b> Search:</b></td>
  6.             <td><input type="text" name="searchtext" size="10" /></td>
  7.             <td rowspan="2">
  8.                 <input name="setSearchBox" type="radio" value="itemid" checked="checked" onClick="setSearchName(this.form,'itemid')" />             
  9.                 Product ID<br />
  10.                 <input name="setSearchBox" type="radio" value="description" onClick="setSearchName(this.form,'description')" />             
  11.                 Description<br />
  12.                 <input name="setSearchBox" type="radio" value="searchtext" onClick="setSearchName(this.form,'searchtext')" />             
  13.                 Both</td>
  14.             <td rowspan="2">
  15.                 <input type="submit" name="Submit" value="Go!" />
  16.                 <input type="hidden" name="c" value="search.htm" />
  17.                 <input type="hidden" name="ud" value="<! ud>" />
  18.                 <input type="hidden" name="storeid" value="<! storeid>" />
  19.             </td>
  20.           </tr>
  21.           <tr>
  22.             <td colspan="3" valign="top" class="searchbox" nowrap="nowrap"><p align="right">(Choose the appropriate<br />
  23.               radio button)</p>
  24.             </td>
  25.             </tr>
  26.         </table>
  27. </form>
  28.  
  29. <script type="text/javascript">
  30. function setSearchName(formHandle,boxName)
  31. {
  32.     formHandle.elements[0].name = boxName;
  33. }
  34. </script>
-------------------------

When displayed in a web page, the Product ID button is checked, but after entering an item number in the field and clicking 'Go!', it searches on "searchtext" not on "itemid". So apparently, using the checked tag does not get passed in the JavaScript, unless I'm missing something.

The odd thing is that the form will search on Product ID _if_ I click the Product ID radio button before clicking Go. But if the button is already checked, why should I have to click on it to make it work?

How can I get this form to search on "itemid" UNLESS the customer actually chooses another radio button?

Thank you,
Fran

P.S. If I've left anything out, please let me know.
Oct 2 '07 #1
Share this Question
Share on Google+
5 Replies


acoder
Expert Mod 15k+
P: 16,027
Welcome to TSDN!
When displayed in a web page, the Product ID button is checked, but after entering an item number in the field and clicking 'Go!', it searches on "searchtext" not on "itemid". So apparently, using the checked tag does not get passed in the JavaScript, unless I'm missing something.

The odd thing is that the form will search on Product ID _if_ I click the Product ID radio button before clicking Go. But if the button is already checked, why should I have to click on it to make it work?
Because the default value is searchtext and it's only changed when you click on the button.
How can I get this form to search on "itemid" UNLESS the customer actually chooses another radio button?
Using your method, just change "searchtext" to "itemid" for the text box.

The better method is just to use the value of the radio button (setSearchBox) in your server-side script.
Oct 2 '07 #2

P: 3
Hi acoder,

Thank you very much for getting back to me, and for the welcome. It appears to be working!

Welcome to TSDN!
Because the default value is searchtext and it's only changed when you click on the button.
Using your method, just change "searchtext" to "itemid" for the text box.
I'm assuming you meant for me to replace the name parameter like this?
Expand|Select|Wrap|Line Numbers
  1. <input type="text" name="itemid" size="10" />
I didn't realize that was the default value - I thought it was just the name for the text field. =:\

The better method is just to use the value of the radio button (setSearchBox) in your server-side script.
Could you elaborate on this? By "server-side script" do you mean the "function setSearchName(formHandle,boxName)" at the bottom of my first post? How would I do this? Also, why would this be a better method?

Thanks again,
Fran
Oct 2 '07 #3

acoder
Expert Mod 15k+
P: 16,027
I'm assuming you meant for me to replace the name parameter like this?
Expand|Select|Wrap|Line Numbers
  1. <input type="text" name="itemid" size="10" />
I didn't realize that was the default value - I thought it was just the name for the text field. =:\
It is the name for the text field, but your code changes the name depending on the radio button that is currently selected.
Could you elaborate on this? By "server-side script" do you mean the "function setSearchName(formHandle,boxName)" at the bottom of my first post? How would I do this? Also, why would this be a better method?
Instead of depending on the text box name, just use the value of the radio button. By server-side script, I mean the script that receives the input and does the searching, e.g. a PHP or ASP script. You would get the value of a radio button as you would a text box, by its name.

It's better because it doesn't depend on the JavaScript to change the name of a text box which looks messy. You'll probably have to change your code as a result, but the code should be easier.
Oct 3 '07 #4

P: 3
It is the name for the text field, but your code changes the name depending on the radio button that is currently selected.
Thank you for the explanation. I understand it now.

Instead of depending on the text box name, just use the value of the radio button. By server-side script, I mean the script that receives the input and does the searching, e.g. a PHP or ASP script. You would get the value of a radio button as you would a text box, by its name.
I take it that you mean the code of our cart. We use SurfShopPro which is an excellent cart, but it's written in Perl and I don't no anything about Perl. I'll post that to the SurfShop forum to see what can be done.

Thanks again for everything. You really helped me out.

Regards,
Fran
Oct 3 '07 #5

acoder
Expert Mod 15k+
P: 16,027
You're welcome. Glad to be of assistance. Post again if you have any more questions.
Oct 3 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.