I had originally had the form inside of the foreach(ran into issues that seemed unsolvable because it would call the function as many times as the foreach had listed).
So now, with the <form>
outside of the foreach it will only submit the data from the final collection in the foreach loop. But what I need it to do is send the data depending on which item in the foreach loop is checked.
This is my function for submitting the information to the database:
function user_add_new() {
global $wpdb;
$bookTOadd = $_POST["book"];
$listTOadd = $_POST["listID"];
$userID = $_POST["user"];
$addTABLE = $wpdb->prefix . 'plugin_read';
$wpdb->insert(
$addTABLE,
array(
'bookID' => $bookTOadd,
'userID' => $userID,
'listID' => $listTOadd,
)
);
}
And this is my foreach loop with the form outside of it:
<form action="<?php user_add_new();?>" method="post">
<?php
foreach($books_array as $i ) {
$s = $i->title;
$o = $i->listID;
$r = $i->submittedBy;
$d = $i->bookDESC;
$t = $i->id;
$current_user = wp_get_current_user();
$current_id = $current_user->ID;
?>
<?php if($r == ''.$current_user->ID.''|| $r == 'administrator') { ?>
<div >
<div >
<!---entire checkbox--->
<div><span >
<input type="checkbox" value="" onChange="this.form.submit()">
<input type="hidden" name="item" value="<?php echo $t;?>">
<input type="hidden" name="listID" value="<?php echo $o;?>">
<input type="hidden" name="userID" value="<?php echo $current_id;?>">
</span></div>
<!---entire checkbox--->
<!---info from book--->
<div>
<b><?php echo $s; ?></b><br>
<?php echo $d; ?>
</div>
<!---info from book--->
</div>
</div>
<?php } else {
}; ?>
<?php
};
?>
</form>
I did check the output with var_export($_POST);
and it indeed does only return the data for the final item in the foreach loop when it needs to return data depending on which foreach item is checked
I have been stuck on this single form for hours now, I think I need a fresh mind to help find the solution. I truly appreciate any help!
CodePudding user response:
Try this as your form:
<form action="<?php user_add_new();?>" method="post">
<?php
foreach($books_array as $index => $i ) {
$s = $i->title;
$o = $i->listID;
$r = $i->submittedBy;
$d = $i->bookDESC;
$t = $i->id;
$current_user = wp_get_current_user();
$current_id = $current_user->ID;
if ($r == $current_user->ID || $r == 'administrator') {
echo '<div >';
echo '<div >';
echo '<!---entire checkbox--->';
echo '<div><span >';
echo "<input type='checkbox' name='checkbox[]' value='$index' onChange='this.form.submit()'>";
echo "<input type='hidden' name='item$index' value='$t'>";
echo "<input type='hidden' name='listID$index' value='$o'>";
echo "<input type='hidden' name='userID$index' value='$current_id'>";
echo '</span></div>';
echo '<!---entire checkbox--->';
echo '<!---info from book--->';
echo "<div><b>$s</b><br>$d</div>";
echo "<!---info from book--->";
echo '</div>';
echo '</div>';
}
};
?>
</form>
I've removed the many <?php ?>
statements and echoed the HTML in PHP. Also notice checkbox input has name='checkbox[]' value = '$index'
.
The []
after the name means the values of the checked checkboxes will come through as an array in $_POST["checkbox"]
. Since you are immediately submitting the form as soon as one is checked, you should only have one value in this array so we access that in the user_add_new()
function with $_POST["checkbox"][0]
Then this is the function:
function user_add_new() {
global $wpdb;
$value = $_POST["checkbox"][0];
$bookTOadd = $_POST["item" . $value];
$listTOadd = $_POST["listID" . $value];
$userID = $_POST["userID" . $value];
$addTABLE = $wpdb->prefix . 'plugin_read';
$wpdb->insert(
$addTABLE,
array(
'bookID' => $bookTOadd,
'userID' => $userID,
'listID' => $listTOadd,
)
);
}
You had $bookTOadd = $_POST["book"];
but I couldn't see an input with book as a name so I assume this should be item?