Spector Posted August 14, 2012 Share Posted August 14, 2012 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>"; } Quote Link to comment https://forums.phpfreaks.com/topic/267053-change-php-variable-by-option-select/ Share on other sites More sharing options...
spiderwell Posted August 14, 2012 Share Posted August 14, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/267053-change-php-variable-by-option-select/#findComment-1369292 Share on other sites More sharing options...
Spector Posted August 15, 2012 Author Share Posted August 15, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/267053-change-php-variable-by-option-select/#findComment-1369520 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.