PDA

View Full Version : This is sad. I need help :P



mburt
08-07-2008, 06:05 PM
<?php
session_start();
$answer = $_SESSION["answer"];
$user = $_POST["user"];
$pass = $_POST["pass"];
$cpass = $_POST["cpass"];
$email = $_POST["email"];
$cemail = $_POST["cemail"];
$valid = $_POST["valid"];
$error = "";
$count = 0;
function add($txt) {
global $count, $error;
$count++;
$error .= "\n<br>".$count.". ".$txt;
};
if ($cpass != $pass) {
add("Your second password did not match the original.");
};
if ($cemail != $email) {
add("Your second e-mail address did not match the original.");
};
if ($valid != $answer) {
add("Your Roman Numeral answer did not match! Please click the link regarding Roman Numerals on the previous page.");
};
if (!eregi("^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$]", $email)) {
add("You did not enter a valid e-mail.");
};
echo $error;
?>

For some reason the add() function won't add the string to the $error variable. I've been poking around with this for a while now, and I just can't get it. It's probably just some stupid mistake I'm overlooking.

motormichael12
08-07-2008, 06:15 PM
Try this:

<?php
session_start();
$answer = $_SESSION["answer"];
$user = $_POST["user"];
$pass = $_POST["pass"];
$cpass = $_POST["cpass"];
$email = $_POST["email"];
$cemail = $_POST["cemail"];
$valid = $_POST["valid"];


if ($cpass != $pass) {
$error[] = "Your second password did not match the original.";
};
if ($cemail != $email) {
$error[] = "Your second e-mail address did not match the original.";
};
if ($valid != $answer) {
$error[] = "Your Roman Numeral answer did not match! Please click the link regarding Roman Numerals on the previous page.";
};
if (!eregi("^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$]", $email)) {
$error[] = "You did not enter a valid e-mail.";
};


if(isset($errors))
{
for($i = 0; $i < count($errors); $i++)
{
echo "$i. " . $errors[$i] . "<br>";
}
}
?>

mburt
08-07-2008, 06:18 PM
Yeah I know that will work, it's just I wanted to use a function. Oh well, I can live with it.

Strangeplant
08-07-2008, 06:58 PM
Could it be the ';' after the closing '}' used in various places?

motormichael12
08-07-2008, 06:59 PM
I testede this with some conditions that I knew would work or not and it worked fine:

<?php
function add($txt) {
global $count, $error;
$count++;
$error .= "\n<br>".$count.". ".$txt;
};
if (1==1) {
add("Your second password did not match the original.");
};
if (2 != 1) {
add("Your second e-mail address did not match the original.");
};
if ("blah" == "blah") {
add("Your Roman Numeral answer did not match! Please click the link regarding Roman Numerals on the previous page.");
};
if (1 != 1) {
add("You did not enter a valid e-mail.");
};
echo $error;
?>

I see no reason why the original wasn't working.

boogyman
08-07-2008, 07:45 PM
while PHP doesn't have an add() function to any other developer that might appear that you attempting to do a mathematical equation.

There are a couple of other coding practices that could be implemented differently.
It's generally good habit to stay away from global variables as much as possible, because as stated, they are global, meaning available whereever and whenever... and can cause about as big of a headache as a missing semi colon LOL.

It is also good practice to separate the code from the output.

Also, there is no way of scaling this function into other application, which is one of the main reasons functions are created.

Below is a function I created a while ago that will address the issues above.



function &addToArray($input, $array, $clear=false)
{
if($clear)
{
unset($array);
$array = array();
}

if(is_array($input))
{
foreach($input as $val)
{
$array[] = $val;
}
}
else
{
$array[] = $input;
}
return $array;
}

mburt
08-07-2008, 10:23 PM
Thanks boogyman, I appreciate the advice. However, I have the problem solved now.

Twey
08-08-2008, 03:16 AM
I made a post recently detailing three fairly critical points of good coding style. It's now in my sig under 'how to code', but I'll link to it here too: How to Code (http://dynamicdrive.com/forums/showpost.php?p=155509&postcount=1337). These guidelines apply to any language, to the extent permissible by that language.