Solved

Radio button onChange not working when trying to submit form

Posted on 2010-11-19
8
799 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
Comment Utility
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 49

Expert Comment

by:Ryan Chong
Comment Utility
try use:

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

instead?
0
 

Author Comment

by:jagku
Comment Utility
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
 
LVL 49

Expert Comment

by:Ryan Chong
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 49

Expert Comment

by:Ryan Chong
Comment Utility
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
Comment Utility
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 49

Accepted Solution

by:
Ryan Chong earned 500 total points
Comment Utility
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
Comment Utility
thanks
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…

728 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now