kelharis Posted November 6, 2008 Share Posted November 6, 2008 I am trying to write a currency calculator form and I am running into a problem. Everything seems to work correctly, except for the final calculation. The user will input an amount and then select via radio button one of four currencies, which will then be run through a calculation and returned as a dollar amount. The radio buttons are returning a value for me, but I need to be able to assign a variable based upon that value, and I think I might be on the wrong track. My current code tries to do this via a switch command, but I have also tried using a series of elseif statements. Here is the current switch code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> </head> <body> <?php $amount = $_POST["amount"]; $currency = $_POST["currency"]; $calcamt = $amount*$calccurr; if ($_POST["amount"] != NULL){ switch ($currency) { case "GBP": $calccurr = 0.6397; break; case "CAD": $calccurr = 1.18659; break; case "EUR": $calccurr = 0.786225; break; case "AUD": $calccurr = 1.4653; break; } echo "$amount $currency converts to $calcamt Dollars"; } echo <<<HEREDOC <form action='test.php' method='post'> <table width=400px height=650px cellspacing=0 cellpadding=3 border=0 align='center' > <tr> <td class='sub-title' colspan=2 style='height: 60px;' valign='center' align='center'> <b>Dollar Conversion Form</b> </td> </tr> <tr> <td colspan=2 style='height: 100px;'> </td> </tr> <tr> <td class='lable' valign='top'> Amount </td> <td class='txtbx' valign='top' align='left'> <input name='amount' type='textbox' maxlength='30' style="width: 200px;"> </td> </tr> <tr> <td valign='top' align='center'> <b>Select Currency Type</b> </td> <td><fieldset> <label>British Pounds<input type="radio" name="currency" value="GBP" /> </label> <br /> <label>Canadian Dollars<input type="radio" name="currency" value="CAD" /> </label> <br /> <label>Euros<input type="radio" name="currency" value="EUR" /> </label> <br /> <label>Australian Dollars<input type="radio" name="currency" value="AUD" /> </label> <br /> </td> </tr> <tr> <td colspan=2 style='padding-left: 25px;'> <input type="submit" name="submit" value="Calculate Value"> </td> </tr> <tr> <td colspan=2 style='height: 300px;'> </td> </tr> </table> </form> HEREDOC; ?> </body> </html> Here is the elseif attempt: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> </head> <body> <?php $amount = $_POST["amount"]; $currency = $_POST["currency"]; $calcamt= $amount*$calccurr; if ($_POST["amount"] != NULL){ if ($currency = "GBP"){ $calccurr = 0.6397; } elseif ($currency = "CAD"){ $calccurr = 1.18659; } elseif ($currency = "EUR"){ $calccurr = 0.786225; } elseif ($currency = "AUD"){ $calccurr = 1.46563; } echo "$amount $currency converts to $calcamt Dollars"; } echo <<<HEREDOC <form action='test.php' method='post'> <table width=400px height=650px cellspacing=0 cellpadding=3 border=0 align='center' > <tr> <td class='sub-title' colspan=2 style='height: 60px;' valign='center' align='center'> <b>Dollar Conversion Form</b> </td> </tr> <tr> <td colspan=2 style='height: 100px;'> </td> </tr> <tr> <td class='lable' valign='top'> Amount </td> <td class='txtbx' valign='top' align='left'> <input name='amount' type='textbox' maxlength='30' style="width: 200px;"> </td> </tr> <tr> <td valign='top' align='center'> <b>Select Currency Type</b> </td> <td><fieldset> <label>British Pounds<input type="radio" name="currency" value="GBP" /> </label> <br /> <label>Canadian Dollars<input type="radio" name="currency" value="CAD" /> </label> <br /> <label>Euros<input type="radio" name="currency" value="EUR" /> </label> <br /> <label>Australian Dollars<input type="radio" name="currency" value="AUD" /> </label> <br /> </td> </tr> <tr> <td colspan=2 style='padding-left: 25px;'> <input type="submit" name="submit" value="Calculate Value"> </td> </tr> <tr> <td colspan=2 style='height: 300px;'> </td> </tr> </table> </form> HEREDOC; ?> </body> </html> Both are returning the echo string correctly, but they return a value of 0 instead of the actual calculation. Where am I going wrong? Thanks for any help! Link to comment https://forums.phpfreaks.com/topic/131597-trying-to-assign-a-variable-through-radio-button-form-input/ Share on other sites More sharing options...
xnowandtheworldx Posted November 6, 2008 Share Posted November 6, 2008 Your doing your switch statement AFTER your calculating your total values try putting your switch statement below the $currency variable but above the $calcamt variable. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> </head> <body> <?php $amount = $_POST["amount"]; $currency = $_POST["currency"]; if(isset($_POST["amount"])) { switch ($currency) { case "GBP": $calccurr = 0.6397; break; case "CAD": $calccurr = 1.18659; break; case "EUR": $calccurr = 0.786225; break; case "AUD": $calccurr = 1.4653; break; } } $calcamt = $amount*$calccurr; if ($_POST["amount"] != NULL){ switch ($currency) { case "GBP": $calccurr = 0.6397; break; case "CAD": $calccurr = 1.18659; break; case "EUR": $calccurr = 0.786225; break; case "AUD": $calccurr = 1.4653; break; } echo "$amount $currency converts to $calcamt Dollars"; } echo <<<HEREDOC <form action='test.php' method='post'> <table width=400px height=650px cellspacing=0 cellpadding=3 border=0 align='center' > <tr> <td class='sub-title' colspan=2 style='height: 60px;' valign='center' align='center'> <b>Dollar Conversion Form</b> </td> </tr> <tr> <td colspan=2 style='height: 100px;'> </td> </tr> <tr> <td class='lable' valign='top'> Amount </td> <td class='txtbx' valign='top' align='left'> <input name='amount' type='textbox' maxlength='30' style="width: 200px;"> </td> </tr> <tr> <td valign='top' align='center'> <b>Select Currency Type</b> </td> <td><fieldset> <label>British Pounds<input type="radio" name="currency" value="GBP" /> </label> <br /> <label>Canadian Dollars<input type="radio" name="currency" value="CAD" /> </label> <br /> <label>Euros<input type="radio" name="currency" value="EUR" /> </label> <br /> <label>Australian Dollars<input type="radio" name="currency" value="AUD" /> </label> <br /> </td> </tr> <tr> <td colspan=2 style='padding-left: 25px;'> <input type="submit" name="submit" value="Calculate Value"> </td> </tr> <tr> <td colspan=2 style='height: 300px;'> </td> </tr> </table> </form> HEREDOC; ?> </body> </html> the above code should work fine. If you have any problems just let us know. Link to comment https://forums.phpfreaks.com/topic/131597-trying-to-assign-a-variable-through-radio-button-form-input/#findComment-683483 Share on other sites More sharing options...
kelharis Posted November 6, 2008 Author Share Posted November 6, 2008 Ah, thank you so much! It works just right! Link to comment https://forums.phpfreaks.com/topic/131597-trying-to-assign-a-variable-through-radio-button-form-input/#findComment-683487 Share on other sites More sharing options...
xnowandtheworldx Posted November 6, 2008 Share Posted November 6, 2008 Your very welcome make sure to click the "Topic Solved" button at the bottom! Link to comment https://forums.phpfreaks.com/topic/131597-trying-to-assign-a-variable-through-radio-button-form-input/#findComment-683491 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.