Posts: 256
Location: Auckland, New Zealand
|
Here's code to do what you want. I've included a few checks that help avoid running into warnings and errors as well as just some ideas you could use for data checking and handling. Please do research what you don't know about this code.
Since it's only a small thing, I decided to combine it all into just the one fiile, where output of what you want will take place in the same page.
Here's the code:
PHP Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <meta http-equiv="Content-Type" content="application/xhtml+xml; charset=UTF-8" /> <title>Is Number Same As Numbera?</title> </head> <body> <form id="form1" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <div> <label for="number">Number:</label> <input id="number" name="number" type="text" <?php if(isset($_POST['number']) && $_POST['number'] == 0 || !empty($_POST['number'])) echo 'value="'.$_POST['number'].'" ' ?>/><br /> <label for="numbera">Numbera:</label> <input id="numbera" name="numbera" type="text" <?php if(isset($_POST['numbera']) && $_POST['numbera'] == 0 || !empty($_POST['numbera'])) echo 'value="'.$_POST['numbera'].'" ' ?>/><br /> <input id="submit" name="submit" type="submit" value="Submit" /> </div> </form> <?php if(isset($_POST['submit']) && strcmp($_POST['submit'],'Submit') === 0) { if(isset($_POST['number'],$_POST['numbera']) && $_POST['number'] == 0 || !empty($_POST['number']) && $_POST['numbera'] == 0 || !empty($_POST['numbera'])) { if(is_numeric($_POST['number']) && is_numeric($_POST['numbera'])) { if($_POST['number'] == $_POST['numbera']) { echo '<p>Number (' . $_POST['number'] . ') and Numbera (' . $_POST['numbera'] . ') are equivalent.</p>'; } else { echo '<p>Number (' . $_POST['number'] . ') and Numbera (' . $_POST['numbera'] . ') are not equivalent.</p>'; } } else { echo '<p>The input required in both fields must be numerical.</p>'; } } else { echo '<p>You cannot leave a field empty. Please correct it.</p>'; } } ?>
</body> </html>
I would usually create objects to handle form processing but in this I'm just showing you how I would do simiple data checking, since it's vital to check all input users are capable of doing. Save this in your local webroot as filename.php and run it and test it, everything should work from the same page and hopefully give you some more ideas as to where you can improve yourself coding wise.
If you want any explanations to go with this code, then do ask as PHP is one of my stronger areas.
Just to clear things up, this compares numbers in many notations, meaning 100 is equivalent to 1e2, which is calculated like 1 * 10 ^ 2, so both resulting answers are 100. Also things like 002 is the same as 2, etc.
Cheers,
MC
__________________
#------------------------------ signature---------------------------------------------------------------------------------#
Quote:
|
I am well recognised for what I don't do than what I do. Chores are just one of those things.
|
Last edited by mastercomputers; 06-13-2006 at 07:37 AM..
|