Jump to content

Change PHP variable by option select


Spector

Recommended Posts

Hello everyone.

 

I am stuck with creating "price change".  I have PHP array with sizes+prices with strings like: "XXL+350Kc".

 

The part before "+" is shown as an option in select and the part between "+" and "K" is the value that I want to add to an initial price when an option is selected (without the page reload).  I know this is time to use JS. But JS and me still are not the best friends  :( So I would like to ask you for help.

 

This is light version of my code:

<div class=\"productProp\"><b>Price:</b></div><div class=\"productProp\">".$item['price']." Kc</div>";

if ($item['size'] <> "")
{
  $size = explode(";", $item['size_value']);     // getting the array of sizes+prices

  echo "<div class=\"productProp\">SIZE:</div> 
  <div class=\"productProp\"><select name=\"size\">";
    for ($i=0; $i<count($size); $i++)
    {
      $parsed_price[$i] = get_string_between($size1[$i], "+", "K");                //part with price - function is working so I am not including it to save space
      $parsed_size[$i] = substr($size[$i],0,strrpos($size[$i],'+'));                    //part with size
      echo "<option value=\"".$parsed_size[$i]."\" >".$parsed_size[$i]."</option>"; 
    }
  echo "</select></div>";
}

Link to comment
Share on other sites

if you want to use javascript, which is correct for what you say, a library like jquery would facilitate this.

 

what you have posted is irrelevant in some respects to your query, its php code to populate the dropdown, really this should be moved to the javascript section, as what you want to do is going to be javascript

 

try adding onchange="alert(this.value.selected);" into the select tag (i havent checked that so it might now work) it should alert the selected value.

also you dont appear to be using the parsed price for anything.

Link to comment
Share on other sites

Oh, I am sorry about that, I didn't notice the JS section :-[. Nevermind, you kind of set me on the right path with the onchange="alert(this.value.selected);" (I was trying some really complicated things... don't know why) Than it was really easy to figure it out and it's working flawelessly now. Thanks  8)

Link to comment
Share on other sites

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.