forms - Validating radio buttons in php -
i'm new php , trying write code test whether or not user has clicked radio button in response survey question. there numerous radio buttons. if haven't clicked on 1 i'd issue error user. i've tried couple of approaches, haven't found works. here current code , error message get. php script, i've tried of 3 following examples:
.... if ($_post['degree_type'] == "ms"||"ma"||"mba"||"jd"||"phd") { $degree_type = ($_post['degree_type']); } else if ($_post['degree_type'] == null) { $errors[] = 'please select degree type.'; } if (isset($_post['degree_type'])) { $errors[] = 'please select degree type.'; } else { $degree_type= $_post['degree_type']; } if (array_key_exists('degree_type', $_post)) { $degree_type = ($_post['degree_type']); } else { $errors[] = 'please select degree type.'; } ....
here html, located in same page , below php.
<table> <tr> <td class="span6">what type of degree?</td> <td class="span6"> <input type="radio" name="degree_type" value="ma" <?php if (($_post['degree_type']) == 'ma') {echo 'checked="checked"';} ?> >ma <input type="radio" name="degree_type" value="ms" <?php if (($_post['degree_type']) == 'ms') {echo 'checked="checked"';} ?> >ms <input type="radio" name="degree_type" value="mba" <?php if (($_post['degree_type']) == 'mba') {echo 'checked="checked"';} ?> >mba <input type="radio" name="degree_type" value="jd" <?php if (($_post['degree_type']) == 'jd') {echo 'checked="checked"';} ?> >jd </td> </tr> etc....
i "undefined index" error on each of html lines referencing radio button. understand might easier in javascript, don't know js... detailed response appreciated!
thanks!
if you're getting undefined error on html page, can add isset()
check logic you're printing out value. e.g.:
<input type="radio" name="degree_type" value="jd" <?php if (($_post['degree_type']) == 'jd') {echo 'checked="checked"';} ?> >jd
becomes
<input type="radio" name="degree_type" value="jd" <?php if (isset($_post['degree_type']) && $_post['degree_type'] == 'jd') {echo 'checked="checked"';} ?>>jd
Comments
Post a Comment