How do I add multiple variables to a URL using javascript?

advertisements

I am having trouble appending multiple variables to the end of a url using javascript. The user provides input on the page using a select menu and textarea. Using just the textarea works, but I’m not able to include the select menu.

<?php
$price = $_GET['price'];
$title = $_GET['title'];
$retailer = $_GET['retailer'];
$img = $_GET['img'];
$link = $_GET['link'];
$desc = $_GET['desc'];
?>

<textarea rows="3" id="textarea" class="input-xlarge" placeholder="Size, color, style, etc" name ="options"></textarea>

<select style="width: 130px;" id="quantity" class="span2">
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        </select>

<script language="javascript">
    function test()
{
    var val=document.getElementById("textarea").value;
    var val=document.getElementById("quantity").value;
    var hrf="viewcart.php?retailer=<?php echo $retailer?>&link=<?php echo $link; ?>&price=<?php echo $price; ?>&title=<?php echo $title; ?>&options="+val;
    document.getElementById("a_link").href=hrf;
}
</script>

<a href ="#" id="a_link" onclick="test();" class="btn btn-success" type="submit">
    <i class="icon-shopping-cart icon-white"></i>  Add to Cart
</a>

The line I am trying to alter is:

    var hrf="viewcart.php?retailer=<?php echo $retailer?>&link=<?php echo $link; ?>&price=<?php echo $price; ?>&title=<?php echo $title; ?>&options="+val;

I tried to have the below but it didn’t work:

    var hrf="viewcart.php?retailer=<?php echo $retailer?>&link=<?php echo $link; ?>&price=<?php echo $price; ?>&title=<?php echo $title; ?>&options="+val + “&quantity=” + quantity;

Any ideas on how to get this to work?


Use this buddy,

var quantity = document.getElementById("quantity").options[document.getElementById("quantity").selectedIndex].value;

var val=document.getElementById("textarea").value;

var hrf="viewcart.php?retailer=<?php echo $retailer?>&link=<?php echo $link; ?>&price=<?php echo $price; ?>&title=<?php echo $title; ?>&options="+val + “&quantity=” + quantity;