Page 1 of 2

validate user input

Posted: Mon Jun 14, 2004 10:39 am
by fresh
hey,

I'm just trying to code a quick and simple script which will validate a text box, it will be looking for the correct pass, without using SQL... I want to embed the pass within the PHP script and validate it from the PHP script...

My php script: cha1.php

Code: Select all

<?php
if ($pass == "AbraCadabra") ; &#123;
echo = "Correct!" ;
else &#123;
echo = "Wrong!" ;
&#125;&#125;
?>
My Htm script: tester.htm

Code: Select all

<html>
<head>
	<title></title>
</head>

<body>

<form action="/cha1.php">
<input type="text" name="pass">
<input type="submit" value="Check Pass">
</form>

</body>
</html>
My php script is the problem obviously, I just posted it to give you an idea of what I'm trying to do here, can someone please reffer me to a page, or help tweak my code to work...either way I'd be appreciative...thanks

Posted: Mon Jun 14, 2004 11:13 am
by Buddha443556
I'm just guessing because you didn't include the error message.

Code: Select all

<?php 
if ($pass == "AbraCadabra") &#123; 
  echo "Correct!"; 
&#125; else &#123; 
  echo "Wrong!"; 
&#125; 
?>
Take a look at this one where the PHP and HTML are combined:
http://www.totallyphp.co.uk/scripts/pas ... a_page.htm

Posted: Mon Jun 14, 2004 11:15 am
by Grim...
Your php code needs some help. First, your braces are a little crazy, and you have a semi-colon after your IF statement.
IF's should be written as such:

Code: Select all

<?php
if ($a == "hello") {
    echo "hello!";
} else {
    echo "goodbye!";
}
?>
(Although I prefer the following formatting, which is technically 'incorrect')

Code: Select all

<?php
if ($a == "hello")
{
    echo "Hello!";
}
else
{
    echo "Goodbye!";
}
?>
Both bits of code do the same thing, but are laid out differently.

alright :)

Posted: Mon Jun 14, 2004 11:48 am
by fresh
thank you guys so much... I preffer the second format too, I code like that in jscript, easier to read right? anyway, thanks alot, you've been a big help...wasn't sure if I could use the if & else statement the way I was, you cleared it up, again thanks alot...

oh man ... lol

Posted: Mon Jun 14, 2004 12:00 pm
by fresh
ok it's defaulting everything to wrong even when the right pass is inputted and submitted...

Code: Select all

<?php
if ($pass == "Ave Maria") &#123;
  echo "Correct!";
&#125; else &#123;
  echo "Wrong!";
&#125;
?>
thats the code I am using... that's correct right? again the htm page is:

Code: Select all

<html>
<head>
   <title></title>
</head>

<body>

<form action="/cha1.php">
<input type="text" name="pass">
<input type="submit" value="Check Pass">
</form>

</body>
</html>
there isn't a problem with my htm script is there...somethings not working right for some reason, I dont get it, seems logical....

anyway, here's the page I'm testing it on:

http://newb.t35.com/tester.htm

don't worry about malicious code or anything like that, I'm not like that... so if you want to see for yourself what's happening, then please feel free to visit the page... oh yeah, the server adds some popup script to my pages automatically, so turn on your blockers before entering the page, I can't do anything about it, so other than that you should be cool ... thanks

Posted: Mon Jun 14, 2004 12:09 pm
by feyd
if ($_GET['pass'] == "Ave Maria") {

you don't have register globals on.. (which is correct)

Posted: Mon Jun 14, 2004 12:10 pm
by magicrobotmonkey
Whoa! I'd say it may be time to find a new server!

Posted: Mon Jun 14, 2004 12:12 pm
by feyd
magicrobotmonkey wrote:Whoa! I'd say it may be time to find a new server!
agreed, hosts that add anything to a page automatically, without our asking it isn't cool.. even if free.

i know, i know...

Posted: Mon Jun 14, 2004 12:21 pm
by fresh
i thought I outta warn you guys...it got me once and was nothing but a headache, got caught in one of their loops and it sucked... anyway, I'll try to add that piece into the script I have now and see what that does for me...again, like always your help is appreciated very much!! Good day.


edit: Woo hoo, that did it... now I can take a break, lol...thank you

Posted: Mon Jun 14, 2004 12:21 pm
by Grim...
Try adding an echo statement to the end of your php so you can see what it thinks $pass is, ie:

Code: Select all

<?php 
if ($pass == "Ave Maria") { 
  echo "Correct!"; 
} else { 
  echo "Wrong!"; 
}
echo "<br><br>Pass = $pass"; 
?>
This sort of thing is essential for debugging, just remove it before you go live!

Posted: Mon Jun 14, 2004 12:46 pm
by infolock
this should probably be your code bro :

Code: Select all

<?php 
$pass = $_POST['pass'];
if ($pass == "Ave Maria")
{ 
  echo "Correct!"; 
}
else
{ 
  echo "Wrong!"; 
} 
?>
the htm page :

Code: Select all

<html> 
<head> 
   <title></title> 
</head> 

<body> 

<form action="cha1.php" method="post"> 
<input type="password" name="pass"> 
<input type="submit" value="Check Pass"> 
</form> 

</body> 
</html>

Posted: Mon Jun 14, 2004 12:50 pm
by feyd
isn't the default method GET in a form?

cool

Posted: Mon Jun 14, 2004 12:51 pm
by fresh
hey that's a good tip, thanks! Kinda like an alert box for javascript, not really, but serves the same purpose...cool... oh and for my own clarifications if I was to want to have multiple answers, I would need to declare a global var right? So I would just go like this right:

$var = "Bill";
$Var = "Jane";

I assume that's what I wold do... then I'd just call that variable in my if statement to compare it to user input... anyway, I'll read up on it... thanks

Posted: Mon Jun 14, 2004 12:55 pm
by infolock
feyd : yup, thought he had already put method="post" in it :oops:

well

Posted: Mon Jun 14, 2004 12:57 pm
by fresh
im too new to say but I think both would do the trick in this case atleast... that's something to research later on... alright I got get back to work, lol, thanks alot guys, you made my day... later