So I am working with some checkboxes that are populated from a db table. Which looks like this.
<form action="" method="post">
<?php
foreach ($chores as $retrieved_search){
?>
<tr>
<td>
<?php echo "<input type='checkbox' id=".$retrieved_search->id." value=".$retrieved_search->image." name='chores[]'>$retrieved_search->name";?>
<?php echo "<input type='text' value='$retrieved_search->name' id='optionName' name='optionName[]' />"?>
</td>
</tr>
<?php
}
?>
</form>
I am trying to figure out a way that I can get that value from the checkbox which I am currently doing and works fine. The value from the checkbox is an image URL.
I also need to get the name associated with the checkbox so I added another input field that is populating the name of the checkbox which I will then hide.
So this is my php statement that is getting the post value from the checkbox but how can I also get the value from the input field for the same insert statement?
<?php
if(isset($_POST['saveOptions'])) {
global $wpdb;
$table_name = $wpdb->prefix . "chartUsersOptions";
$user_ID = get_current_user_id();
$typeChore = "chore";
$typeBehavior = "behavior";
if(isset($_POST['chores'])){
foreach ($_POST['chores'] as $chores) {
$wpdb->insert( $table_name, array(
'userId' => $user_ID,
'optionImage' => $chores,
'type' => $typeChore,
));
}
}
$msg = "Saved now redirect to 3rd step";
echo $msg;
}
else{
.............
}
?>
EDIT:
Based on a suggestion This worked.
<?php
foreach ($chores as $retrieved_search){
echo "<input type='checkbox' value='{$retrieved_search->image}' name='chores[{$retrieved_search->id}]'>{$retrieved_search->name}";
echo "<input type='text' value='{$retrieved_search->name}' name='optionName[{$retrieved_search->id}]'>"
}
?>
And This
if(isset($_POST['chores'])){
foreach ($_POST['chores'] as $chores_key => $chores) {
$text_input_value = $_POST['optionName'][$chores_key];
$wpdb->insert( $table_name, array(
'userId' => $user_ID,
'optionImage' => $chores,
'optionName' => $text_input_value,
'type' => $typeChore,
));
}
}
But optionName is still inserting blank.