Home > Net >  Wordpress: Is it possible to have 2 forms on the same page that use $index and $value for input fiel
Wordpress: Is it possible to have 2 forms on the same page that use $index and $value for input fiel

Time:11-15

I have two checkbox forms on the same page. When they are both there, the second form will not work. I'm wondering if there is a way to have both on the same page with them both needing the $index of the input.

The input fields are similar to this (the other is unchecked):

echo "<input type='checkbox' name='check[]' value='$index' onChange='this.form.submit()' checked='checked'>";
echo "<input type='hidden' name='itemDELETE$index' value='$bookID'>";

This is the other

echo "<input type='checkbox' name='checkbox[]' value='$index' onChange='this.form.submit()'>";
echo "<input type='hidden' name='itemADD$index' value='$bookID'>";

And the data is stored like:

$value = $_POST["check"][0];
$toDELETE = $_POST["itemDELETE" . $value];

and

$value = $_POST["checkbox"][0];
$toADD = $_POST["itemADD" . $value];

I have tried renaming $value but the issue lies in the $index on the input fields.

They are outputting correctly, but the second one will not store the value properly in it's variable, whereas the first does. The second is the delete one.

When I remove the first forms functionality, the second form works. But when I add it back, the second one stops working. Not picking up on the itemDELETE $value

If I manually put the value in like this "itemDELETE0" instead of ["itemDELETE" . $value]it works. But I need the $value to picked up on since it's a foreach loop.

CodePudding user response:

My solution to this was to put the second form in different shortcode, and include it on the page. It works well and doesn't clash. Same code, just in a different shortcode.

Then on the page created, I used the shortcode for the first form and then put the shortcode for the second form below it.

And instead of defaulting it to checked, I had to use it unchecked. But that works alright.

  • Related