Jump to content

Using an Array in a form post--Help please (SOLVED!!!) :)


phencesgirl

Recommended Posts

Hello.  I am trying to pass a variable as an array in a form post.  I have searched the PHP manual, but I cannot find the answer in it, as I am a newbie with PHP.  So far, I have only been able to pass one value at a time for the post variable 'newquantity'.  Here is the code for that:

[code]
<?php
if (isset($_POST['update'])) { //if the quantity is being changed for an item
//rename a common variable
$pid = $_POST['pid'];

//get the product's ordered from the cart table
$sql = "SELECT * FROM cart WHERE username = '$uid' AND confirmed = 'no' AND product_id = '$pid'";
$result = mysql_query($sql) or die(mysql_error());

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
extract($row);
//set the variables
$stock = $row['stock'];
$newquantity = $_POST['newquantity'];

//first, check if the user's selected quantity is greater than the stock quantity
if ($newquantity > $stock) {  //if it is, then set the order quantity to equal the stock quantity
$sql = "UPDATE cart SET quantity = '$stock' WHERE username = '$uid' AND confirmed = 'no' AND product_id = '$pid'";
mysql_query($sql) or die(mysql_error());
} else {
//put the current newquantity into the cart table
    $sql = "UPDATE cart SET quantity = '$newquantity' WHERE username = '$uid' AND confirmed = 'no' AND product_id = '$pid'";
mysql_query($sql) or die(mysql_error());
} //end else
} //end while
                   } //end if

         //now let the user view their cart, if it exists.
        // Select * FROM cart WHERE username == "$uid"
        $sql = "SELECT * FROM cart WHERE username = '$uid' ORDER BY product_id";
        $result = mysql_query($sql) or die(mysql_error());
       
        //***make a while loop here that will repeat the table row for each item in the wish table for this user
       while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
                extract($row);
?>

<table><tr>
<form action="/shop/main_shop.php?PGNM=cart" method="post"> <!--this posts to this same script-->
<input type="hidden" name="pid" value="<?php echo $row['product_id']; ?>" />
<td><input type="text"  size="3" name="newquantity" maxlength="5"/></td> <!--'newquantity'--this is the variable that I would like to pass in the form of an array, so that the quantity can be changed on multiple items, with only one click on 'Change Qty' button-->
<td><input type="submit" name="update" value="Change Qty" /></td>
</form>
</tr></table>

<?php
} //end while loop
?>
[/code]

I am having trouble understanding how to pass an array over the form post.  Any help or suggestions will be greatly appreciated.  Thank you so much in advance! 
Link to comment
Share on other sites

newquantity is an array, which is $_POST['newquantity']. $_POST is an array of all the POST'd data from the form.

Could you define what you mena by an array. DO you want the newquantity text field to hold an array of values, eg 1,3,5,1 each number after the comma is an item within the newquantity array?

Link to comment
Share on other sites

Then you'll want to do is create seperate fields for each product that user has in thier basket. You give the each form field the same name, but you suffix the name with two square brackets ([]) eg pid[] newquantity[]

When you do that the data will be submit as an array. To access product 1 you use $_POST['pid][0] and $_POST['newquantity'][0] to access product 2 you use $_POST['pid][1] and $_POST['newquantity][1] etc. Remeber artrays start at zero.

You're best of creatting the form fields dynamically. How do you store each product in the users basket?
Link to comment
Share on other sites

WOW!  Thank you so much!!  I think that this is what I am looking for. 

I am storing the each product in the cart table, with the username and date and confirmed (yes or no) to distinguish between different 'shopping baskets'.

Well, I am going to try to implement your solution now, and I will reply back again with the results.  Thank you again!
:)
Link to comment
Share on other sites

ok...I don't think I've implemented this correctly.  Here is my revised code:

[code]
<?php

if (isset($_POST['update'])) { //if the quantity is being changed for an item


//get the product's ordered from the cart table
$sql = "SELECT * FROM cart WHERE username = '$uid' AND confirmed = 'no'";
$result = mysql_query($sql) or die(mysql_error());

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
extract($row);

//set the variables
$pid = $_POST['pid'][0];
$newquantity = $_POST['newquantity'][0];
$stock = $row['stock'];

foreach ($_POST as $pid => $newquantity) {

//first, check if the user's selected quantity is greater than the stock quantity
if ($newquantity > $stock) {
$sql = "UPDATE cart SET quantity = '$stock' WHERE username = '$uid' AND confirmed = 'no' AND product_id = '$pid'";
mysql_query($sql) or die(mysql_error());
} else {
//put the current newquantity into the cart table
$sql = "UPDATE cart SET quantity = '$newquantity' WHERE username = '$uid' AND confirmed = 'no' AND product_id = '$pid'";
mysql_query($sql) or die(mysql_error());
} //end else
} //end foreach
} //end while

} //end foreach
        //now let the user view their cart, if it exists.
        // Select * FROM cart WHERE username == "$uid"
        $sql = "SELECT * FROM cart WHERE username = '$uid' ORDER BY product_id";
        $result = mysql_query($sql) or die(mysql_error());
       
        //***make a while loop here that will repeat the table row for each item in the wish table for this user
      while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
                extract($row);
?>

<table><tr>
<form action="/shop/main_shop.php?PGNM=cart" method="post"> <!--this posts to this same script-->
<input type="hidden" name="pid[]" value="<?php echo $row['product_id']; ?>" />
<td><input type="text"  size="3" name="newquantity[]" maxlength="5"/></td> <!--'newquantity'--this is the variable that I would like to pass in the form of an array, so that the quantity can be changed on multiple items, with only one click on 'Change Qty' button-->
<?php
} //end while loop
?>

<td><input type="submit" name="update" value="Change Qty" /></td>
</form>
</tr></table>
[/code]

I added the foreach loop to go through the array items, and update the cart table with each new quantity.  However, the script is not updating the cart table with the new quantities for each product.  Is 'foreach' the wrong way to go about this?  I am trying to allow the customer to choose the quantities that they want to order of each product, by inputting those quantities into a seperate text field for each product, and clicking on one 'update qty' button at the bottom of the page.  Any help is appreciated!  Thank you!
Link to comment
Share on other sites

well I think your problem is that you are making a new form everytime you loop thru the items in the cart. You need to put the <form> tag before the while loop then close it after the loop. The way you have it now you are creating X amount of forms for each item in the cart. The way you have it you are really only getting post data from one item in the cart.

Try your form this way
[code]<?php         //now let the user view their cart, if it exists.
?>
<form action="/shop/main_shop.php?PGNM=cart" method="POST"> <!--this posts to this same script-->
<table>
<?php
        // Select * FROM cart WHERE username == "$uid"
        $sql = "SELECT * FROM cart WHERE usrname = '$uid' ORDER BY product_id";
        $result = mysql_query($sql) or die(mysql_error());

        //***make a while loop here that will repeat the table row for each item in the wish table for this user
       while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
                extract($row);
?>
  <tr>
<input type="hidden" name="pid" value="<?php echo $row['product_id']; ?>" />
<td><input type="text"  size="3" name="newquantity" maxlength="5" value="<?=$row['quantity']?>"/></td> <!--'newquantity'--this is the variable that I would like to pass in the form of an array, so that the quantity can be changed on multiple items, with only one click on 'Change Qty' button-->
</tr>


<?php
} //end while loop
?>
<tr>
<td><input type="submit" name="update" value="Change Qty" /></td>
</tr>
</table>
</form>[/code]

Ray
Link to comment
Share on other sites

This is untested code so it may work. But try this:
[code]<?php

// if the quantity is being changed for an item
if (isset($_POST['update']))
{
    // get the product's ordered from the cart table
$sql = "SELECT * FROM cart WHERE username = '$uid' AND confirmed = 'no'";
$result = mysql_query($sql) or die(mysql_error());

    while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
    {
    extract($row);

        // set the variables

        foreach ($_POST['pid'] as $pid_key => $pid)
        {
    $newquantity = $_POST['newquantity'][$pid_key];
    $stock      = $row['stock'];

            // first, check if the user's selected quantity is greater than the stock quantity
if ($newquantity > $stock)
            {
    $sql = "UPDATE cart SET quantity = '$stock' WHERE username = '$uid' AND confirmed = 'no' AND product_id = '$pid'";
mysql_query($sql) or die(mysql_error());
}
            else
            {
    // put the current newquantity into the cart table
$sql = "UPDATE cart SET quantity = '$newquantity' WHERE username = '$uid' AND confirmed = 'no' AND product_id = '$pid'";
mysql_query($sql) or die(mysql_error());
} // end else
} // end foreach
} // end while
} // end if

// now let the user view their cart, if it exists.
// Select * FROM cart WHERE username == "$uid"
$sql = "SELECT * FROM cart WHERE username = '$uid' ORDER BY product_id";
$result = mysql_query($sql) or die(mysql_error());
?>
<form action="/shop/main_shop.php?PGNM=cart" method="post">
  <table>
    <tr>
<?php
//***make a while loop here that will repeat the table row for each item in the wish table for this user
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
?>
      <td>
        <input type="hidden" name="pid[]" value="<?php echo $row['product_id']; ?>" />
        <input type="text"  size="3" name="newquantity[]" maxlength="5"/>
      </td>
<?php
} //end while loop
?>
      <td><input type="submit" name="update" value="Change Qty" /></td>
    </tr>
  </table>
</form>[/code]
Link to comment
Share on other sites

I noticed also, there is no need for the 1st query. You already have the $pid so there is not need to query the cart again. just run your foreach loop THEN sql the row to get the stock amount then update the table. With the help of wildteens foreach statement this works fine for me.

[code]<?php
if (isset($_POST['update'])) { //if the quantity is being changed for an item
    foreach($_POST['pid'] as $pid_key => $pid){
    $newquantity = $_POST['newquantity'][$pid_key];
      //get current product stock
  $sql = "SELECT stock FROM cart WHERE product_id = '$pid'";
$result = mysql_query($sql) or die(mysql_error());
          $row = mysql_fetch_array($result, MYSQL_ASSOC);
            //set the variables
$stock = $row['stock'];
            //first, check if the user's selected quantity is greater than the stock quantity
if ($newquantity > $stock) {  //if it is, then set the order quantity to equal the stock quantity
$sql = "UPDATE cart SET quantity = '$stock' WHERE username = '$uid' AND status = 'no' AND product_id = '$pid'";
mysql_query($sql) or die(mysql_error());
} else {
//put the current newquantity into the cart table
    $sql = "UPDATE cart SET quantity = '$newquantity' WHERE username = '$uid' AND status = 'no' AND product_id = '$pid'";
mysql_query($sql) or die(mysql_error());
} //end else
} //end foreach
} //end if

        //now let the user view their cart, if it exists.
?>
<form action="/shop/main_shop.php?PGNM=cart" method="POST"> <!--this posts to this same script-->
<table>
<?php
        // Select * FROM cart WHERE username == "$uid"
        $sql = "SELECT * FROM cart WHERE username = '$uid' ORDER BY product_id";
          $result = mysql_query($sql) or die(mysql_error());
          //***make a while loop here that will repeat the table row for each item in the wish table for this user
            while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
            extract($row);
?>
  <tr>
<input type="hidden" name="pid[]" value="<?php echo $row['item_id']; ?>" />
<td><input type="text"  size="3" name="newquantity[]" maxlength="5" value="<?=$row['quantity']?>"/></td> <!--'newquantity'--this is the variable that I would like to pass in the form of an array, so that the quantity can be changed on multiple items, with only one click on 'Change Qty' button-->
</tr>
<?php
} //end while loop
?>
<tr>
<td><input type="submit" name="update" value="Change Qty" /></td>
</tr>
</table>
</form>[/code]
Link to comment
Share on other sites

Thank you all for helping this newbie!  :)
Thank you both for your replies.  

I have now put the form tags outside of the while loop, and have changed my script to reflect the one from craygo.  I have two items in my test shopping cart.  As the script is now, whichever number I input as the quantity of the first item, that number is put into the cart table as the quantity of the [b]second [/b] item in my cart.  Obviously, this is not right.   I'm not sure where to go from here...but I'll keep trying!  

Thanks again for your help!
Link to comment
Share on other sites

Ok, here is the latest code:

[code]
<?php

if (isset($_POST['update'])) { //if the quantity is being changed for an item

foreach($_POST['pid'] as $pid_key => $pid){
$newquantity = $_POST['newquantity'][$pid_key];
//get current product stock
$sql = "SELECT stock FROM cart WHERE product_id = '$pid'";
$result = mysql_query($sql) or die(mysql_error());
                $row = mysql_fetch_array($result, MYSQL_ASSOC);

//set the variables
$stock = $row['stock'];
//first, check if the user's selected quantity is greater than the stock quantity
if ($newquantity > $stock) {  //if it is, then set the order quantity to equal the stock quantity
$sql = "UPDATE cart SET quantity = '$stock' WHERE username = '$uid' AND product_id = '$pid'";
mysql_query($sql) or die(mysql_error());
} else {
//put the current newquantity into the cart table
$sql = "UPDATE cart SET quantity = '$newquantity' WHERE username = '$uid' AND product_id = '$pid'";
mysql_query($sql) or die(mysql_error());
} //end else
} //end foreach

} //end if

    // Select * FROM cart WHERE username == "$uid"
    $sql = "SELECT * FROM cart WHERE username = '$uid'";
    $result = mysql_query($sql) or die(mysql_error());
?>
<form action="/test_0706/shop/main_shop.php?PGNM=cart" method="post">
<table>
<?php
//***make a while loop here that will repeat the table row for each item in the cart table for this user
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
extract($row);
?>
<tr><input type="hidden" name="pid" value="<?php echo $row['product_id']; ?>" />
<td><input type="submit" name="remove" value="Remove"  /></td>
<td><?php echo $row['product_id']; ?></td>
<td>$<?php echo $row['price']; ?></td>
<td><a href="<?php echo $row['url']; ?>">View Item</a></td>
<td align="center">
<?php
//put stock quantity here
echo $stock;
?>
</td>
<input type="hidden" name="pid[]" value="<?php echo $row['product_id']; ?>" />
<td><input type="text"  size="3" name="newquantity[]" maxlength="5" value="<?=$row['quantity']?>"/></td>
<td align="center"><?php echo $row['quantity']; ?></td></tr>
<?php
} //end while loop
?>
<tr><td><input type="submit" name="update" value="Change Qty" /></td></tr>
</form>
[/code]

Like I said in my last post, I have a test cart with two products in it.  I am still having the problem where the number in the input field for the [b]first [/b] product is put into the cart table as the quantity of the [b]second [/b] product.  The quantity of the first product is never changed, no matter what numbers I put in the first and second input fields. 

All help is appreciated! 
Link to comment
Share on other sites

Yes!!  I don't have that problem anymore!  However, I have had to really expand the code, and now the remove button is not working properly!  Oh well, I think I can figure [i]that [/i] one out, as I have had similar problems before. Well, thank you all so much for your help everyone!  And here is the script as it now stands:
[code]
<?php

    if (isset($_POST['update'])) { //if the quantity is being changed for an item

foreach($_POST['pid'] as $pid_key => $pid){
$newquantity = $_POST['newquantity'][$pid_key];
//get current product stock
$sql = "SELECT stock FROM cart WHERE product_id = '$pid'";
$result = mysql_query($sql) or die(mysql_error());
    $row = mysql_fetch_array($result, MYSQL_ASSOC);
//set the variables
$stock = $row['stock'];
//first, check if the user's selected quantity is greater than the stock quantity
if ($newquantity > $stock) {  //if it is, then set the order quantity to equal the stock quantity
$sql = "UPDATE cart SET quantity = '$stock' WHERE username = '$uid' AND product_id = '$pid'";
mysql_query($sql) or die(mysql_error());
} else {
//put the current newquantity into the cart table
$sql = "UPDATE cart SET quantity = '$newquantity' WHERE username = '$uid' AND product_id = '$pid'";
mysql_query($sql) or die(mysql_error());
} //end else
} //end foreach

} //end if

    // Select * FROM cart WHERE username == "$uid"
    $sql = "SELECT * FROM cart WHERE username = '$uid'";
    $result = mysql_query($sql) or die(mysql_error());
?>
<table><tr>
<form action="/test_0706/shop/main_shop.php?PGNM=cart" method="post">
<?php
//***make a while loop here that will repeat the table row for each item in the cart table for this user
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
extract($row);
?>
<input type="hidden" name="pid" value="<?php echo $row['product_id']; ?>" />
<td><input type="submit" name="remove" value="Remove"  /></td></tr>
<?php
} //end while
?>
</form>
<tr>
<form action="/test_0706/shop/main_shop.php?PGNM=cart" method="post">
<?php
// Select * FROM cart WHERE username == "$uid"
  $sql = "SELECT * FROM cart WHERE username = '$uid'";
$result = mysql_query($sql) or die(mysql_error());
//***make a while loop here that will repeat the table row for each item in the cart table for this user
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
extract($row);
?>

<td><?php echo $row['product_id']; ?></td>
<td>$<?php echo $row['price']; ?></td>
<td><a href="<?php echo $row['url']; ?>">View Item</a></td>
<td align="center">
<?php
//put stock quantity here
echo $stock;
?>
</td>
<input type="hidden" name="pid[]" value="<?php echo $row['product_id']; ?>" />
<td><input type="text"  size="3" name="newquantity[]" maxlength="5" value="<?=$row['quantity']?>"/></td>
<td align="center"><?php echo $row['quantity']; ?></td>
</tr>
<?php
} //end while
?>
<tr><td><input type="submit" name="update" value="Change Qty" /></td>
</form>
</tr>
</table>
[/code]

Thank you again!    :)
Link to comment
Share on other sites

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.