masnobi Posted April 15, 2013 Share Posted April 15, 2013 Hi all, I am trying to make sure that a user cannot move on unless they select a radiobutton, but unfortunately it's not working. I used same function with checkbox in my other page and it works perfect but i don't know why it is not working here Btw, this is not inside a form (as in other page I have used inside the form) PLEASE HELP echo "<td class='kooltd'><input type = 'radio' name = 'rFlight' value = '".$row['flight_id']."'></td>"; echo "<input type='submit' id='button1' name='booking' value='Make Booking for the Selected Flight' onclick='if(!this.form.rFlight.checked){alert('You must select a flight.');return false}'/></br>"; Quote Link to comment https://forums.phpfreaks.com/topic/276962-why-radiobutton-onclick-function-is-not-working/ Share on other sites More sharing options...
PravinS Posted April 15, 2013 Share Posted April 15, 2013 As this code is not in form, so "this.form" will not work, instead of it assign "id" attribute to radio button and use javascript "document.getElementById" function to check the radio button. Quote Link to comment https://forums.phpfreaks.com/topic/276962-why-radiobutton-onclick-function-is-not-working/#findComment-1424800 Share on other sites More sharing options...
Psycho Posted April 15, 2013 Share Posted April 15, 2013 Somewhat off topic, it's not clear from the sample code provided, but are you using a single radio button or doe the page actually have multiple options? If you only have one option, then you should be using a checkbox, not a radio button "group". More on topic, you should really dissociate the JavaScript code from the triggers.Your trigger should just call a function or, even better, use a framework like JQuery to dynamically set the trigger. Putting all that "logic" in your HTML code makes it difficult to debug and maintain your code. Also, you define the onclick trigger using single quotes and then you defined the message for the alert within single quotes. That will cause an error because the JavaScript parser will interpret the start of the alert message as the end of the onclick trigger definition. You need to use single quotes for one and double quotes for the other (or properly escape one set). Quote Link to comment https://forums.phpfreaks.com/topic/276962-why-radiobutton-onclick-function-is-not-working/#findComment-1424870 Share on other sites More sharing options...
nogray Posted April 15, 2013 Share Posted April 15, 2013 You are using single quote to enclose the onclick event and single quote on the alert message, so the browser sees your onclick code as onclick='if(!this.form.rFlight.checked){alert(' Try the following echo "<input type='submit' id='button1' name='booking' value='Make Booking for the Selected Flight' onclick=\"if(!this.form.rFlight.checked){alert('You must select a flight.');return false}\"/></br>"; Another note, you should put your form validation on the form "onsubmit" event. This way your code will work on all browsers Quote Link to comment https://forums.phpfreaks.com/topic/276962-why-radiobutton-onclick-function-is-not-working/#findComment-1424929 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.