problem with IF()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
xwhitchx
Forum Commoner
Posts: 38
Joined: Mon Jun 21, 2010 4:30 pm

problem with IF()

Post by xwhitchx »

Im trying to make a very simple copy the code and if they match you can go on, but it doesnt seem to be working right at all. I can type in the same numbers as the code but the are not equel I dont get it . well here is my code. thank for your time.

Code: Select all

<?php session_start(); ?>
<?php

$getcode =  rand() . "\n";

$_SESSION['getcode'] = $getcode;

$gettingcode = $_SESSION['getcode'];


  echo "
  
  <form method='post' action='codetestpage.php'>
  Enter this code: ".$getcode." <input name='inputcode' type='text' border='1' />
  <input value='Send' type='submit' />
  
  </form>"
  ;

?>

Code: Select all

<?php session_start(); ?>



<?php

$input = $_POST["inputcode"];
$getcode = $_SESSION["getcode"];



echo 'Your input was: '.$input.'<br/>';

echo 'The code was: '.$getcode;


?>


<hr/>


<?php

if ($input == $getcode)
{ 

echo "Code matched.";

}
 else 
 
 { 
	 echo "Code didn't match.";
	 
	 }

?>


cwheel3915
Forum Commoner
Posts: 28
Joined: Wed Apr 28, 2010 8:02 pm

Re: problem with IF()

Post by cwheel3915 »

Okay first im going to give you some working code then im going to point a few things out. Someone else may also want to talk to you about your coding practices I imagine others could explain things better, but I have the code working, and it wasnt the if statement that was the problem.

here is the first page re-done

Code: Select all

<?php 
session_start(); 

$getcode =  rand();

$_SESSION['getcode'] = $getcode;

$gettingcode = $_SESSION['getcode'];
?>
<html>
<head></head>
<body>
  <form action='codetestpage.php' method='post'>
  Enter this code: <?php echo $getcode; ?> 
  <input name='inputcode' type='text' border='1' >
  <input value='Send' type='submit' >
  </form>
  </body>
  </html>

And here is the codetestpage.php redone it works I tested it.

Code: Select all

<?php 
session_start(); 

$input = $_POST["inputcode"];
$getcode = $_SESSION["getcode"];

echo 'Your input was: '.$input.'<br/>';
echo 'The code was: '.$getcode . '<br/>';


if ($input == $getcode)
{ 

echo "Code matched.";

}
 else 
 
 { 
         echo "Code didn't match.";
         
         }

?>
The problem was your code in general lets look at the original.

Code: Select all

<?php session_start(); ?>
<?php

$getcode =  rand() . "\n";

$_SESSION['getcode'] = $getcode;

$gettingcode = $_SESSION['getcode'];


  echo "
  
  <form method='post' action='codetestpage.php'>
  Enter this code: ".$getcode." <input name='inputcode' type='text' border='1' />
  <input value='Send' type='submit' />
  
  </form>"
  ;

?>


first, there was no reason to use the ?> and then <?php after the start session. Also you should try to seperate your php, and html where possible, there was no need to echo all that html. Just echoing the variable in the one spot works fine. Im sure I have missed alot.

Code: Select all

<?php session_start(); ?>



<?php

$input = $_POST["inputcode"];
$getcode = $_SESSION["getcode"];



echo 'Your input was: '.$input.'<br/>';

echo 'The code was: '.$getcode;


?>


<hr/>


<?php

if ($input == $getcode)
{ 

echo "Code matched.";

}
 else 
 
 { 
         echo "Code didn't match.";
         
         }

?>

Again here there was no reason to keep opening, and closing with <?php tags. That was the only error I was getting.

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\xampp\htdocs\test\codetestpage.php:3) in C:\xampp\htdocs\test\codetestpage.php on line 3

However the code still did everything you wanted it to do with that error, on my enviroment anyway.
xwhitchx
Forum Commoner
Posts: 38
Joined: Mon Jun 21, 2010 4:30 pm

Re: problem with IF()

Post by xwhitchx »

Thank you, It works perfectly now. As for my coding, I have learned it from little youtube videos and tut online =P so that my be why it looks the wait it does. Do you happen to know any good places to learn the right way of coding php?
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: problem with IF()

Post by social_experiment »

xwhitchx wrote:Do you happen to know any good places to learn the right way of coding php?
This forum is a good place to learn. You can also have a look at w3schools in the php section.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: problem with IF()

Post by AbraCadaver »

social_experiment wrote:
xwhitchx wrote:Do you happen to know any good places to learn the right way of coding php?
This forum is a good place to learn. You can also have a look at w3schools in the php section.
FYI... http://w3fools.com/
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: problem with IF()

Post by social_experiment »

Note to self: don't recommend anymore sites about anything ;)
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply