SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast JCsuperstar's Avatar
    Join Date
    Mar 2005
    Location
    Many places.
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help with pricing module.

    Hello,

    I've been posting related questions but have not had much luck, so I figured I'd just put the whole problem out there.

    I have 2 separate prices for each product in a database: 'New' and 'Like New'. These products also have accessories which can be added to the products' package each with 2 prices. I want to change the pricing for all of the accessories by clicking on a radio button selection. So, without knowing javascript at all, I am trying to make it so that the <input name="Price[]" value="this should change"> tags and the text of the Price change when you select the 'Like New' radio button and back when you select the 'New' radio button.

    My strategy this far has been to pull all of the prices into an array like this:

    Code:
    nPrice = new Array ("106.99" , "86.99" , "115.99" , "319.99" , "")
    rPrice = new Array ("89.99" , "74.99" , "102.99" , "239.99" , "")
    I then use this code to set which radio button is selected:

    Code:
    function setCheckedValue(radioObj, newValue) {
    	if(!radioObj)
    		return;
    	var radioLength = radioObj.length;
    	if(radioLength == undefined) {
    		radioObj.checked = (radioObj.value == newValue.toString());
    		return;
    	}
    	for(var i = 0; i < radioLength; i++) {
    		radioObj[i].checked = false;
    		if(radioObj[i].value == newValue.toString()) {
    			radioObj[i].checked = true;
    		}
    	}
     }
    Next I use this code to set the values for the Price inputs (it doesn't work):

    Code:
    function GetThePrices(){
     var priceLength = document.choices.Price.length;
        if (radioObj[0].checked == true){
     for(var i = 0; i < priceLength; i++) {
        document.choices.Price[i].value = nPrice[i];
        return document.choices.Price[i].value;   
      }
       if (radioObj[1].checked == true){
     for(var i = 0; i < priceLength; i++) {
        document.choices.Price[i].value = rPrice[i];
        return document.choices.Price[i].value;  
      }
    }
    Finally, I use this code to display the prices (it works on loading but will not change):

    Code:
    if (document.choices.New[0].checked == true) {
            var Price = nPrice[$i];
    }
    if (document.choices.New[1].checked == true) {
           	var Price = rPrice[$i];
    }
    	document.write(Price)
    This doesn't change the pricing text and when I submit the form there are no values for the 'Price' inputs. What am I doing wrong? How can I make the onclick events change the text and set the values of the prices? Should I name the array the same and just change the variables inside?

    Please help!!!!!

    Thanks,

    Colin

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    u have to trigger it with some function call
    regards

  3. #3
    SitePoint Enthusiast JCsuperstar's Avatar
    Join Date
    Mar 2005
    Location
    Many places.
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Alright, it was easier than I was making it out to be.

    I just made the radio buttons do this: onchange = "GetPrices();".

    Then the I used PHP (
    $i=0;
    $products_query = ( "SELECT THE SAME AS WHEN YOU ACTUALLY CALL THE PRODUCTS");
    while($result = mysql_fetch_array($products_query) {
    echo "DoIt$i();";
    $i++;
    }
    )
    in the GetPrices function to create a switch of numbered function calls (i.e. DoIt0 (); DoIt1();, etc.) corresponding to the number of products.

    Then for each product $i there was the function (function DoIt0 (), function DoIt1(), etc.) that detected which radio button is checked and set whatever variables I needed passed on (
    if (document.choices.New[0].checked == true) {
    var addPr = $price;
    var addStat = '$nstatus';
    }
    )

    The values and text were then set using:
    document.getElementById("Prices0", "Prices1", etc.).innerHTML (to display text you can't use document.write) and
    document.getElementById("Status0", "Status1", etc.).value.
    etc.

    This changes the values and text for all products when different radio buttons are checked.

    Hopefully, this will help anybody with the same problem.

    Thanks,

    Colin


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
  •