Solved

Radio button onChange not working when trying to submit form

Posted on 2010-11-19
8
829 Views
Last Modified: 2012-05-10
Hi,

The scenerio is as follows:

The form is loaded with radio buttons prepopulated.
I need to the form to submit if the user selects a new option.

Example code:

<form action="submit.php" method="post">
<input type="radio" name="mybutton" value="0" onChange="this.form.submit();" />
<input type="radio" name="mybutton" value="1" onChange="this.form.submit();" />
<input type="radio" name="mybutton" value="2" onChange="this.form.submit();" />
</form>

What have I done wrong?

Thanks
0
Comment
Question by:jagku
  • 4
  • 3
8 Comments
 
LVL 13

Expert Comment

by:dsmile
ID: 34174811
Use onclick instead

<form action="submit.php" method="post">
<input type="radio" name="mybutton" value="0" onclick="this.form.submit();" />
<input type="radio" name="mybutton" value="1" onclick="this.form.submit();" />
<input type="radio" name="mybutton" value="2" onclick="this.form.submit();" />
</form>
0
 
LVL 51

Expert Comment

by:Ryan Chong
ID: 34174813
try use:

onClick="this.form.submit();"

instead?
0
 

Author Comment

by:jagku
ID: 34187524
Hi Guys,

I did try using onClick (actually, I should have mentioned that - sorry!).
However, the problem was that this was firing everytime the user clicked the radio button (even if it was already checked).

Thanks
Dipen
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 51

Expert Comment

by:Ryan Chong
ID: 34187668
Try:

<form action="submit.php" method="post">
<input type="radio" name="mybutton" value="0" onClick="if ((!this.form.mybutton[0].checked)&&(!this.form.mybutton[1].checked)&&(!this.form.mybutton[1].checked)) this.form.submit();" />
<input type="radio" name="mybutton" value="1" onClick="if ((!this.form.mybutton[0].checked)&&(!this.form.mybutton[1].checked)&&(!this.form.mybutton[1].checked)) this.form.submit();" />
<input type="radio" name="mybutton" value="2" onClick="if ((!this.form.mybutton[0].checked)&&(!this.form.mybutton[1].checked)&&(!this.form.mybutton[1].checked)) this.form.submit();" />
</form>
0
 
LVL 51

Expert Comment

by:Ryan Chong
ID: 34187672
Try:

<form action="submit.php" method="post">
<input type="radio" name="mybutton" value="0" onClick="if ((!this.form.mybutton[0].checked)&&(!this.form.mybutton[1].checked)&&(!this.form.mybutton[2].checked)) this.form.submit();" />
<input type="radio" name="mybutton" value="1" onClick="if ((!this.form.mybutton[0].checked)&&(!this.form.mybutton[1].checked)&&(!this.form.mybutton[2].checked)) this.form.submit();" />
<input type="radio" name="mybutton" value="2" onClick="if ((!this.form.mybutton[0].checked)&&(!this.form.mybutton[1].checked)&&(!this.form.mybutton[2].checked)) this.form.submit();" />
</form>
0
 

Author Comment

by:jagku
ID: 34187835
Hi Ryancys,

Will the form submit if value = 0 is checked when we reach this screen and click on any of the buttons?

What I need (and I am starting to think that it isn't possible as the onClick event appears to fire after the radio button is changed (not before)) is to make sure that the form isn't submitted if (and only if) the radio button hasn't been changed.  I only want it to submit if there has been a radio button change.

0
 
LVL 51

Accepted Solution

by:
Ryan Chong earned 500 total points
ID: 34188229
you can try something like this instead:

<form action="submit.php" method="post">
<input type="radio" name="mybutton" value="0" onClick="if (x!=0) {this.form.submit();} else {x=0;}" />
<input type="radio" name="mybutton" value="1" onClick="if (x!=1) {this.form.submit();} else {x=1;}" />
<input type="radio" name="mybutton" value="2" onClick="if (x!=2) {this.form.submit();} else {x=2;}" />
</form>
<script language="javascript">
      var frm = document.forms[0];
      var x = -1;
      if (frm.mybutton[0].checked) x = 0;
      if (frm.mybutton[1].checked) x = 1;
      if (frm.mybutton[2].checked) x = 2;
</script>
0
 

Author Closing Comment

by:jagku
ID: 34189618
thanks
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question