简体   繁体   中英

Validation HTML Radio Button

I am facing a problem with simple validating of radio button though database.

What I want to do is to just simply check whether radio button for each question is selected or not. I know the simple checking. But as I am using php variable name for every radio button name, it's very hard to convert to javascript variable and check. And I am stuck at this point.

<form id="formID" method="post" action="user_anger_quiz.php?rating_finished=true">

<input type="radio" name="<?php echo $question_form_name; ?>" value="<?php echo $question_id ?>,1"/>
<input type="radio" name="<?php echo $question_form_name; ?>" value="<?php echo $question_id ?>,2" />

<p id="anger_rating_submit">
<input type="submit" value="Rate my anger level"/></p>
  </form>

Any suggestions would be appreciated in advanced.

regarding to your comment:

I guess that you will use php to check it due to the fact that you just add php as a tag. In php you can use $_POST or $_GET on action page of your form to fetch the radio buttons.

Otherwise you can use javascript / javascript library

I think you're asking to make sure there is a radio button checked before submitting? Try the below untested function using jquery to see if there is a radio button selected. Run that before the form is submitted.

function somethingChecked()
{
    if (!$("input[name='<?php echo $question_form_name; ?>']:checked").val()) {
        alert('You need to check something');
        return false;
    } else {
        return true;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM