CageyJ0nnY Posted October 27, 2009 Share Posted October 27, 2009 Hi all, Im in the process of creating a script that allows a user to enter a number into a text box, select a conversion from a drop down list(i.e. 1 mile, 1 inch ect.) and then convert the origional number into the mile / inch ect. I cant understand why the code I have isnt working =[ please help. The main page <html> <head> <title>Imperial to Metric</title> </head> <body> <form method="post" action = "converter.php"> <input name = "number1" type="text"> <select name = "operator"> <option value = "1 Mile"> <option value = "1 Inch"> <option value = "1 Pound (lb)"> <option value = "1 Hoursepower"> <option value = "1 Pint"> </select> <input name = "Submit" type = "submit" value="="> </form> </body> </html> the page it links to: <?php $1mile = "1.609"; $1inch = "2.54"; $1pound = "0.454"; $1horsepower = "0.746"; $1pint = "0.568"; $number1 = $_POST[number1]; $operator = $_POST[operator]; if ($operator=="1 mile"){ $answer = $number1 * "$1mile"; } elseif ($operator == "1 inch"{ $answer = $number1 * "$1inch"; } elseif ($operator == "1 pound"{ $answer = $number1 * "$1pound"; } elseif ($operator == "1 horsepower"{ $answer = $number1 * "$1horsepower"; } else{ $answer = $number1 * "$1pint"; } echo " The answer is $answer"; ?> thanks John Quote Link to comment https://forums.phpfreaks.com/topic/179178-php-forms-please-help/ Share on other sites More sharing options...
cags Posted October 27, 2009 Share Posted October 27, 2009 For future reference "It's not working" is not sufficient information, 9 times out of 10 people won't even bother trying to help unless you explain what isn't working actually means. ie does it not produce the result you expect, does it throw an error etc, etc. In your case you are getting errors because variable names cannot begin with a number and you are trying to use 5 of them... $1mile = "1.609"; $1inch = "2.54"; $1pound = "0.454"; $1horsepower = "0.746"; $1pint = "0.568"; EDIT: Also the semicolons inside the if blocks don't want to be there (ie where the smilies are produced in your original post). Quote Link to comment https://forums.phpfreaks.com/topic/179178-php-forms-please-help/#findComment-945327 Share on other sites More sharing options...
PFMaBiSmAd Posted October 27, 2009 Share Posted October 27, 2009 The most apparent problem is that variable names cannot start with a number. Are you developing and testing php code on a system with error_reporting set to E_ALL and display_errors set to ON in your master php.ini so that php would help you by displaying all the errors it detects? You will save a ton of time. Quote Link to comment https://forums.phpfreaks.com/topic/179178-php-forms-please-help/#findComment-945328 Share on other sites More sharing options...
Bricktop Posted October 27, 2009 Share Posted October 27, 2009 Hi CageyJ0nnY, There a few problems with the form as follows: 1. You can't start a variable with a number (read more information on this here 2. POSTed values are case senstive you're POSTing "1 Mile" but checking for "1 mile" 3. Horsepower is spelt wrong on the option box 4. None of your option boxes are being closed with the </option> tag. I have amended the code as above and posted it below: PHP Code (converter.php) <?php $mile = "1.609"; $inch = "2.54"; $pound = "0.454"; $horsepower = "0.746"; $pint = "0.568"; $number1 = $_POST[number1]; $operator = $_POST[operator]; echo $_POST[operator]; if ($operator=="1 Mile"){ $answer = $number1 * "$mile"; } elseif ($operator == "1 Inch"){ $answer = $number1 * "$inch"; } elseif ($operator == "1 Pound (lb)"){ $answer = $number1 * "$pound"; } elseif ($operator == "1 Horsepower"){ $answer = $number1 * "$horsepower"; } else{ $answer = $number1 * "$pint"; } echo " The answer is $answer"; ?> HTML Code <html> <head> <title>Imperial to Metric</title> </head> <body> <form method="post" action="converter.php"> <input name = "number1" type="text"> <select name = "operator"> <option value = "1 Mile">Mile</option> <option value = "1 Inch">Inch</option> <option value = "1 Pound (lb)">Pound</option> <option value = "1 Horsepower">Horsepower</option> <option value = "1 Pint">Pint</option> </select> <input name = "Submit" type = "submit" value="="> </form> </body> </html> Hope this helps. Quote Link to comment https://forums.phpfreaks.com/topic/179178-php-forms-please-help/#findComment-945332 Share on other sites More sharing options...
CageyJ0nnY Posted October 27, 2009 Author Share Posted October 27, 2009 ahh, im new to php and still trying to get my head around the principles. Ive taken out the numbers in the variable names and im starting to get somewhere with it now which is all good. thanks for the help Quote Link to comment https://forums.phpfreaks.com/topic/179178-php-forms-please-help/#findComment-945334 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.