Search code examples
phpformsradio-buttonchecked

Set 'checked' to value from database radio input


I have been struggling how to have my input radio buttons to have a preset value set in an edit account page. I want to have the 'role' input checked when viewing the edit account page from the value in my database.

edit: I want to have the radio button that has been previously selected by the user to be checked on the edit account page, so use the value from the database to choose which input should be checked.

        // Initial query parameter values 
                $query_params = array( 
            ':rank' => $_POST['rank'],
            ':role' => $_POST['role'], 
            ':user_id' => $_SESSION['user']['id']
        );







        // If the user is changing their password, then we need parameter values 
        // for the new password hash and salt too. 
        if($password !== null) 
        { 
            $query_params[':password'] = $password; 
            $query_params[':salt'] = $salt; 
        } 

        // Note how this is only first half of the necessary update query.  We will dynamically 
        // construct the rest of it depending on whether or not the user is changing 
        // their password. 
        $query = "UPDATE users SET rank = :rank";
        $query .= ", role = :role";


         // If the user is changing their password, then we extend the SQL query 
        // to include the password and salt columns and parameter tokens too. 
        if($password !== null) 
        { 
            $query .= " 
                , password = :password 
                , salt = :salt 
            "; 
        } 

        // Finally we finish the update query by specifying that we only wish 
        // to update the one record with for the current user. 
        $query .= " 
            WHERE 
                id = :user_id 
        "; 

        try 
        { 
            // Execute the query 
            $stmt = $db->prepare($query); 
            $result = $stmt->execute($query_params); 
        } 
        catch(PDOException $ex) 
        { 
            // Note: On a production website, you should not output $ex->getMessage(). 
            // It may provide an attacker with helpful information about your code.  
            die("Failed to run query: " . $ex->getMessage()); 
        } 

        // Now that the user's rank has changed, the data stored in the $_SESSION 
        // array is stale; we need to update it so that it is accurate. 
        $_SESSION['user']['rank'] = $_POST['rank'];  



        // This redirects the user back to the members-only page after they register 
        header("Location: private.php"); 


        die("Redirecting to private.php"); 
    } 

?> 
<h1>Edit Account</h1> 
<form action="edit_account.php" method="post"> 
    battletag:<br /> 
    <b><?php echo htmlentities($_SESSION['user']['battletag'], ENT_QUOTES, 'UTF-8'); ?></b> 
    <br /><br /> 
    Preferred Role:<br />
    <input type="radio" name="role" value="Assasin">Assasin 
    <input type="radio" name="role" value="Warrior">Warrior 
    <input type="radio" name="role" value="Specialist">Specialist
    <input type="radio" name="role" value="Support">Support
    <br /><br />
    Rank<br /> 
    <input type="text" name="rank" value="<?php echo htmlentities($_SESSION['user']['rank'], ENT_QUOTES, 'UTF-8'); ?>" /> 
    <br /><br /> 
    Password:<br /> 
    <input type="password" name="password" value="" /><br /> 
    <i>(leave blank if you do not want to change your password)</i> 
    <br /><br /> 
    <input type="submit" value="Update Account" /> 
</form>

Solution

  • You can create a function to return checked if you have multiple radio buttons.

    function is_checked($db_value, $html_value){
      if($db_value == $html_value){
        return "checked";
      }
      else{
        return "";
      }
    }
    

    And use the function like:

    <input type="radio" name="role" value="Assasin" <?php echo is_checked($db_value, "Assasin"); ?> >Assasin 
    <input type="radio" name="role" value="Warrior" <?php echo is_checked($db_value, "Warrior"); ?> >Warrior 
    <input type="radio" name="role" value="Specialist" <?php echo is_checked($db_value, "Specialist"); ?> >Specialist
    <input type="radio" name="role" value="Support" <?php echo is_checked($db_value, "Support"); ?> >Support