Where have I gone wrong? Conversion Calculator Form? Please Help!

Hello there,

I have been trying to learn php for a couple of months now so decided to try to write a small program to test how I am doing.

The form is supposed to allow a user to use the select list box to choose a conversion option; mm to inch or inch to mm, then a text box is displayed bellow to allow the user to enter the size they require converting as well as a submit button which when clicked should use an external php document to process the selection and entry…

I have written this from scratch with very little experience and although Dreamweaver or notepad ++ do not display errors, I cant seem to get my form to process the information.

Here is the HTML:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<form action="file:///C|/wamp/www/Dynamic_Web_Design_with~_PHP_and_MySQL_Working Files/Chapter04/math_inch_to_mm.php" method="post">

	<p>
	Select unit of measurement to convert:<br />
	<select name="position" size="1">
		<option value="mm_select">mm to inches</option>
		<option value="inches_select">inches to mm</option>
	</select>
	</p>
    <p>
    	Please enter the size you want converting:
    </p>
    <p>
    	<input type="text" name="measurement_input" value="" />
    </p>

	<p>
	<input type="submit" value="Convert">
	</p>

</form>

</body>
</html>


Here is the PHP code:

[php]

mm to inch/inch to mm calculator <?php $mmPerInch == 25.4; $measurement_input = $_POST['measurement_input']; $millimetre = $_POST['mm_select']; $inches = $_POST['inches_select']; $measurement_input * $mmPerInch = $inches_to_mm; $measurement_input / $mmPerInch = $mm_to_inches; if (isset ($_POST ['$millimetre'])){ print "$mm_to_inches"; } else { print "$inches_to_mm"; ; } ?>

[/php]

Can anyone see where I have gone wrong and guide me in the right direction please?

Kind regards,

Luke
[font=comic sans ms][/font]

change the action to the name of the php file, IE:

<form action="NAME_OF_PHP_FILE_HERE" method="post">

Hope that helps,
Red :wink:

PS: And always save your file in dreamweaver BEFORE you include files or images - saves dreamweaver messing up your code by using a local path.

For something as simple as this, you can use Live View in Dreamweaver to test how the form will do. Once the file is saved, it’ll go back and take out the physical path and replace it with a web friendly version. Unless the php is on a separate page, you don’t need to use the action.

Your variables are backwards too, should be

$inches_to_mm =$measurement_input * $mmPerInch;
$mm_to_inches=$measurement_input / $mmPerInch;

and $mmPerInch == 25.4; should be $mmPerInch = 25.4;

also, $_POST[’$millimetre’] is wrong in this instance. You need to detect what position. $inches and $millimetre is wrong too. There is no input called in_to_mm and mm_to_inches, those are values gotten from position.

I moved the answer to a readonly text input to make it a little easier to find.

The whole page should be
[php]

<?php if(isset($_POST['submit'])) { $mmPerInch = 25.4; $measurement_input = $_POST['measurement_input']; if($_POST['position'] == 'mm_select') { $mm_to_inches= $measurement_input / $mmPerInch; $title = "mm to inches:"; } else { $inches_to_mm= $measurement_input * $mmPerInch; $title = "inches to mm:"; } } ?> Untitled Document

Select unit of measurement to convert:
mm to inches inches to mm

Please enter the size you want converting:


<?=$title?>

[/php]

Hi there, thanks for your help. I changed the document root but now get the following errors:

SCREAM: Error suppression ignored for
( ! ) Notice: Undefined variable: mmPerInch in C:\wamp\www\practice\conversion_calc\math_inch_to_mm.php on line 12
Call Stack

Time Memory Function Location

1 0.0013 251480 {main}( ) …\math_inch_to_mm.php:0

I have the same server, ill load it up and see if I get the same errors.

Thanks Richei,

That works fine…Although I get the following error message below the form:

code SCREAM: Error suppression ignored for
( ! ) Notice: Undefined variable: title in C:\wamp\www\practice\conversion_calc\math_inch_to_mm1.php on line 35
Call Stack

Time Memory Function Location

1 0.0007 249984 {main}( ) …\math_inch_to_mm1.php:0
( ! ) SCREAM: Error suppression ignored for ( ! ) Notice: Undefined variable: inches_to_mm in C:\wamp\www\practice\conversion_calc\math_inch_to_mm1.php on line 35 Call Stack #TimeMemoryFunctionLocation 10.0007249984{main}( )…\math_inch_to_mm1.php:0 " readonly />[/code]

I am obviously trying run when I should first master walking :smiley:

Also,this on line 35:

[php]

<?=$title?>

[/php]

I am not quite sure what is going on with the question marks in the code? I looked up in google and can see they must be short hand for an if else statement…is that correct? If so, how would this be written in long hand please?

Kind regards,

Luke

<?= is the same as <?php echo it's shorthand, richie just showin off his ninja skills ;)

as redcouse said, it is short hand, the php version is longer, which is

I use the shorthand when i have to do stuff like that or echo variables outside of php.

as for the undefined variable, you can assign it to an empty or null variable on the top of your code., then its technically in use, therefor, not undefined.

Hi again, thanks for your help! :slight_smile: I have no idea what you mean regarding defining the undefined variable at the top of the page…but the long hand version works fine and I find it easier to understand thanks again!

Regards,

Luke

Do whatever works for you. Took me a while to start using the shorthand code too, if you ever have to do countries in a select box, you’ll want to use the shorthand :slight_smile:

as for the other stuff I was talking about, just do something like
[php]<?php
// pre-define your variables to get rid of the undefined variable warning
$title = ‘’;
$mm_to_inches = ‘’;
$inches_to_mm = ‘’;

if(isset($_POST[‘submit’])) {
$mmPerInch = 25.4;
$measurement_input = $_POST[‘measurement_input’];

 if($_POST['position'] == 'mm_select') {
     $mm_to_inches= $measurement_input / $mmPerInch;
     $title = "mm to inches:";
} else {
    $inches_to_mm= $measurement_input * $mmPerInch;
    $title = "inches to mm:";
}

}
?>

Untitled Document

Select unit of measurement to convert:
mm to inches inches to mm

Please enter the size you want converting:


<?=$title?>

[/php]
Sponsor our Newsletter | Privacy Policy | Terms of Service