i have a website and i would like to add a checkbox for each record. so if i select the checkbox the page updates the database .
how can i do this please, not sure were to start
regards craig
Printable View
i have a website and i would like to add a checkbox for each record. so if i select the checkbox the page updates the database .
how can i do this please, not sure were to start
regards craig
you'll have to be a bit more descriptive about what you already have done and what you're actually trying to do. post some of your code.
also -- step 1: make a checkbox.
HTML Code:<input type="checkbox" name="myCheckbox" value="myCheckboxValue" />
hi kows
i used your posted and now i have a checkbox on my page There is now text next to it and its not getting the value from my DB.
i also need the checkbox if selected to change the value in the db please
thanks for your help in advance
Code:</tr>
<tr>
<td width="20%" rowspan="1" height="120" BORDERCOLOR=white><input type="checkbox" name="WantToWatch" value=<?php echo $row['WantToWatch']; ?></td>
<td width="80%" height="38" colspan="4" BORDERCOLOR=white><?php echo $row['filmplot1']; ?></td>
</tr>
<tr>
well, your syntax is wrong -- you need to fix your <input> so that it looks like this:
where is the code you're using to update your database now?HTML Code:<input type="checkbox" name="WantToWatch" value="<?php echo $row['WantToWatch']; ?>" />
hi kows.
i corrected the syntax but im still not getting the check box checked. in the db "wanttowatch has the value of "true" or "false"
hi kows i have had a play around with checkboxs and come up with this. but im stuck with it i cant get the true value from the wanttowatch colom of the db to change the checkbox to checked
i have posted the full table to try and show my working
Code:$sql = "SELECT vidnumber, titleid, hd, year,starring1,videotitle,starring2, directedby, filmplot1,dirid,starrid1,starrid2, trilogy,age, Runningtime,WantToWatch FROM videolist WHERE WantToWatch LIKE '".mysql_real_escape_string("True")."' LIMIT $offset, $rowsperpage";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
// while there are rows to be fetched...
while ($row = mysql_fetch_assoc($result)) {
// <td><?php echo $row['vidnubmer']; ?></td>
<td width="20%" rowspan="3" height="120" BORDERCOLOR=white><p align="center"><img border=0 src="/vidpics/<?php echo $row['videotitle'];?>web.jpg" <?php</p></td>
<td width="20%" height="19" BORDERCOLOR=white><p align="center">ID:<?php echo $row['vidnumber']; ?></td>
<td width="60%" colspan="3" height="19" BORDERCOLOR=white><p align="center"><b><?php echo $row['videotitle']; ?></b></td>
</tr>
<tr>
<td width="20%" height="14" BORDERCOLOR=white><p align="center"><a href="http://imdb.com/title/<?php echo $row['titleid']; ?>" target="_top"><?php echo $row['titleid']; ?></a></td>
<td width="20%" height="14" BORDERCOLOR=white><p align="center">Age <?php echo $row['age']; ?></td>
<td width="20%" height="14" BORDERCOLOR=white><p align="center"><?php echo $row['year']; ?></td>
<td width="20%" height="14" BORDERCOLOR=white><p align="center"><?php echo $row['Runningtime']; ?>mins</td>
</tr>
<tr>
<td width="20%" height="25" BORDERCOLOR=white><p align="center"><a href="http://imdb.com/name/<?php echo $row['dirid']; ?>" target="_top"><?php echo $row['directedby']; ?></td>
<td width="20%" height="25" BORDERCOLOR=white><p align="center"><a href="http://imdb.com/name/<?php echo $row['starrid1']; ?>" target="_top"><?php echo $row['starring1']; ?></td>
<td width="20%" height="25" BORDERCOLOR=white><p align="center"><a href="http://imdb.com/name/<?php echo $row['starrid2']; ?>" target="_top"><?php echo $row['starring2']; ?></td>
<td width="20%" height="25" BORDERCOLOR=white><p align="center"><?php echo $row['hd']; ?></td>
</tr>
<tr>
if <?php echo $row['WantToWatch']; ?>="true" {
<td width="20%" rowspan="1" height="120" BORDERCOLOR=white><input type="checkbox" name="WantToWatch" value="<?php echo $row['WantToWatch']; ?>" checked/>Watch</td>
} else {
<td width="20%" rowspan="1" height="120" BORDERCOLOR=white><input type="checkbox" name="WantToWatch" value="<?php echo $row['WantToWatch']; ?>" />Watch</td>
}
<td width="80%" height="38" colspan="4" BORDERCOLOR=white><?php echo $row['filmplot1']; ?></td>
</tr>
<tr>
<td width="80%" height="38" colspan="5" BORDERCOLOR=white><p align="center">~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~</p></td>
</tr>
<?php
}
?>
</table>
you have a random IF statement there which won't do anything; you haven't actually opened a PHP tag, so you're literally just printing an IF statement. change it to something like this:
you don't have to put the entire <input> again, however. you could shorten this immensely (this would replace your entire if statement, and produce the same result):PHP Code:<?php if($row['WantToWatch'] == "true"){ ?>
<!-- something if the value is true -->
<?php }else{ ?>
<!-- something is the value is not true -->
<?php } ?>
PHP Code:<input type="checkbox" value="<?php echo $row['WantToWatch']; ?>"<?php if($row['WantToWatch'] == 'true') echo ' checked="checked"'; ?> />
hi kows.
thanks for your help i used the longer version so i can better understand what im doing while im learnng
i now want tobe able to update my db if a checkbox is selected.. ????Code:<?php if($row['WantToWatch'] == "true"){ ?>
<!-- something if the value is true -->
<?php }else{ ?>
<!-- something is the value is not true -->
<?php } ?>
but i dont know were to start
you need to have a form, and then you need a button that will submit the form.
then, you'll need to modify your checkbox, because you need to keep track of whatever ID you're on (all of your checkboxes can not be named "WantToWatch"). I would suggest an array:HTML Code:<form method="post">
<!-- your entire table should go here -->
<input type="submit" value="Submit" />
</form>
then, at the very top of your script you should put the following:HTML Code:<input type="checkbox" name="WantToWatch[<?php echo $row['vidnumber']; ?>]" value="<?php echo $row['WantToWatch']; ?>" />
then, whenever you submit your form, you should see a big array holding all of the values that you'll need to update. keep in mind: checkboxes are NOT SET when they are submitted on a form and unchecked! this means that when you submit this form, you will have a list of all the checkboxes that were checked, but not a list of which ones were not checked. this shouldn't be a problem for updating your database table, though.PHP Code:<?php
if($_SERVER['REQUEST_METHOD'] == "POST"){
echo '<pre>';
print_r($_POST);
echo '</pre>';
}
?>
let me know when you get all of that working properly.
hi kows
thanks. for the advice and help. im off to work now.
im away for 3 days so will get straight on it when im back and let you know
thanks again for your help
craig