Page 1 of 1

problem with IF()

Posted: Sat Jun 11, 2011 12:32 am
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.";
	 
	 }

?>



Re: problem with IF()

Posted: Sat Jun 11, 2011 1:15 am
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.

Re: problem with IF()

Posted: Sat Jun 11, 2011 1:11 pm
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?

Re: problem with IF()

Posted: Tue Jun 14, 2011 6:50 am
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.

Re: problem with IF()

Posted: Tue Jun 14, 2011 10:36 am
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/

Re: problem with IF()

Posted: Tue Jun 14, 2011 11:55 am
by social_experiment
Note to self: don't recommend anymore sites about anything ;)