Solved

Radio button onChange not working when trying to submit form

Posted on 2010-11-19
8
811 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 50

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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 50

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 50

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 50

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
javascript: add id amounts 5 47
Need Help adding an array and loop to a React file 7 28
How can i  prevent default  Submision? 6 28
Hide cell in a table 2 14
This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
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…

832 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