Passing multiply variables in the url

Posted on 2005-04-11
Medium Priority
Last Modified: 2008-03-06
I am writing a shopping cart, I have run into a bit of an problem.
On my cart i have an update feature which works if i can post the value in the text box.

I have my cart being displayed in a table within a foreach loop, that gets the items from a class in a different file.
So say i have 10 items in the cart, how do i pass the 10 values in the text boxes? At present i have an update link on each item, but this still doesn't pass the value in the text box.  Ideally I would like to have one update button that passes all values.

Do I have the name of the text boxes as a variable incremented on each iteration of the loop?

I have thought of using a session array for this, but still cant use that if the value isnt being passed.  I keep thinking that its because the form isnt being submitted as i am using <a href....> to reload the cart.

This is the bit of code with the foreach loop, printing the cart to the screen within the table.
This is to have an update button on each row, if there is anyone out there willing to give up the secret to passing 'x' amount of values for the 170 points(all i have left) that would be great.

<TD align=\"center\"><input type='text' name='qty' size='1' value='".$prod['qty']."'>                        <a href = 'basket.php?item=".$prod['id']."&qty=".$_POST   ['qty']."&command=update'>update</a><br /></TD>
<TD align=\"center\">&pound;".number_format($prod['price'],2)."</TD></p>      
<TD align=\"center\">&pound;".number_format($prod['subtotal'],2)."</TD></TR></p>";

This is the code to get the values. On the same page but obviously only invoked when the command is update.

else if ($_GET['command'] = 'update'){ //this is to use if you put an update command in place
                        echo "else if item $item_id  $quant";

I'm not after being spoon fed answers, i love solving the problems but this is my first ever WEB project (I am pretty chuffed to have got this far) and i am out of resources (for once google has failed me!)
In case you want to see the the cart, goto www.ilt.bridgwater.ac.uk/~clementss/project/
click add on one of the items to see the cart. (Please note this site is completely fictional, its just me playing)
Thanks in advance for some pointers.
Question by:osiris247
  • 3
  • 3

Expert Comment

ID: 13756607
Hi osiris247,
> s is the bit of code with the foreach loop, printing the cart to the
> screen within the table.
> This is to have an update button on each row, if there is anyone out
> there willing to give up

you can use  the name fields of html elements also as an array when you use PHP
lets say you have $i as loop iritator and counter. 1 to 10 then you could use the following line

<TD align=\"center\"><input type='text' name='qty[".$i."]' size='1' value='".$prod[$i]['qty']."'>      

Then you can access this in PHP as $_POST['qty'][$i]

And to update all lines with a single line you shoud use as href="javascript:yourformname.submit();"
and then place everything in a form with a POST action.

Best regards,
Danny Ebbers

Author Comment

ID: 13757625
ok thanks Danny, that gave me loads to go on but!
i think understand whats going on here but after having a play it doesnt seem to agree with my code.  I think its to do with the way i have written the class functions to handle one update at a time.  As i take the original qty from an array within the class, adding a further array to this didnt seem right.  I think i will have to have a loop within the php to extract the value from the array and call the class function with that single value within the iteration. I will have a play with it later down the line but I just want the update function to work.

Back to the passing the variable in the url.... how can i pass this?
I have this now which will work but only for the final text box in the table, if i update any other within the loop they stay at 1, which is the default value passed from the shop page.

<input type='text' name='qty' size='1' value='".$prod['qty']."' />
<input type='hidden' name='command' value='update' />
<input type='hidden' name='item' value='".$prod['id']."' />
<a href ='javascript:update.submit();'>update</a>

So i guess i will have to have these text boxes name within an array and have a for loop on the page to go through each one?

I look forward to your reply, Its late here so i'll check in again tomorrow morning at work.

Thanks again

Accepted Solution

danny_ebbers earned 680 total points
ID: 13757702
<form name="updateform" action="yourphpscript.php" METHOD="POST">
    <input type='text' name='qty[1]' size='1' value='".$prod['qty']."' />
    <input type='text' name='qty[2]' size='1' value='".$prod['qty']."' />
    <input type='text' name='qty[3]' size='1' value='".$prod['qty']."' />

     <input type='hidden' name='command' value='update' />
     <input type='hidden' name='item' value='".$prod['id']."' />
     <a href ='javascript:updateform.submit();'>update</a>

you can access them then by a loop with a numeric iritator like i showed before

or with a foreach

foreach ($_POST['qty'] as $k => $v) {
    echo 'Quantity '.$k.' = '.$v."\n<BR>";

i gonna sleep as well and i won't be back before tomorrow evening
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 13757724
BTW pay attention that you never use $_REQUEST,

and the if statement $_GET['command'] == 'update' only works for get requests

so you should use $_POST['command']]

And you should read this i think:

Author Comment

ID: 13757748
I have actually used POST with the revised version, GET was when the form was a GET form.  Although i though REQUEST could be used for either.

Thanks I'll take a look

Author Comment

ID: 13767674
All working now, thanks Danny.

Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses
Course of the Month16 days, 15 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question