简体   繁体   中英

Processing multiple forms with individual submits

i am trying to setup a product page, with multiple products etc... It is done through a while loop getting the data from a mysql database, the products display fine, but the problem i am having is that i want to be able to process each product individually, In other words, i don't know what products may be appearing on the page, so when i try to process them through php, it needs to be flexible. Its a bit hard to explain but here is my current code, hopefully you will see what i mean.

Here is my html for one individual product, all the products get ouputed like the code below

 <form id="<?php print "$material_id";?>" class="form">    
      <div class="supp_bro_mat_wrap">    
            <input type="hidden" name="supp_bro_mat_id" value="<?php print "$material_id";?>" />        
            <input name="supp_bro_qty_input" class="supp_bro_qty_input" type="text" />              
      </div><!---end supp_bro_input_wrap--->
            <input name="supp_bro_add_to_cart" type="submit" class="supp_bro_add_to_cart" id="<?php print "$material_id";?>" />          
 </form>

And here is the php code, simple i know, but i just want to be able to get the values at this stage,

 if (isset($_POST['supp_bro_add_to_cart'])) {


    $mat_id = $_POST['supp_bro_mat_id'];
    $mat_qty = $_POST['supp_bro_qty_input'];

    var_dump($mat_id);
    var_dump($mat_qty);
}

And when i try to enter a quantity for the first product i get the right var_dumps appear, but when i try it on all the products below it i get

supplies_browse.php?supp_bro_mat_id=5&supp_bro_qty_input=8768&supp_bro_add_to_cart=Submit

appear in my browser header instead of

supplies_browse.php?sid=masonary

Like it should be and no var_dumps. Hopefully you will get what i mean, let me know if you need anything else.

Thanks

It not easy to tell from what you've posted, but it looks like you have an additional <form> tag in your HTML that precedes the products list. This additional <form> tag has method="POST" on it.

Because of this, on your products list, the <form> tag of the first product is being ignored (form elements are not allowed to be nested) so submitting the first product performs a POST submit, so the data gets transferred in the HTTP body, the URL has the value that you want, and your $_POST variables get populated correctly.

The </form> tag at the end of the first product then closes the additional <form> , and the second product and subsequent products are created in their own forms. The <form> tags for these have no method attribute specified, so the default method is used which is GET. This sends the information on the URL, which will not populate the $_POST variables.

The solution is to remove the additional <form> tag from the start of your page, and move the method="POST" attribute (and probably any other attributes on that tag) to each of the <form> tags for the products.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM