473,405 Members | 2,415 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Search Form Not Passing Checked Radio Button

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
5 4124
acoder
16,027 Expert Mod 8TB
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
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
16,027 Expert Mod 8TB
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
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
16,027 Expert Mod 8TB
You're welcome. Glad to be of assistance. Post again if you have any more questions.
Oct 3 '07 #6

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

Similar topics

1
by: sman | last post by:
Hi, I recently read this article on About.com on how to create required fields for a form: http://javascript.about.com/library/scripts/blformvalidate.htm Everything works great except that there...
2
by: jason | last post by:
The following (likely far from imperfect code), reports a value of NaN in the j4 display. I suppose the problem is I am not really passing the "checked" value of the radio button via .value ......
4
by: mitch-co2 | last post by:
What I am trying to do is when someone clicks on the YES radio button I want the text field called MYTEXT to equal the text field named DATE. The below code works as long as I do NOT UN-COMMENT...
1
by: ratnakarp | last post by:
Hi, I have a search text box. The user enters the value in the text box and click on enter button. In code behind on button click i'm writing the code to get the values from the database and...
2
by: NishSF | last post by:
Would anyone have any suggestions/javascript code so that if one clicks the Radio Button "Yes" below he has the option of selecting any of the six CheckBox below. If the user clicks on Radio Button...
1
by: Suma | last post by:
Hi, i have a problem with the controls .i have to validate Form1 Radio button option is checked or Not in the Form2. even after checking the radio button also in FOrm2 it is always taking as...
1
by: cglewis03 | last post by:
Hello, I am trying to build a search form with several different options to choose from. Currently it is set up to open within the same window if a single option is selected and open within a...
2
by: mars123 | last post by:
hi, I am facing a js error in my code, below is the prob. statement I have a radio2 javascript function as below, it works like this.. When a parent radio button is selected only one of its...
1
by: arggg | last post by:
I created a submit form that calls a javascript:AjAX Command that will call the data and submit it without have the page refresh. This works perfect in Firefox however in IE and Opera when the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.