0

Good Morning,

Im working on a larger project but i tried to do a simple page in order to check my work. php/mysql newb here. Sorry! :)

What im trying to accomplish is ultimately having a single user page shown with rows of tasks from a table and a single check mark in order to say if the user has completed the task or not by checking or unchecking the box.

For testing purposes, I have set up a table with rows labled testid, testdata and testcheck. The testid a INT(2)AutoIncrement and Primary and Unique, testdata is a VARCHAR(30) and testcheck is a TINYINT(1). The auto increment isnt really important because I manually populated all the rows. I have 5 rows (for the array sake) consisting of 1-5 testid, "testdata1-5" for testdata and either a 0 or a 1 for testcheck. The table is functioning fine and the database can be queried.

Here is the code for the php start page:

<html>

<?php

include_once('init.php');

$query = mysql_query("SELECT testid, testdata, testcheck FROM testtable");

?>
<h1>Test form for checkmarks</h1>
<form method="POST" action="testover.php">
<table border="1">
    <tr>
        <td>
            Test ID
        </td>
        <td>
            Test Data
        </td>
        <td>
            Checked
        </td>
        <td>
            Test Check
        </td>
    </tr>
<?php
    while ( $row = mysql_fetch_assoc($query)) {
?>            
    <tr>
        <td>
            <?php echo $row['testid']; ?>
        </td>
        <td>
            <?php echo $row['testdata']; ?>
        </td>
        <td>
            <center><input type="checkbox" name="<?php $row['testid'] ?>" value="<?php $row['testcheck']; ?>" <?php if($row['testcheck']=='1'){echo 'checked';} ?>></center>
        </td>
        <td>
            <center><?php echo $row['testcheck']; ?></center>
        </td>
    </tr>
<?php } ?>

</table>
           <input type="submit" name="confirm" value="Confirm Details">
           <a href="testcheck.php"><input type="button" value="Home"></a>
</form>
</html>

Ive been going back and forth trying to use an array for the inputs name (name="something[]") or the "isset" parameter but that is where my knowledge is failing. Ive read countless articles both here and on other websites and I cant seem to find the right code to use. Most sites have rows with multiple check boxes or a different layout of their table.

I posted here to hopefully be pointed in some direction as to how to update the DB with these values of the check marks.

2
  • You lack the <form></form> tag. the submit button requires a form to work and send the data through to the page you'd like or the refresh and use that data. Even if it's just a simple checkbox. After that you can use the if(isset()) part, cause your submit button doesn't know what to submit. Commented Sep 4, 2014 at 16:04
  • I edited it so they are easier to see but they were in the original. Commented Sep 4, 2014 at 17:34

2 Answers 2

0

I am trying to make sense of your code on what you want, and if I understand it right, you want those 5 rows outputted right?

<?php 
echo '<form action=''>;
while ( $row = mysql_fetch_array($query)) {
echo "<tr>";
echo "<td>".$row['testid']."</td>";
echo "<td>".$row['testdata']."</td>";
echo "<td><input='checkbox' name='".$row['testid']."' value='".$row['testcheck']."'  ".if($row['testcheck']=='1'){echo 'checked';}."></td>";
echo "<td>".$row['testcheck']."</td>";
echo "</tr>";
}
echo "<input type='submit' name='confirm'>";
echo "</form>";
?>

and then updating. since the form has no action, it returns to itself. So now you can add on top of your page your new php and updating code.

<?php
if(isset($_POST['confirm'])){ // the name of your submit button
    // your update code here.
?>

-- edit --

Checkbox looping in php while posting to database

I had a while ago the same problem as you. I needed to loop through checkboxes to post. Please check this topic for relevant information.

Sign up to request clarification or add additional context in comments.

8 Comments

The 5 rows output correctly. They show the testid, testdata and testcheck columns from the table including the check mark. I would like for the submission process to update any changes to the check marks in their appropriate rows in the table.
okay, as i stated in my answer, it requires to be then in a form so when you press the submit button, it knows it has to POST the data within the form. In the action attribute of the form you can specify where your script is (blank is on the same page). Then add an isset as i stated above your call to the update.
I used your code (had to take the input line out of PHP; netbeans didnt like it at all) but im probably messing up on the update line. I also do not know how to add code to comments here.... ugh
its because i made an error in the quotation ;) it's "<input type='submit' name='confirm'>"; i fixed that line here :) and will do it in the answer as well. the isset code is a bit more complicated, but what you need to do is make sure it checks which checkboxes are checked and that it executes the update statements on there. I ll see if i can post an example
i actually meant the input line above where the checkboxes are being made
|
0

This is my new code. I know im on the right track. Just need some guidance in writing the isset statement and such.

<?php

include_once('init.php');

$query = mysql_query("SELECT testid, testdata, testcheck FROM testtable");

$checkedarray = $_POST['isChecked'];

if(isset($_POST['confirm'])) {
if(isset($checkedarray)){ // the name of your submit button
   foreach ($checkedarray as $value)
mysql_query("UPDATE testtable SET testcheck = '{$value}' WHERE testid = {$_POST['testid']}"); } }
?>

<h1>Test form for checkmarks</h1>
<table border="1">
    <tr>
 <td>
            Test ID
        </td>
        <td>
            Test Data
        </td>
        <td>
            Checked
        </td>
        <td>
            Test Check
        </td>
    </tr>
<?php 
echo '<form action="">';
while ( $row = mysql_fetch_array($query)) {
echo "<tr>";
echo "<td>".$row['testid']."</td>";
echo "<td>".$row['testdata']."</td>"; ?>
<td><input type="checkbox" name="isChecked[]" value="<?php $row['testcheck']; ?>" <?php if($row['testcheck']=='1'){echo 'checked';} ?>></td>
<?php
echo "<td>".$row['testcheck']."</td>";
echo "</tr>";
}
echo "<input type='submit' name='confirm'>";
echo "</form>";
?>
           <a href="testcheck.php"><input type="button" value="Home"></a>
</form>
</html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.