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
https://forums.phpfreaks.com/topic/96061-little-help/
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
https://forums.phpfreaks.com/topic/96061-little-help/#findComment-491796
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.