Jump to content

Recommended Posts

Hey Guys,

 

I'm posting a set of hidden fields. Each product has a product_id[] hidden field as below:

 

<input type="hidden" value="6" name="product_id[]"/>

 

So as the user selects more and more products, more hidden fields are added with the name "product_id[]". I then use this function to loop through the array:

 

<?php

$check=$_POST['product_id'];
while (list ($key,$val) = @each ($check)) {
$insert = array(

'product_id' => $val,

?>
);

 

All this so far is fine, however... I need to add a textfield called "qty[]" that will take in a quantity for each product. How can I handle this with the product_id[] hidden field so I can tie them in together and know which quantity is associated with each product_id[]?... hope that makes sense

 

 

Link to comment
https://forums.phpfreaks.com/topic/173713-posting-an-array-on-a-hidden-field/
Share on other sites

Your $insert = array ... statement does not look correct.  I'm guessing you cut some code out or re-typed it.

 

Usually when I do something like this, I put the product_id (or whatever) as the key to the array in the form field, this way everything related to that product has the same array key.  for example (this is not real code, just off the top of my head)

 

<input type="text" name="color[1]" value="Black"><input type="text" name="qty[1]" value = 1>
<input type="text" name="color[2]" value="Blue"><input type="text" name="qty[2]" value = 3>
<input type="text" name="color[3]" value="Cyan"><input type="text" name="qty[3]" value = 1>

This way I don't need a hidden field for the product_id and if there are check boxes, the returned array is indexed by the product_id since the ones that are not checked (were not selected) are not sent to the action script.  The indexes do not have to be numeric, they just have to be valid for an array key; however, if they have certain special characters they may get mangled (urlencoded?) when POSTed.

 

I do it this way because I do not trust browsers.  If for some weird reason it sent the fields out of sequence, the numerically assigned array keys could get mixed up.  Using the product ID insures that the fields related by product have the same unique index.  Call me paranoid, but hey, as long as M$ is has a browser out there, I'm going to protect myself from their idiocy.  Besides, checkboxes are not sent unless checked and the numeric array would not tie to the other fields for that same line.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.