Jump to content

Little help


firecat318

Recommended Posts

Alright, So I just made simple miles to kilo's script... not for functionality but just to explore my PHP knowledge...

 

Anyways, here is the code:

 

<html><head><title>Conversion</title></head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" name="miles1" method="POST">
<h4>Convert miles into kilometers</h4> <br />
Miles into Kilometers <input type="text" name="miles" size="4" /> <br />
Kilometers into Miles <input type="text" name="kilo" size="4" /> <br />
<input type="submit" name="submit" value="Convert!" />
</form>

<?php

	//Define variables
$miles = $_POST['miles'];
$kilos = $_POST['kilos'];
$miles1 = $_POST['miles1'];

if (isset($miles1))
  	{
echo "";	
}	
//Convert the data
$mresult = $miles * 1.6;	
echo "$miles = $mresult Kilometers";

//Convert for Kilometers
$kresult = $kilos * 0.6;
echo "$kilos = $kresult Miles";

?>

 

My problem is that when I load the page, below the form PHP is automatically displaying = 0 Kilometers = 0 Miles

but I want it to be gone unless the user has submitted their data already.

 

Thats why I put in an isset() function but I don't think I did it right. 

Can somebody explain how to fix it?

Link to comment
Share on other sites

Try:

 

<?php
//next step var
$step = $_POST['step'];
//others
$miles = $_POST['miles'];
$kilos = $_POST['kilos'];
$miles1 = $_POST['miles1'];

//show the default form
if($step==0 || $step==""){

  echo "<html><head><title>Conversion</title></head><body>";
  echo "<form method='POST' action='$_SERVER['PHP_SELF']' name='miles1'>";
  echo "<h4>Convert miles into kilometers</h4> <br />";
  echo "Miles into Kilometers <input type='text' name='miles' size='4' /> <br />";
  echo "Kilometers into Miles <input type='text' name='kilo' size='4' /> <br />";
  echo "<input type='hidden' value='1' name='step'>";
  echo "<input type='submit' name='submit' value='Convert!' />";
  echo "</form>";
  
}  

//show the results
if($step==1){

if (isset($miles1))
  	{
echo "";	
}	
//Convert the data
$mresult = $miles * 1.6;	
echo "$miles = $mresult Kilometers";

//Convert for Kilometers
$kresult = $kilos * 0.6;
echo "$kilos = $kresult Miles";

}	
?>

 

I didn't get a chance to test it out sorry...

Link to comment
Share on other sites

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.