Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
If different form inputs are equal...
Old 06-12-2006, 02:14 PM If different form inputs are equal...
Junior Talker

Posts: 1
Trades: 0
Why is this script not working?
Everything seems to be right, but for some reason it isn't working. What I'm trying to do is when a person submits two different inputs into a form, the php echoes they are equal, if they are, and equals not equal if they are not equal. Here is the html and php code as follows:

com.html

<html>
<body>
Insert Value<Br>
<form action=convertera.php method=POST>
<input type=text name=number><br>
<input type=text name=numbera><br>
<input type=submit value=submit>
</form>
</body>
</html>

convertera.php

<?php

if ($_POST["number"] == $_POST["numbera"])

echo "They are equal. The first was $_POST["number"] and the second was $_POST["numbera"];

else echo "They are not equal. The first was $_POST["number"] and the second was $_POST["numbera"]";

?>


However, every time I test it seems not be working? But I've checked it all over and there seems to be nothing wrong?
Waterman is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 06-12-2006, 05:16 PM Re: If different form inputs are equal...
stoot98's Avatar
Ultra Talker

Posts: 427
Name: Stuart
Location: Glasgow, Scotland
Trades: 0
I tested it out and got a few parse errors due to the double quotes within the POST variables, personally i use single quotes and ive concatonated the strings and variables in the echo statement and it works fine.

Hope this helps.
Stoot

PHP Code:
<?php

if ($_POST["number"] == $_POST["numbera"])

echo 
"They are equal. The first was " $_POST['number']." and the second was ".$_POST['numbera'];

else echo 
"They are not equal. The first was " .$_POST['number']." and the second was ".$_POST['numbera'];

?>
stoot98 is offline
Reply With Quote
View Public Profile
 
Old 06-13-2006, 07:23 AM Re: If different form inputs are equal...
Ultra Talker

Posts: 256
Location: Auckland, New Zealand
Trades: 0
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'] == || !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'] == || !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'] == || !empty($_POST['number']) && $_POST['numbera'] == || !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..
mastercomputers is offline
Reply With Quote
View Public Profile Visit mastercomputers's homepage!
 
Reply     « Reply to If different form inputs are equal...
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.12285 seconds with 12 queries