demlasjr
demlasjr

Reputation: 121

Showing value data from dropdown in html

I created a script which get from my database the states and price of shipping by number of products.

So...I have 2 dropdown, one for quantity and the second one for state.

This is my main php file: http://pastebin.com/U6dncsZ6
This is the second php file (for database connect) - findcity.php : http://pastebin.com/AuEXrnpD

As you see, in my second php file I have this line:

<option value=<?=$row['price']?>><?=$row['title']?></option>

I want to display the shipping price from the database ($row['price'] on the page. I specify that using "Inspect element" in my browser I can see the prices added as value for the states, like:

<option value="48.11">State</option>

Please help :(

Le: As I stated in a comment, I finally found that like here it's working: jsfiddle.net/Dkn4n/1/ But for some reasons, it's not detected but it's not working in my page, maybe because the value is not set in html, I can't see the values in source, only in "Inspect element". The value is set as I specified above, with $row['price']

Upvotes: 2

Views: 179

Answers (2)

snitch182
snitch182

Reputation: 723

Adding to "Chase"s Answer:

$(document).ready(function(){
 $("#provincia").change(function(){
   $("#selectedprice").html(this.value);
   //<div id=selectedprice></div> should show  your value
 });
});  

and expanding it:

at this line: document.getElementById('provinciadiv').innerHTML=req.responseText;

do

    $("#provincia").change(function(){
       $("#selectedprice").html(this.value);
       //<div id=selectedprice></div> should show  your value
     });

again! Because the "change" binding is gone once you replace the part...

Upvotes: 1

Chase
Chase

Reputation: 29549

Your select statement looks like this on the page:

<select id="provincia">
    <option value="10.00">CA</option>
    <option value="20.00">NY</option>
</select>

So using javascript/jQuery you can do the following to get the value of the selected option.

$(document).ready(function(){
 $("#provincia").change(function(){
   alert(this.value);
   //do stuff here to set the value where you want it.
 });
});  

Here's a demo: http://jsfiddle.net/Dkn4n/

Upvotes: 2

Related Questions