multiple values into one query

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
smartie2
Forum Newbie
Posts: 1
Joined: Mon Nov 30, 2009 10:00 pm

multiple values into one query

Post by smartie2 »

Okay I may have described the title wrong but here is what I am doing and I know how its suppose to work but I can't get it to work right.

Its a simple shopping cart I created and everything is prefect but it needs to submit multiple items to the cart at one time so say I need a hat and mittens, I add item id 1 and 2 to cart at the same time. Simple right? not totally...

Here is the code for submitting it:

Code: Select all

<form action="cart.php" action="POST">
<input type="hidden" name="action" value="add">
<select name="id">
<option value="1">one</option>
<option value="2">two</option>
<option value="3">three</option>
</select><br /><br />
<select name="id">
<option value="1">one</option>
<option value="2">two</option>
<option value="3">three</option>
</select><br /><br />
<input type="submit" value="submit">
</form>
Now when it posts to cart.php for just one item it comes out like this, and its suppose to this way
cart.php?action=add&id=3

Okay now when I use the drop downs and add two items at one time I get
cart.php?action=add&id=3&id=2

Its suppose to come out like this
cart.php?action=add&id=1,2,3

Am I crazy, is this even possible... somebody help me here!! Thanks in advance!
Ben11858
Forum Newbie
Posts: 16
Joined: Sat Nov 28, 2009 11:27 am

Re: multiple values into one query

Post by Ben11858 »

I might have got this wrong, but you made the method post so it won't come out like cart.php?action=add&id=1,2,3.. Url usually refers to Get method..

You have 2 different select forms, you should name them different so you can refer to them..

In your cart.php refer to the varibles like this to get the values $_POST['selectid1'] and you can get the other value from your other select using same method $_POST['selectid2'] for the 2 different variables then do whatever you need to the variables..

Like assign $varselectid1 = $_POST['selectid1']; and $varselectid2 = $_POST['selectid2']
if you want you can join those 2 variables together to do whatever you want too.. Hope that helps
User avatar
BlaineSch
Forum Commoner
Posts: 28
Joined: Sun Jun 07, 2009 4:28 pm
Location: Trapped in my own little world.

Re: multiple values into one query

Post by BlaineSch »

You can do this but it would require javascript... the submit button would use a onclick to get the values and make a hidden field value that. The hidden field would be "id" and the select boxes could not even have a name.

It might be easier to do this:

Code: Select all

<form action="cart.php" action="POST">
<input type="hidden" name="action" value="add">
<select name="id[]">
<option value="1">one</option>
 <option value="2">two</option>
<option value="3">three</option>
</select><br /><br />
<select name="id[]">
<option value="1">one</option>
<option value="2">two</option>
<option value="3">three</option>
</select><br /><br />
<input type="submit" value="submit">
</form>
This will look like:
page.php?id[]=1&id[]=2

Which will read in as an array.
Post Reply