How can I access the values of a checkbox outside of the form action that it is declared in?

Viewed 35

I successfully created a script that will delete the rows of a table if a checkbox is checked on that row (the checkbox holds the rowID). The checkboxes and button to delete these rows are inside of the same form tags. Now I want to create another button that uses the value of the checkboxes to do a different update statement, but the values of the checkboxes are not appearing in $_POST on this separate page.

Does anyone know how to make the checkbox values accessible outside of the form action it is inside of? Here is my reduced code for the delete that works:

The function below is called on PickTicket.php to display a table.

Function DisplayPickTicket() {

$conn = getDBConnection();
$sql = "SELECT * FROM dbo.BK_NotesRecord WHERE StatusID = 1 ";
$stmt = sqlsrv_query( $conn, $sql );
if ( $stmt === false ) {
    die( print_r( sqlsrv_errors(), true) );
} 

echo '<form action="updatepickstatus.php" method="post">';

// Delete Checkbox header.
echo '<th class="table-header" style="width:5px;">';
echo 'Delete';
echo '</th>';   

// Inventory number header.
echo '<th class="table-header" style="width:90px;">';
echo 'Inventory #';
echo '</th>';   

//InventoryID Header
echo '<th class="table-header" style="width:40px;">';
echo 'InventoryID';
echo '</th>';   

if (sqlsrv_has_rows($stmt)) {
    while($row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC)) {
        
        
        echo '<tr>';
        //Delete checkbox
        echo '<td class="cell"><div class="cell">';
        echo '<input type = "checkbox"  name="chkbox[]" value= "' .$row['InventoryID']. 
        '">';
        echo '</td>';
        // Inventory#
        echo '<td class="cell"><div class="cell">';
            echo $row["InventoryNumber"];
        echo '</td>';
        
        // InventoryID.
        echo '<td class="cell"><div class="cell">';
        echo $row["InventoryID"];
        echo '</td>';
            
    }
    
    }

 echo "<tr>";
 echo "<td>";

 echo "<input type='submit' name='submit' Value='Remove'>";
 echo '</form>';
 echo "</td>";
 echo "</tr>";

This is updatepickstatus.php:

<?php
$serverName = "(local)";
$connectionOptions = array("Database"=>"Powerlink");
$conn = sqlsrv_connect( $serverName, $connectionOptions);
if( $conn === false ) {
    echo "Connection failed!<br>";
    die( print_r( sqlsrv_errors(), true));
} 


if (isset($_POST['chkbox'])) {
foreach($_POST['chkbox'] as $Update) {
    
    
$sql = "UPDATE BK_NotesRecord set StatusID = '2' WHERE InventoryID  LIKE '".$Update."'";
$stmt = sqlsrv_query( $conn, $sql );
//echo '$ids';
}
}
print_r($_POST);

?>

^^I want to accomplish this same basic task, but outside of updatepickstatus.php. When applying similar logic to check the values of the selected checkboxes on a different I get an empty array. Any thoughts?

1 Answers

Try declaring a variable and passing the value you want to use to it then use sessions to move it where ever you want.

Related