Jump to content

Recommended Posts

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:

<form action="cart.php" action="POST" enctype="multipart/form-data">
<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!

 

Link to comment
https://forums.phpfreaks.com/topic/183525-multiple-values-into-one-url-query/
Share on other sites

no, that doesn't make sense actually. Your two select tags have the same name. So the second one will overwrite the first. You can either make them different names, or make them an array, like so

<select name="id[]" >

 

if you choose to make an array,  you can access them like

$_GET['id'][0];//first one
$_GET['id'][0];//second one

 

if you choose to make an array,  you can access them like

$_GET['id'][0];//first one
$_GET['id'][0];//second one

 

Correction

[code=php:0]
$_GET['id'][0];//first one
$_GET['id'][1];//second one

[/code]

 

That aside. Your idea is in the right place, however. Considering what you want to do, and how you have it laid out...

 

<select name="[b]id[/b]">
<option value="[b][color=red]1[/color][/b]">one</option>
<option value="[b][color=red]2[/color][/b]">two</option>
<option value="[b][color=red]3[/color][/b]">three</option>
</select><br /><br />
<select name="[b]id[/b]">
<option value="[b][color=red]1[/color][/b]">one</option>
<option value="[b][color=red]2[/color][/b]">two</option>
<option value="[b][color=red]3[/color][/b]">three</option>
</select>

 

id is going to equal 1, 2, or 3 no matter what. id is also going to be id, whether you put it in an array as 1,3 as suggested prior by someone else. But both things are different one is for hats one is for gloves, but both carry ID as a name. Soo when you add it whats it going to add as for starters?

 

Your best bet is to give each form element its own unique name instead of "id" try "hats" and "mittens"

 

Since your posting the data through a form you don't need it to run it through the URL as a variable in order to catch what the inputs were.

 

so try this in a dummy page:

Your Code:

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" name="add2a">
<select name="[b]id[/b]">
<option value="[b][color=red]1[/color][/b]">one</option>
<option value="[b][color=red]2[/color][/b]">two</option>
<option value="[b][color=red]3[/color][/b]">three</option>
</select><br /><br />
<select name="[b]id[/b]">
<option value="[b][color=red]1[/color][/b]">one</option>
<option value="[b][color=red]2[/color][/b]">two</option>
<option value="[b][color=red]3[/color][/b]">three</option>
</select>
<input type="submit" name="add2cart" value="Add to Cart">
</form>
<?php if(empty($_POST['id'])){}else{echo $_POST['id'];} ?>

 

select an option from both (both selections different). notice which one gets printed out after form submission. Chances are it will always be the second form elements choice or it will some how manage to combine both (though I dont think that be the case, but im not testing as I go either).

 

Now try

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" name="add2b">
<select name="[b]gloves[/b]">
<option value="[b][color=red]1[/color][/b]">one</option>
<option value="[b][color=red]2[/color][/b]">two</option>
<option value="[b][color=red]3[/color][/b]">three</option>
</select><br /><br />
<select name="[b]hats[/b]">
<option value="[b][color=red]1[/color][/b]">one</option>
<option value="[b][color=red]2[/color][/b]">two</option>
<option value="[b][color=red]3[/color][/b]">three</option>
</select>
<input type="submit" name="add2cart" value="Add to Cart">
</form>
<?php
if(empty($_POST['gloves'])){}else{echo $_POST['gloves']."<br />";}
if(empty($_POST['hats'])){}else{echo $_POST['hats']."<br />";}
?>

Should print out both now. Now what you can do with this, is rather than have it echo out for display you can run your add cart function so it adds each one independently of the other as I think you want it to rather than have it post the the way you are having it post currently. If ya need more help with the script itself, ill be happy to help if you want to send me the full file.

 

 

 

 

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.