I'm using a table to update a database, the table has 264 checkboxes which can be checked and unchecked then updated on the database.
I'm doing this by posting data on the form, using a while loop to set all fields to blank ( the value of the checkbox and the textarea value) for each respective field, then using a foreach loop to update each row in the database wit开发者_高级运维h the value of the checkbox.
Now, what I want to do is add the textarea value for each ticked checkbox into the database as well, what i cant figure out is how to do this?
This is my update code:
if (isset($_POST["update"])) {
$seolistRes2 = mysql_query($seolistQ) or die(mysql_error());
while ($seolistRow2 = mysql_fetch_array($seolistRes2)) {
    $wsID1 = $seolistRow2["worksheetID"];
    $updateWSQ2 = "UPDATE seo_work SET taskValue=0, taskInfo='' WHERE worksheetID=$wsID1 AND userID=$userID";
    mysql_query($updateWSQ2) or die(mysql_error());
}
$item = $_POST;
foreach($item as $key => $value) {
    $wsID = str_replace("checkbox","",$key);
    if (is_numeric($wsID)) {
        $updateWSQ = "UPDATE seo_work SET taskValue=$value taskInfo=$value WHERE worksheetID=$wsID AND userID=$userID";
        mysql_query($updateWSQ) or die(mysql_error());
        header("Location: worksheet.php?y=".$seoworkyear."&userID=$userID&action=success");
    }
}
}
This is checkbox and textarea code: (please note this is within a form)
$currentTask = '';
echo "<tr class='tr'>";
while ($seolistRow = mysql_fetch_array($seolistRes)) {
    $taskValue = $seolistRow["taskValue"];
    $worksheetID = $seolistRow["worksheetID"];
    $taskName = $seolistRow["taskName"];
    $taskInfo = $seolistRow["taskInfo"];
    if ($taskValue == 1) {
        $taskDone = "<input type='checkbox' value='1' class='checkbox' name='checkbox".$worksheetID."' id=checkbox'".$worksheetID."' checked='checked' />".
        "<textarea class='textarea' name='textarea".$worksheetID."' id=textarea'".$worksheetID."'>" . $taskInfo . "</textarea>";
    }
    else {
        $taskDone = "<input type='checkbox' value='1' class='checkbox' name='checkbox".$worksheetID."' id='checkbox".$worksheetID."' />".
        "<textarea class='textarea' name='textarea".$worksheetID."' id=textarea'".$worksheetID."'>" . $taskInfo . "</textarea>";
    }
    if ($currentTask != $taskName) {
        echo "</tr>";
        echo "<tr class='tr'>";
        echo "<td class='task'>".$taskName."</td>";
    }
    echo "<td class='tick'>".$taskDone."</td>";
        $currentTask = $taskName;
}
echo "</tr>";
Use your HTML form like shown below.
$currentTask = '';
echo "<tr class='tr'>";
while ($seolistRow = mysql_fetch_array($seolistRes)) {
    $taskValue = $seolistRow["taskValue"];
    $worksheetID = $seolistRow["worksheetID"];
    $taskName = $seolistRow["taskName"];
    $taskInfo = $seolistRow["taskInfo"];
    if ($taskValue == 1) {
        $taskDone = "<input type='checkbox' value='1' class='checkbox' name='checkbox[".$worksheetID."]' id='checkbox[".$worksheetID."]' checked='checked' />".
        "<textarea class='textarea' name='textarea[".$worksheetID."]' id='textarea[".$worksheetID."]'>" . $taskInfo . "</textarea>";
    }
    else {
        $taskDone = "<input type='checkbox' value='1' class='checkbox' name='checkbox[".$worksheetID."]' id='checkbox[".$worksheetID."]' />".
        "<textarea class='textarea' name='textarea[".$worksheetID."]' id='textarea[".$worksheetID."]'>" . $taskInfo . "</textarea>";
    }
    if ($currentTask != $taskName) {
        echo "</tr>";
        echo "<tr class='tr'>";
        echo "<td class='task'>".$taskName."</td>";
    }
    echo "<td class='tick'>".$taskDone."</td>";
        $currentTask = $taskName;
}
echo "</tr>";
See the modified name and id of your textarea and checkbox. I have modified it to textarea[SOME_WORKSHEET_ID] and checkbox[SOME_WORKSHEET_ID] respectively.
So, when you submit the form, you will receive those checkbox and textarea value as an array, with worksheetId as an index. You can use this [] technique to retrieve the values of the textarea and checkbox, or as many field you want to add in form.
Use above HTML structure and check the $_POST array.
Hope this will help..
Thanks!
Hussain.
You should be able to get the value in the textarea using $_POST['textarea' . $wsID] after your call to is_numeric.
Here is another approach, not completely tested, but hopefully you get the idea...
if (isset($_POST["update"])) 
{
    // All to blank
    $seolistRes2 = mysql_query($seolistQ) or die(mysql_error());
    while ($seolistRow2 = mysql_fetch_array($seolistRes2)) 
    {
        $wsID1 = $seolistRow2["worksheetID"];
        $updateWSQ2 = "UPDATE seo_work SET taskValue=0, taskInfo='' WHERE worksheetID=$wsID1 AND userID=$userID";
        mysql_query($updateWSQ2) or die(mysql_error());
    }
    // Re use your result from the select to go through all the known IDs
    foreach($seolistRow2 as $i => $data) 
    {
        // Obtain the ID
        $id = $data['worksheetID'];
        // Get the checkbox value for current ID
        $checkbox_wsID = $_POST['checkbox' . $id];
        // Get the textarea value for current ID
        $textarea_wsID = $_POST['textarea' . $id];
        // Update the Row using mysql_escape
        $updateWSQ = "UPDATE seo_work " . 
        "SET taskValue = '" . mysql_escape_string($checkbox_wsID) ."' taskInfo = '" . mysql_escape_string($textarea_wsID) ."'" .
        "WHERE worksheetID=$id AND userID=$userID";
        mysql_query($updateWSQ) 
            or die(mysql_error());
        header("Location: worksheet.php?y=".$seoworkyear."&userID=$userID&action=success");         
    }
}
 
         
                                         
                                         
                                         
                                        ![Interactive visualization of a graph in python [closed]](https://www.devze.com/res/2023/04-10/09/92d32fe8c0d22fb96bd6f6e8b7d1f457.gif) 
                                         
                                         
                                         
                                         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论