firecat318 Posted March 13, 2008 Share Posted March 13, 2008 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? Quote Link to comment Share on other sites More sharing options...
l0ve2hat3 Posted March 13, 2008 Share Posted March 13, 2008 //Convert the data $mresult = $miles * 1.6; echo $miles. "=" .$mresult. "Kilometers"; //Convert for Kilometers $kresult = $kilos * 0.6; echo $kilos. "=" .$kresult. "Miles"; Quote Link to comment Share on other sites More sharing options...
l0ve2hat3 Posted March 13, 2008 Share Posted March 13, 2008 oops if(isset($_POST['submit'])){ //Convert the data $mresult = $miles * 1.6; echo $miles. " = " .$mresult. " Kilometers"; //Convert for Kilometers $kresult = $kilos * 0.6; echo $kilos. " = " .$kresult. " Miles"; } Quote Link to comment Share on other sites More sharing options...
derrick1123 Posted March 13, 2008 Share Posted March 13, 2008 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... Quote Link to comment 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.