Reputation: 65
I am creating a shopping cart function which adds items to the cart, then if the user presses the order button then I want the data to be posted across to my PHP page. I have the following HTML
<div class="main">
<form action = "test.php" method = "POST">
<div class = "cart-items">
</div>
<div class="btn btn-primary btn-purchase"><input type = "submit" name ="submit" class="btn_1 gradient full-width mb_5">Order Now></div>
</form>
</div>
I populate the "cart-items" class using javascript,
var cartRowContents =
`<ul class="clearfix">
<div class="cart-item cart-column">
<li class = 'remove'>
<a href="#0"><input type = "hidden" name = "selectedIDs[]" value="${itemID}"></a>
</li>
</div>
</ul>`
The href tag only has css applied to it, I tested removing the tag to see if that was the issue but it still didn't show the "selectedIDs" post data.
The code I have in test.php is below. The form is posting, as I can see inside the isset function, but the only thing posting is the "submit" from the submit button.
<?php
if(isset($_POST["submit"]))
{
echo "yes it has submitted";
}
print_r($_POST);
?>
See below how the cart looks in the console log. There are other elements within the "cart-items" class but I took them out of this example to keep things clear.
Upvotes: 0
Views: 662
Reputation: 36
You cannot use the submit to append the hidden button to the form. You will have to change the type of submit to <input type = "button" name ="submit">
and append the hidden field to the form and then submit the form through $('form').submit();
. When click on the submit button the process for submitting the form is already started.
You can also refer: jQuery not posting all inputs of a form after the .append()
Upvotes: 1