kayz100 Posted September 10, 2013 Share Posted September 10, 2013 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> Quote Link to comment https://forums.phpfreaks.com/topic/282032-php-improve-my-if-statements/ Share on other sites More sharing options...
gristoi Posted September 10, 2013 Share Posted September 10, 2013 sorry, what do you mean by incorporate? if you mean set the value of the input in php then do this: <input type="text" name="number2" value="<?= $number1 ?>"/> Quote Link to comment https://forums.phpfreaks.com/topic/282032-php-improve-my-if-statements/#findComment-1448954 Share on other sites More sharing options...
cyberRobot Posted September 10, 2013 Share Posted September 10, 2013 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(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/282032-php-improve-my-if-statements/#findComment-1448958 Share on other sites More sharing options...
Solution PaulRyan Posted September 10, 2013 Solution Share Posted September 10, 2013 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> Quote Link to comment https://forums.phpfreaks.com/topic/282032-php-improve-my-if-statements/#findComment-1448985 Share on other sites More sharing options...
kayz100 Posted September 10, 2013 Author Share Posted September 10, 2013 Thanks again guys you are all awesome. I cant thank you enuogh Quote Link to comment https://forums.phpfreaks.com/topic/282032-php-improve-my-if-statements/#findComment-1448994 Share on other sites More sharing options...
cyberRobot Posted September 10, 2013 Share Posted September 10, 2013 I marked the topic as solved. If you need anything else, please mark it as unsolved or start a new thread. Quote Link to comment https://forums.phpfreaks.com/topic/282032-php-improve-my-if-statements/#findComment-1449004 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.