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 class="user-wrap">
<div class="inner-wrap">
<!---entire checkbox--->
<div><span class="check">
<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 withvar_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!
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 class="user-wrap">';
echo '<div class="inner-wrap">';
echo '<!---entire checkbox--->';
echo '<div><span class="check">';
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 hasname='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 theuser_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?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
HTML (English "hyper text markup language" - hypertext markup language) is a special markup language that is used to create sites on the Internet.
Browsers understand html perfectly and can interpret it in an understandable way. In general, any page on the site is html-code, which the browser translates into a user-friendly form. By the way, the code of any page is available to everyone.
https://www.w3.org/html/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.