SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2001
    Location
    upstate NY, USA
    Posts
    47
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help for php newbie

    Help pleeze...

    I'm not sure how to name and pass form variables created within a loop.

    Here is what I tried:
    while(conditional statement)
    {
    <input type="hidden"
    name="item_num"<?$var_x;?>
    value=<?$var_x;?> >
    }

    where I've given the hidden field a unique name within the loop.

    What would the php (or javascript) reference be for the hidden field or is there a better way to name it?

  2. #2
    epsilon transition cupid's Avatar
    Join Date
    Aug 2001
    Location
    Kent, Ohio
    Posts
    367
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    while(conditional statement)
    {
    <input type="hidden"
    name="item_num"<?$var_x;?>
    value=<?$var_x;?> >
    }
    Might want to try this instead:

    PHP Code:

    while(conditional statement) { 
       echo 
    "<input type=\"hidden\" name=\"item_num".$var_x."\" value=\"".$var_x."\">";

    Last edited by cupid; Sep 6, 2001 at 10:35.

  3. #3
    SitePoint Enthusiast
    Join Date
    Mar 2001
    Location
    upstate NY, USA
    Posts
    47
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cupid,

    Thanks for the code:
    while(conditional statement) {
    echo "<input type=\"hidden\" name=\"item_num".$var_x."\" value=\"".$var_x."\">";
    }

    But how can I refer to the fields outside of the loop
    if it can be done at all?

    i.e. $item_num.$var_x; ???

  4. #4
    epsilon transition cupid's Avatar
    Join Date
    Aug 2001
    Location
    Kent, Ohio
    Posts
    367
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This hidden input file is for a form.. to access it, you'd have to be passing them to a new file that is specified in the action attribute of the <form> tag.
    Meaning, you CAN'T access these $item_num variables in the same page, let alone outside the loop.

    Once inside the new file that's supposed to process the form, you can refer to the variable by $item_num23234, granted that 23234 is inside $var_x.

    I'm not sure what you're doing inside or before the while loop. Is $var_x an identifier that is incremented each time through the loop? If that's the case, you may want to use arrays instead:

    PHP Code:
    while(conditional statement) { 
    echo 
    "<input type=\"hidden\" name=\"item_num[".$var_x."]\" value=\"".$var_x."\">"


  5. #5
    SitePoint Enthusiast
    Join Date
    Mar 2001
    Location
    upstate NY, USA
    Posts
    47
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $var_x is an identifier that can be incremented each time through the loop (I used $product_id in the code below which is read from MySQL. What I'm trying to do is when a form select option item displaying a quantity is clicked, it will self submit to this same page. The select option items are created within the loop with each uniquely named as:

    $x = 0;
    while($x != $cart->num_items($table,$session))
    {
    echo "<TR><TD>".$contents[product][$x]. "</TD><TD>".$contents[price][$x]. "</TD>\n";
    $product_id = $contents[product_id][$x];

    $quantity = $contents[quantity][$x];
    name=quantityty$product_id ";
    $x]. "\"></TD>";
    ?>
    <td>
    <select NAME="quantity<?$product_id?>" ONCHANGE="autoSubmit(this.form)">
    <option value="1"<?=($quantity == "1") ? " selected" : ""?>>1</option>
    <option value="2"<?=($quantity == "2") ? " selected" : ""?>>2</option>
    <option value="3"<?=($quantity == "3") ? " selected" : ""?>>3</option>
    </select>
    <input type="hidden" name="modify" value="-1">

    When the form is self submitted I am trying to update a table with the select option item that has changed by referencing it in the code below. I don't how to reference the form select option item as I've indicated in red.

    if($modify)
    {if($ShoppingCart)
    {
    $session = $ShoppingCart;
    }

    $contents = $cart->display_contents($table,$session,$product_id);
    for($i = 0; $i < sizeof($quantity); $i++)
    {
    $oldquan = $contents[quantity][$i];
    $product_id = $contents[product_id][$i];
    $newquan = $quantity[$product_id];
    $newquan = $quantity;

    $cart->modify_quantity($table,$session,$product_id,$newquan);

    I don't have any ideas on how to do this. There is probably a better way to do it but I'm trying to work with an existing class (cart) of functions. Thanks for any help.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •