0

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.

enter image description here

2
  • The two things I can see is it doesn't look liek your html ended up in the form tag (as far as I can tell from your screenshot, you may have cropped that out) and also your input with the class cart-quantity-input doesn't have a name attribute, so that won't show up in your $_POST variable. I'm not sure if that will fix all of the issues, but it might be a start. Commented Feb 10, 2021 at 19:30
  • @FrankA.Yeah I just cropped that out, I didn't want to give a long list of code to read through. The console log I took, listed the contents of "cart-items, see updated original post, this will show the full contents of class "main" which is where the form is inside. Also yeah I'll sort the "cart-quantity" one next, I'm just trying to get the post value for "selectedIDs" first but just nothing is coming across, is there anything else on your end that you can see. I'm stumped. Commented Feb 10, 2021 at 19:40

1 Answer 1

1

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()

Sign up to request clarification or add additional context in comments.

6 Comments

How are you appending the hidden fields to the form ? Are you using append() ?
Ahh yeah I understand now the submit post is done on page load, so anything I add to it won't be accounted for. I tried <form id = "myForm" action = "test.php" method = "POST"> <input type = "button" id ="submitBtn" Then I have this jquery code in the .js file $(document).ready(function(){ $("#submitBtn").click(function(){ $("#myForm").submit(); // Submit the form}); }); But still it's not sending the page to test.php or bringing the SelectedIDs across.
In the screenshot of the html elements I couldn't find the submit button. Do you have it there?
Yeah sorry, I've updated that now, check OP. Thanks for the help.
Can you change to ajax for the form submit. So that we can track the event happening in the network tab. I'm not able to find any other solution for you.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.