Jump to content

[SOLVED] Function Problem


eugeniu

Recommended Posts

I have a script that processes errors from a form and makes an array out of the errors. I have a line of code that submits what kind of error it is and what the error text is to a function, which then puts it in an array. This is the line of code:

 

$form->setError($field, $badEmail);

 

$field is "email" and $badEmail is the error text, defined on another included file. Now this my function:

 

function setError($field, $errmsg) {
$this->errors[$field] = $errmsg;
$this->num_errors = count($this->errors);
}

 

Problem is, it outputs nothing. It seems to enter nothing into the array. The only way I can get it to work is by doing this:

 

$form->setError($field, "Error goes here as a non-variable.");

 

But I really don't want to have it done that way because I often have to place the same error in multiple places, which would make it a lot more helpful if I just had variables.

 

Thanks for your time.

Link to comment
Share on other sites

@jxrd: I'm sorry, I'm not following you. What would the exact code for that be?

 

@wildteen88: Here's the full code for generating errors:

 

$field = "email";  // Use field name for email
if(!$subemail || strlen($subemail = trim($subemail)) == 0){
$form->setError($field, $noEmail);
} else {
/* Check if valid email address */
$regex = "^[_+a-z0-9-]+(\.[_+a-z0-9-]+)*"
		."@[a-z0-9-]+(\.[a-z0-9-]{1,})*"
		."\.([a-z]{2,}){1}$";
if (!eregi($regex,$subemail)) {
	$form->setError($field, $badEmail);
}
$subemail = stripslashes($subemail);
}

 

The function is called on line 3 and 10. $noEmail and $badEmail are defined on a separate and included file called constants.php. This is what I have for those two:

 

$ebefore = " <p class=\"error\">* ";
$eafter = "</p>";

$noEmail = $ebefore . "No email has been entered." . $eafter;
$badEmail = $ebefore . "You have entered an invalid email." . $eafter;

 

@nrg_alpha: I'm echoing the errors with codes like this: <? echo $form->error("email"); ?>

Link to comment
Share on other sites

Is this code

$field = "email";  // Use field name for email
if(!$subemail || strlen($subemail = trim($subemail)) == 0){
$form->setError($field, $noEmail);
} else {
/* Check if valid email address */
$regex = "^[_+a-z0-9-]+(\.[_+a-z0-9-]+)*"
		."@[a-z0-9-]+(\.[a-z0-9-]{1,})*"
		."\.([a-z]{2,}){1}$";
if (!eregi($regex,$subemail)) {
	$form->setError($field, $badEmail);
}
$subemail = stripslashes($subemail);
}

Wrapped in a function? If it is then you'll have to define the variables $noEmail and $badEmail as global within your function. Otherwise your variables wont be available to your function and this is why your script outputs no errors. You should read up on variable scope

Link to comment
Share on other sites

Thanks! Looks like that's exactly what I was doing wrong. Now I have one last question. Is is possible to shorten things up by having the error variables in some sort of array and just make the array global in the function?

Link to comment
Share on other sites

@nrg_alpha: I'm echoing the errors with codes like this: <? echo $form->error("email"); ?>

 

I figured that you expected that code in your initial post to do the outputting (but now understand what you meant - I misread).

 

In your newest posted code snippet, I would scrap ereg, as this will not be included within the core of PHP as of version 6. I would instead use preg (part of Perl Compatible regular expressions). Future proof your code while you can.

 

Some online resources:

regular expressions

weblogtoolscollection

php freaks regex resources

php freaks regex tutorial

Link to comment
Share on other sites

Good idea. Why are they removing ereg?

 

ereg is part of POSIX (Portable Operating System Interface), and is generally slower than PCRE (granted, I never benchmarked this to find out personally.. but I read that it is). As well, PCRE offers additional functionality / flexibility that POSIX doesn't support. You can read the very top tip on this page that explains this.

 

I also just noticed that note regarding POSIX in that same link:

 

Note: As of PHP 5.3.0 this extension is deprecated, calling any function provided by this extension will issue an E_DEPRECATED notice.

 

If that's not a sign, I don't know what is ;)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.