Page 1 of 1

Problem with code....please help

Posted: Sat Jun 03, 2006 2:02 pm
by richard.cali
Hi

I am begginer in PHP, please help......I need to build simple "Distance calculation script based on two zip_codes entered by user....
It is almost done.....I need only change some part of PHP code.....please help

Zip code number 1 should go to textfield(zipOne)
Zip code number 2 should go to textfield(zipTwo)

Right now I have something like this:

php:

Code: Select all

<?php
include_once ("db_mysql.inc");
include_once ("phpZipLocator.php");

$db = new db_sql;

$zipLoc = new zipLocator;

$zipOne = 92126;
$zipTwo = 92109;

$distance = $zipLoc->distance($zipOne,$zipTwo);
?>
<form name="form1" method="post" action=""> 
  <table width="467" height="143" border="0"> 
    <tr> 
      <td width="152">Enter ZIP_CODE nr 1: </td> 
      <td width="299"><input name="zipOne" type="text" id="zipOne" size="10" maxlength="5"></td> 
    </tr> 
    <tr> 
      <td>Enter ZIP_CODE nr 2: </td> 
      <td><input name="zipTwo" type="text" id="zipTwo" size="10" maxlength="5"></td> 
    </tr> 
    <tr> 
      <td>&nbsp;</td> 
      <td><input name="submmit" type="submit" id="submmit" value="Calculate distance"></td> 
    </tr> 
	<tr>
    <td colspan="2">
	<?php
	echo 'Distance between '.$zipOne.' and '.$zipTwo.' is '.number_format($distance,2).' miles.';
	?>
	</td>
	</tr>
  </table> 
  </form>
 </body>
When I change zip_codes inside code....everything working fine....but it should work from the table....
Thank you for help good people. :D

Posted: Sat Jun 03, 2006 2:49 pm
by anthony88guy

Code: Select all

<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
  <table width="467" height="143" border="0">
<?php
if($_POST)
{
    include_once ("db_mysql.inc");
    include_once ("phpZipLocator.php");
       
    $db = new db_sql;
       
    $zipLoc = new zipLocator;
       
    $zipOne = $_POST['zipOne'];
    $zipTwo = $_POST['zipTow'];
	
	if( (is_numeric($zipOne) && (is_numeric($zipTwo) )
	{
        $distance = $zipLoc->distance($zipOne,$zipTwo);
?>
     <tr>
        <td colspan="2"><?php echo 'Distance between '.$zipOne.' and '.$zipTwo.' is '.number_format($distance,2).' miles.'; ?></td>
     </tr>
<?php
	}
	else
	{
?>
	  <tr>
        <td colspan="2">Error: Zip codes must be numerical.</td>
     </tr>
<?php
	}
}
else
{
?>
    <tr>
      <td width="152">Enter ZIP_CODE nr 1: </td>
      <td width="299"><input name="zipOne" type="text" id="zipOne" size="10" maxlength="5"></td>
    </tr>
    <tr>
      <td>Enter ZIP_CODE nr 2: </td>
      <td><input name="zipTwo" type="text" id="zipTwo" size="10" maxlength="5"></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td><input name="submit" type="submit" id="submit" value="Calculate distance"></td>
    </tr>

<?php
}
?>
 </table>
 </form>
Not tested.

Get in the habit of checking data that users submit, rule of thumb NEVER trust them. In this case the zip codes have to be numeric correct? For US based zip codes at least. So you can set up an IF with the php function is_numeric, if not throw an error. You may not see the important here, but when you start making apps where data is key and could also be harmful you want to check what your users are submitting.

Edit: was bored, added a little error checking.

Posted: Sat Jun 03, 2006 4:28 pm
by richard.cali
thank you :D