Ryan
Ryan

Reputation: 71

while loop with form with checkbox php not updating based on database details

I have a while loop which is display all information correctly, I want a checkbox allowing me to mark each member of the loop as completed in the database, I don't really know where to start with this and I would appreciate any help.

Below is how far I am, it is generating the loop correctly and displaying the checkbox however it is not being set as completed on loading?

    while ($row = mysql_fetch_array($query)){
        $task_name = $row['task_name'] ;
        $task_description = $row['task_description'];
        $task_completed = $row['completed'];
        $tasks .= '<div id="tasksBody"><form action="" method="post">Completed? <input name="completed" type="checkbox" if ($task_completed == 1){checked="checked"} /><input type="submit" value="Update"><br /><br /><b>'.$task_name.'</b><br /><br />'.$task_description.'<hr><br /></form></div>';
    }
}

Any advice would be greatly appreciated

Upvotes: 0

Views: 168

Answers (1)

bwoebi
bwoebi

Reputation: 23777

You cannot write raw PHP code in a string and hope that it's executed. Inline you also cannot use if, but you have to use the ternary operator.

$tasks .= '<div id="tasksBody">
<form action="" method="post">Completed? <input name="completed" type="checkbox" '.
($task_completed == 1?'checked="checked"':'').
' /><input type="submit" value="Update">
<br /><br />
<b>'.$task_name.'</b><br /><br />'.$task_description.'<hr><br /></form></div>';

Upvotes: 3

Related Questions