Hi Guys,

As part of an online store, I have a dropdown box where the user selects a size. If the user selects size L then the 'add to cart' button needs to link to a page different than if the user selected size M.

I figured out that I can only grab the selected value for PHP to process if it was submitted, i.e., through a form. Then I could do something like this:

if($_POST['dropdown'] == 'M') {
  $product = 'link';
} else if {
  //do something else
}

But I am not sure if this is possible using a button... I'm still a beginner here, but any thoughts?

Member Avatar for diafol

You can have a bank of buttons (like radio buttons). However, I'm unsure why you'd want to link to a different 'page'. I'd have thought that size was just a property of the product, just some piece of data to be carried on with the product_id.

<label><input type="radio" name="size" value="S" checked />S</label>
<label><input type="radio" name="size" value="M" />M</label>
<label><input type="radio" name="size" value="L" />L</label>
<label><input type="radio" name="size" value="XL" />XL</label>

Always make sure you have a default checked, otherwise errors can ensue if a user submits without checking a size.

Then you pick it up as:

$size = $_POST['size'];
$product_id = $_POST['product_id'];
$quantity = $_POST['quantity'];

These would require validation.

Of course, most baskets are 'ajaxed', so that you don't lose the page you're on and the item appears 'magically' in the basket, e.g. "BASKET Items: 3"

The HTML given will give you radio buttons, which visual-wise may not be what you want, BUT there are many, many CSS snippets that can give them the appearance of buttons. Sorry, can't help further off on vacation.

maybe using a <select><option> was what you wish for, by using then onchange event from to select box using jquery/javascript, you can easily change the form's attribute. You may even prevent default action from the form submit using jquery function and based on the user's selection and then submit the form using jquery.

Cheers guys. I was looking at this fairly weirdly, but have manage to sort just by using the form info :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.