php - Drop down list values linking with mySQL values issue

94

I have drop down lists to convert one currency to another after a user enters a value into a text box, selects the currency symbols (USD->AUD for example) and then clicks convert and the currency converted should be output onto the page.

This is the php code that grabs the data from the database from the tables currency and rate and assigns to the options value to be used in the drop down lists.

while ($row=mysql_fetch_array($refDB)) { 

$currency=$row["currency"];
$rate=$row["rate"];     

$options.="<option/$rate/>".$currency;
}

Here are the drop down lists that populate the data. So far the symbols are being populated but I am unsure if I am passing the rates properly through the drop down lists as those are the values I am using.

<select id = "convert1" width="10"> 
<option value=0>From 
<?=$options?> 
</select> 

<select id = "convert2" width="10">  
<option value=0>To 
<?=$options?> 
</select> 

After the user selects the "Convert Currency Button":

                <input type="button" name="convertCurrency" value="Convert Currency" onClick="entry.value=convert(entry);">

This is where the user enters the value they want to convert:

<input type="text" id="enter" name="entry" size="18" value = "0"> 

It is taken to the JavaScript to convert the currency:

function convert(entry)
{



var convert1 = document.getElementById("convert1");
convert1.options[convert1.selectedIndex].value;

var convert2 = document.getElementById("convert2");
convert2.options[convert2.selectedIndex].value;


var sum = (entry/convert1)*(convert2);

return sum;

}

Though what it is returning is "NaN" suggesting it is "Not a number", so therefore I must be putting the rate value through the drop down list incorrectly I assume.

$options.="<option/$rate/>".$currency;

I think I am doing something wrong with the above code. I have tried assigning the rate preferably as a hidden value next to the currency symbol to no avail. If anyone could show me in the right direction to properly connecting mySQL column values through drop down lists correctly that would be greatly appreciated.

580

Answer

Solution:

Use the value attribute

$options.="<option value='$rate'>".$currency;
915

Answer

Solution:

There is something wrong with your JavaScript code:

You get the element with the id "convert1" and assign it to variable convert1. On the next line you retrieve the value of the selected option but you don't assign it to a variable. So when calculating the sum you are actually operation on DOM elements instead of numbers.

var convert1 = document.getElementById("convert1");
convert1.options[convert1.selectedIndex].value;

var convert2 = document.getElementById("convert2");
convert2.options[convert2.selectedIndex].value;

// Divides entry of type ? with DOM element and multiplies with DOM element
var sum = (entry/convert1)*(convert2);

You have to make sure you work on the right objects, add some validation for null references and convert the value to an integer before calculation sums.

495

Answer

Solution:

Fixed, my javascript just was not properly assigned.

function convert(entry)
{   
    var e1 = entry.value;

    var convert1 = document.getElementById("convert1");
    var v1 = convert1.options[convert1.selectedIndex].value;

    var convert2 = document.getElementById("convert2");
    var v2 = convert2.options[convert2.selectedIndex].value;

    var sum = ((e1/parseFloat(v1))*parseFloat(v2));

    return sum;

}

People are also looking for solutions to the problem: php - How to sum keys values inside array and print a list of values

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.