[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Radio button onChange not working when trying to submit form

Posted on 2010-11-19
8
Medium Priority
?
928 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 54

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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 54

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 54

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 54

Accepted Solution

by:
Ryan Chong earned 1500 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…
Suggested Courses

864 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