Jump to content

why radiobutton onclick function is not working?


masnobi

Recommended Posts

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>";
Link to comment
Share on other sites

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).

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.