Jump to content

PHP improve my IF statements


Go to solution Solved by PaulRyan,

Recommended Posts

Hi guys

i am busy playing around with php if statements. The code below works fine as it is but I am having problems trying to gt it to work inside an input field please help

 

<?php
$number1 = "01234";
$number2 = "012345";


if($number1 != $number2) {
//if they don't match output the following
echo "info do not match. Please try again.";
}else{ 
//if they match output the following
echo "Info matched Successfully!";
exit();
}
?>
 
Now how do I tweak my above php code to incorporate the code above for easy input into fields?
$number1 = "01234";
$number2 = "012345";
Please help?
<form method="post" name="change">
<p>Number 1 Info<br />
<input type="text" name="number1" />
</p>
<p>Number 2 Info<br />
<input type="text" name="number2" />
</p>
<p>
<input name="submit" type="submit" value="Check if they match" />
</p>
</form>

 

Link to comment
https://forums.phpfreaks.com/topic/282032-php-improve-my-if-statements/
Share on other sites

If you're looking to display the form when $number1 doesn't match $number2, just put the form in the if portion.

 

 

<?php
$number1 = "01234";
$number2 = "012345";
 
 
if($number1 != $number2) {
     //if they don't match output the following
     //echo "info do not match. Please try again.";
     ?>
     <form method="post" name="change">
     <p>Number 1 Info<br />
     <input type="text" name="number1" />
     </p>
     <p>Number 2 Info<br />
     <input type="text" name="number2" />
     </p>
     <p>
     <input name="submit" type="submit" value="Check if they match" />
     </p>
     </form>
     <?php
} else { 
     //if they match output the following
     echo "Info matched Successfully!";
     exit();
}
?>
  • Solution

Try this:

 

<?PHP

  //### Check if form has been posted
  if($_SERVER['REQUEST_METHOD'] == 'POST') {
 
    //### Assign incoming numbers
    $number1 = isset($_POST['number_1']) ? $_POST['number_1'] : NULL ;
    $number2 = isset($_POST['number_2']) ? $_POST['number_2'] : NULL ;
    
    //### Check to see if either field is empty
    if($number1 == NULL) {
      $message = 'Number 1 field is empty.';
    } else if($number2 == NULL) {
      $message = 'Number 2 field is empty.';
    } else {
    
      //### Check if numbers match
      if($number1 == $number2) {
        $message = 'Yes, those numbers match.';
      } else {
        $message = 'No, those do not numbers match.';
      }      
    }
  }
?>

<form method="POST">
<?PHP if(isset($message)) { echo '<p>'. $message .'</p>'; } ?>
<p>
  Number 1 Info <br>
  <input type="text" name="number_1" value="<?PHP if(isset($number1)) { echo htmlentities($number1, ENT_QUOTES); } ?>">
</p>
<p>
  Number 2 Info <br>
  <input type="text" name="number_2" value="<?PHP if(isset($number2)) { echo htmlentities($number2, ENT_QUOTES); } ?>">
</p>
<p>
  <input type="submit" value="Check if they match?">
</form>
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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