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.
I'm not sure what the exact problem is, but my guess is that your checkboxes don't match your input boxes. That is because unchecked checkboxes do not get sent to the server, so you probably end up with an checkboxes array that is smaller than the input boxes array.
You should change your html so that a checkbox-array-key always matches an input-array-key, so something like:
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}]'>"
Also note that ID's need to be unique so I removed them but that is probably not related to your problem.
Edit: To get the value of the text box after making the above changed, you can do:
if (isset($_POST['chores'])) {
foreach ($_POST['chores'] as $chores_key => $chores) {
$text_input_value = $_POST['optionName'][$chores_key];
// and the rest of your code
Instead of
foreach ($_POST['chores'] as $chores) {
you want
foreach ($_POST['chores'] as $id=>$chores) {
Then you can access
optionName[$id]
in your loop.