please help why everytime when I run it it has this undefined error

       (     Notice: Undefined index: u_role in C:\xampp\htdocs\mysite\membership.php on line 136

/> Member Notice: Undefined index: u_role in C:\xampp\htdocs\mysite\membership.php on line 137
/> Administrator)

        <td>User Role:*</td>
        <td><input type="radio" name="u_role"  value="Member" <?php if($_POST['u_role']=="Member"){echo"checked='checked'";}?> /> Member
         <input type="radio" name="u_role"  value="Administrator" <?php if($_POST['u_role']=="Administrator"){echo"checked='checked'";}?> /> Administrator </td>
                </tr> 

                 <tr>

               /*  and also the gender */
                 <tr>
        <td>Gender:*</td>
        <td><select name="gender">
            <option value=""></option>
            <option value="Male"> <?php if($_POST['gender']=="Male"){echo "selected='selected'";}?>Male</option>
            <option value="Female"> <?php if($_POST['gender']=="Female"){echo "selected='selected'";}?>Female</option>
        </select> </td>
        </tr>
Member Avatar for LastMitch

please help why everytime when I run it it has this undefined error

@Ann Krizette

You need to add isset() function to see whether u_role to see whether it exists.

For example like this:

if (isset($_POST["u_role"]) && !empty($_POST["u_role"])) {
echo "checked";    
}else{  
echo "not checked";
}
Member Avatar for diafol

AS LM says. However, as you're using a radiobutton, u_role should always be set, so you need to set one of them to 'checked' as default. So LM's code will probably set the last radiobutton regardless of which one was set on form submit because it doesn't check for the value.

<?php
$memCheck = (!isset($_POST["u_role"]) || $_POST["u_role"] == 'Member')) ? "  checked = 'checked'" : "";
$admCheck = (isset($_POST["u_role"]) && $_POST["u_role"] == 'Administrator')) ? "  checked = 'checked'" : "";
?>


 <td><input type="radio" name="u_role"  value="Member"<?php echo memCheck;?> /> Member
         <input type="radio" name="u_role"  value="Administrator"<?php echo admCheck;?> /> Administrator </td>

thank you LM but its not working :(

Diafol can you help me., theres a syntax error in the code you've given..tnx

Member Avatar for diafol
$memCheck = (!isset($_POST["u_role"]) || $_POST["u_role"] == 'Member') ? "  checked = 'checked'" : "";
$admCheck = (isset($_POST["u_role"]) && $_POST["u_role"] == 'Administrator') ? "  checked = 'checked'" : "";

Substitute that bit - I put in too many parentheses. That's was comes from coding off the top of your head. :)

Also...

<td>
    <input type="radio" name="u_role"  value="Member"<?php echo $memCheck;?> /> Member
    <input type="radio" name="u_role"  value="Administrator"<?php echo $admCheck;?> /> Administrator
</td>

Notice the $ for the vars - I forgot those too. :(

Thank you guys for helping me code complete,, thank you DIAFOL...no error at all and working :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.