Jump to content

Accessing dynamically named input boxes.

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
13 replies to this topic

#1
Blue Indian

Blue Indian

    Learning Programmer

  • Members
  • PipPipPip
  • 67 posts
Alright, the following loop dynamically names input boxes for a user to enter a quantity like in a shopping cart:

while($row = mysqli_fetch_assoc($rs)){
                echo "<tr>";
                echo "<td>". $row['coffee_name'] . "</td>";
                echo "<td>$" . $row['coffee_price'] . "</td>";
                echo "<td><input type='text' size='2' name='".$row['coffee_sku']."' value='" . $row['quantity'] . "'></td>";
                echo "</tr>";                
            }
Now my question is how do I access these quantities after the user hits the "Update Quantities" submit button.

I tried something like the follwing:

$quantity = $_POST($row['sku']);


#2
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
All the values will be in the $_REQUEST / $_POST / $_GET super global.

#3
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
I would store what you are updating in the current session, for example:
$_SESSION['currsku'] = $row['coffee_sku'];
while($row = mysqli_fetch_assoc($rs)){
                     echo "<tr>";
                     echo "<td>". $row['coffee_name'] . "</td>";
                     echo "<td>$" . $row['coffee_price'] . "</td>";
                     echo "<td><input type='text' size='2' name='quantity' value='" . $row['quantity'] . "'></td>"; 
                     echo "</tr>";                
            }
And then you can access it appropriately by $_SESSION['currsku'] again on the other page.
echo "You are now purchasing "  . $_POST['quantity'] . " items of SKU " . $_SESSION['currsku'];
you can unset() it to stop using the session key. You will be required to place session_start(); on the top of each page using sessions.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#4
Blue Indian

Blue Indian

    Learning Programmer

  • Members
  • PipPipPip
  • 67 posts
My problem is that the user could be updating one or more of several item quantities which are dynamically named. So, for example, let's say I am using the post method and each quantity field is a text box named by that item's SKU. Alright, so I would access this updated quantity by:

$_POST['some dynamically named textbox name which I do not know'];

I tried it this way but the compiler doesn't like this format:

$_POST[$row['sku']]; //where $row['sku'] will be the name of the dynamically named textbox.

Do you understand my dilemma?

#5
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
No, I don't understand your dilemma. You have access to the $_POST superglobal which has all the information you need.

print_r($_POST)
If you want the keys...
$keys = array_keys($_POST);

print_r($keys);
if you want the key/value pair

foreach($_POST as $key -> $value) {

    echo "$key / $value <br />\n";

}

And now you have the 'dynamically named textbox names which you do not know'

#6
Blue Indian

Blue Indian

    Learning Programmer

  • Members
  • PipPipPip
  • 67 posts
OK, I see how to get the $keys, but I need to be able to use a variable for the key such as $_POST[$key] for each element of the array of keys.

#7
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
$_POST[$keys[0]];

#8
Blue Indian

Blue Indian

    Learning Programmer

  • Members
  • PipPipPip
  • 67 posts
Cool! Thanks!

#9
Blue Indian

Blue Indian

    Learning Programmer

  • Members
  • PipPipPip
  • 67 posts
When I use the following code, I get this error: Parse error: syntax error, unexpected T_STRING, expecting T_VARIABLE or '$' in C:\website\webdev\robert.harrison\ITEC 3280\Coffee Shop\shoppingCart.php on line 39

foreach($_POST as $key -> $value) {
               
            $quantity = $value;
            $sku = $key;
            
            echo "<p>Inside loop to update cart<br/>
                  $quantity = ".$quantity."</p>";
            
            $sql = "UPDATE CART
                    SET quantity = ".$quantity."
                    WHERE userid = '".$userid."' AND sku = '".$sku."';";
                    
            echo $sql;
            
            mysqli_query($conn, $sql);
                    
            //$rs = mysqli_query($conn, $sql);
        }


#10
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
Ok... now I feel like you're not even trying...

There is a clear issue with your syntax here:
            echo "<p>Inside loop to update cart<br/>

                  $quantity = ".$quantity."</p>";


#11
Blue Indian

Blue Indian

    Learning Programmer

  • Members
  • PipPipPip
  • 67 posts
What is wrong with this line? This is just an output statement put there for debugging purposes. The compiler doesn't like the foreach loop. This line you are talking about seems to be working as I expect it to.

#12
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
key => value, not key -> value. Key is not an object and value is not a member.

And what John was pointing out, is you meant to write
"\$quantity = $quantity"
Otherwise your code would produce "2 = 2"
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.