View Full Version : Simple PHP Validation Help?
tonyking
11-02-2008, 04:40 AM
Hello, I have a very simple form where a user will enter a "code". I want to check that code to make sure it matches a list of codes. If it does the code will be emailed to me, and they would be redirected to a success page. If it doesn't match one of the valid codes, they would be redirected to an error page.
Here's my code so far:
<?php
$to = "my email";
$subject = "Code";
$email = "my email";
$message = $_REQUEST['code'] ;
$headers = "From: $email";
$sent = mail($to, $subject, $message, $headers) ;
if($sent)
{header( 'Location: http://www.mysite.com/success.html' ) ; }
else
{header( 'Location: http://www.mysite.com/error.html' ) ; }
?>
As you can see no validation of the submitted code, I have no idea how to do this with php, but I know it's not hard.
All I really need is something simple where I can manually code 15 - 20 codes into the form and check for a match. Can anyone help?
Moshambi
11-02-2008, 04:46 AM
Are you going to be storing the codes in a database?
tonyking
11-02-2008, 04:48 AM
Eventually yes, but for right now I don't mind hard coding 10 into the script itself just to get me by.
Moshambi
11-02-2008, 04:57 AM
Well first you need to set up a form:
<form action="submitcode.php" method="post">
<input type="text" name="usercode" /><br />
<input type="submit" name="submit" />
</form>
then you are going to need to make a check in your PHP file submitcode.php for this example:
$usercode = $_POST['usercode'];
$realcode = "123abc";
if(strcasecmp($usercode, $realcode) == 0)
{
echo "They Match!";
}
else
{
echo "Invalid code!";
}
I would also like to note a few things.
FIRST! I hvae not tested this code I simply just created it in the reply box just now.
Second. The strcasecmp() will match a string "XXAA" to "xxAa"....it is case insensitive.
Third. If you are going to do this from a database there will be some different code.
I hope this basic example gives you an idea how to go about this, also I hope this post helped you out :)
[/code]
tonyking
11-02-2008, 05:01 AM
Thanks, one question as I don't know the syntax. How do I code it for multiple "real codes"? There will be 10 or so.
Moshambi
11-02-2008, 05:10 AM
You could easily do it using an array:
$realcode = array('code1', 'code2', 'code3', 'code4', 'code5'); // You can put as many as you need in here...
$match = "false";
// Now to loop through the array...
foreach ($codes as $value)
{
if(strcasecmp($realcode[$value], $usercode) == 0)
{
$match = "true";
}
}
if($match == "true")
{
echo "Valid Code!";
}
else
{
echo "Invalid Code!";
}
tonyking
11-02-2008, 05:13 AM
Awesome I think that's exactly what I need, then I can just replace the "Valid Code" message with an email script & redirect.
Moshambi
11-02-2008, 05:18 AM
Yup you should be able to. But if you have troubles with it you can always post here :) hope it all goes well for you
tonyking
11-02-2008, 05:21 AM
Or not.. >.< Wish I knew this stuff better. here's the code I am using:
<?php
$usercode = $_POST['code'];
$codes = array('1', '2', '3', '4', '5');
$match = "false";
foreach ($codes as $value)
{
if(strcasecmp($realcode[$value], $usercode) == 0)
{
$match = "true";
}
}
if($match == "true")
{
$to = "info@n2flash.com";
$subject = "Raffle Code";
$email = "info@n2flash.com";
$message = $_REQUEST['code'] ;
$headers = "From: $email";
$sent = mail($to, $subject, $message, $headers) ;
if($sent)
{header( 'Location: http://www.n2flash.com/success.html' ) ; }
else
{header( 'Location: http://www.n2flash.com/error.html' ) ; }
}
else
{
header( 'Location: http://www.n2flash.com/error.html' ) ;
}
?>
If i submit a 1, 2 ,3, 4 or 5 or anything else... It directs to the error page, and no email is sent.
If I leave the form blank, it goes to success and sends an email. Is there somethign wrong with what I have?
Moshambi
11-02-2008, 05:24 AM
Ok 1 thing that might be making it mess up...but I'm not positive about this is where you do your foreach loop try using a different variable for $codes as that is what you named your array...
if that doesn't work could you show your html page too please?
EDIT: I just noticed that you should just name you array to $realcodes for this line here:
if(strcasecmp($realcode[$value], $usercode) == 0)
tonyking
11-02-2008, 05:35 AM
Ok I made some changes and still have erros.
Here's the form:
<form name="raffle" action="http://n2flash.com/raffle.php" method="post">
<input name="code" maxlength="25" size="20" type="text"><br>
<input value="Submit" type="submit">
</form>
Here's the modified code:
<?php
$usercode = $_POST['code'];
$realcode = array('1', '2', '3', '4', '5');
$match = "false";
foreach ($realcode as $value)
{
if(strcasecmp($realcode[$value], $usercode) == 0)
{
$match = "true";
}
}
if($match == "true")
{
$to = "info@n2flash.com";
$subject = "Raffle Code";
$email = "info@n2flash.com";
$message = $_REQUEST['code'] ;
$headers = "From: $email";
$sent = mail($to, $subject, $message, $headers) ;
if($sent)
{header( 'Location: http://www.n2flash.com/success.html' ) ; }
else
{header( 'Location: http://www.n2flash.com/error.html' ) ; }
}
else
{
header( 'Location: http://www.n2flash.com/error.html' ) ;
}
?>
Still producing the same error. submitting a blank form directs to success page with an email generated. Any value directs to error page with no email sent.
Mind boggling!
Moshambi
11-02-2008, 05:43 AM
hmmm...I'm starting to suspect that it might be storing the numbers in your array as numbers and not as a string so try changing the if statement to this:
if ($realcode[$value] == $usercode)
if that makes it work then it is just the fact that your array has integer values and we were trying to string compare them.
tonyking
11-02-2008, 05:49 AM
Lol, this is making no sense to me but check this out...
Now with this code:
<?php
$usercode = $_POST['code'];
$realcode = array('1', '2', '3', '4', '5');
$match = "false";
foreach ($realcode as $value)
{
if ($realcode[$value] == $usercode)
{
$match = "true";
}
}
if($match == "true")
{
$to = "info@n2flash.com";
$subject = "Raffle Code";
$email = "info@n2flash.com";
$message = $_REQUEST['code'] ;
$headers = "From: $email";
$sent = mail($to, $subject, $message, $headers) ;
if($sent)
{header( 'Location: http://www.n2flash.com/success.html' ) ; }
else
{header( 'Location: http://www.n2flash.com/error.html' ) ; }
}
else
{
header( 'Location: http://www.n2flash.com/error.html' ) ;
}
?>
I submit nothing, and it's a sucess, with email.
I submit 1, it fails no email.
I submit 2, it works with an email???
Same for 3, 4 and 5. works and sends an email.
Just for giggles I submitted xx, and it failed.
I have no clue why, but I hope you do! lol
Moshambi
11-02-2008, 05:55 AM
lol wtf that is weird....I just noticed that you have this:
if($sent)
{header( 'Location: http://www.n2flash.com/success.html' ) ; }
else
{header( 'Location: http://www.n2flash.com/error.html' ) ; }
Now I am just wondering if this is the same that you are doing for the else
part of the if($match == "true) statement. I don't think it would be making a problem, but with the luck we've been having who knows at this point...
Lol, restart! Start your code over again! Thats defiantly the sign to restart!! (Lol, jk theres probably an explanation, or not. I suggest restarting on it though)
Clean your code:
<?php
$usercode = $_POST['code'];
$realcode = array('1', '2', '3', '4', '5');
$match = "false";
foreach ($realcode as $value) {
if (strcasecmp($realcode[$value], $usercode) == 0) {
$match = "true";
}
}
if ($match == "true") {
$to = "info@n2flash.com";
$subject = "Raffle Code";
$email = "info@n2flash.com";
$message = $_REQUEST['code'];
$headers = "From: $email";
$sent = mail($to, $subject, $message, $headers);
if ($sent) {
header('Location: http://www.n2flash.com/success.html');
} else {
header('Location: http://www.n2flash.com/error.html');
}
} else {
header('Location: http://www.n2flash.com/error.html');
}
?>
tonyking
11-02-2008, 06:00 AM
Well this is depressing, I changed the values to 5 digits, aa592.. bb592... etc and nothing works right.
Why do I even bother with php
>.<
Moshambi
11-02-2008, 06:06 AM
OK I GOT IT!
Sorry I should have done this earlier, but I fired up my XAMPP and started testing it.
so now here is what I got:
<?php
$usercode = $_POST['code'];
$realcode = array('1', '2', '3', '4', '5');
$match = "false";
for($i = 0; $i < count($realcode); $i++)
{
if($realcode[$i] == $usercode)
{
$match = "true";
}
}
if($match == "true")
{
echo "Matched";
}
else
{
echo "did not match";
}
?>
Now just enter in your email stuff in the same section you were before.
I tested it on a couple numbers and it printed "Match" and also it does not match if you leave it blank now...
Hope this works for you now...
tonyking
11-02-2008, 06:12 AM
It works! The input is case sensitive but at this point WHO CARES! IT WORKS! lol
Here's the final code I tested:
<?php
$usercode = $_POST['code'];
$realcode = array('1XB4', '2XB5', '3GG', '4778', '5');
$match = "false";
for($i = 0; $i < count($realcode); $i++)
{
if($realcode[$i] == $usercode)
{
$match = "true";
}
}
if($match == "true")
{
$to = "info@n2flash.com";
$subject = "Raffle Code";
$email = "info@n2flash.com";
$message = $_REQUEST['code'];
$headers = "From: $email";
$sent = mail($to, $subject, $message, $headers);
if ($sent) {
header('Location: http://www.n2flash.com/success.html');
} else {
header('Location: http://www.n2flash.com/error.html');
}
}
else
{
header('Location: http://www.n2flash.com/error.html');
}
?>
I will play with it some more tonight and see if I can understand more of how it's actually working. Thanks for all your help and prompt replies Moshambi!
Moshambi
11-02-2008, 06:17 AM
Hey no problem man sorry it was such a drag at first! lol at least we got through it. Oh and btw to help out with the case sensitive issue just do this:
$usercode = strtoupper($_POST['code']);
that will convert it to all uppercase, if you want lowercase it is strtolower();
Also if you would like me to explain to you how each of the parts is working I would have no problem helping to try and help you understand it. Just let me know
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.