SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Get multiple values onChange?!?!

    I'm trying to figure out how I can pass multiple values to my javascript from one selectbox.

    This is what I got now:
    Code JavaScript:
    function Update(item)
    {
    	product_id = item.name;
    	price_id = item.price;
    	color_id = item.color;
    	newQty = item.options[item.selectedIndex].text;
    	document.location.href = 'index.php?page=myCart&product_id='+product_id+'&price_id='+price_id+'&color_id='+color_id+'&qty='+newQty+'&action=update_product';
    }

    The form element:
    PHP Code:
    <select name="<? echo $product_id?>" onchange="Update(this);"> 
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
    </select>
    As you can see the product_id get passed to the javascript, but it is essential that I get the price and color id passed along... Is that possible, and if yes...HOW???

    Please help and thanks in advance :-)

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Where would the color and price be? Maybe you have other form elements?

  3. #3
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,786
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    To be able to select multiples you need

    <select name="<? echo $product_id; ?>" onchange="Update(this);" multiple>

    The multiple selections should then be passed to the JavaScript instead of just the one so that rather than just getting the selectedIndex you'd need to loop through the array and see which are selected.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  4. #4
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by crmalibu View Post
    Where would the color and price be? Maybe you have other form elements?
    `

    The Color and price is part of the product as the product_id. Imagine that I have a bunch of products in a loop, and the quantity is a option for each product...

    So mayby if there is a way to comma seperate the ids in the selectname like this:
    PHP Code:
    <select name="<? echo $product_id.','.$price_id.','.$color_id?>" onchange="Update(this);">
    And then somehow in the javascript split the (item) up by the commas... Just dont know if thats possible or if so, how?!?!?

  5. #5
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    And then... Solved it...:-)

    Here is what I did:
    PHP Code:
    <select name="<? echo $row['product_id'].'/'.$row['price_id'].'/'.$row['color_id']; ?>" onchange="Update(this);">
    And in the javascript:
    Code JavaScript:
    var myString = item.name;
    	myArray = myString.split("/");
     
    	product_id =  myArray[0];
    	price_id =  myArray[1];
    	color_id =  myArray[2];

    Voila... Thanks alot for your help guys ;-)

  6. #6
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Unless there's some compelling reason to do something mucky like build a comma delimited string as the name, I wouldn't.

    A function can take multiple arguments.
    Code:
    function Update(selectElement, color, price) {
        ...
    }
    Then you could just embed the values into the html.
    Update(this, 'red', 5.99);

    But...is there a reason why you aren't just using a form(s)? If want the form to submit automatically when the user makes a selection from a menu, you can do that while still using a form. Then you don't require javascript for it to work.

    onchange="this.form.submit()"

    And then just make some input elements for the color and price.

    I'm not sure how you're handling this, but thought I'd mention that you should make sure the user doesn't change the price. So check it against a list, or create some kind of numerical id that maps to a specific combination of name, color, and price, that only you know about, and just use that as the id in the html.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •