Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/179178-php-forms-please-help/
Share on other sites

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).

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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.