Use of undefined constant error ???

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cbguest
Forum Newbie
Posts: 7
Joined: Fri Jun 21, 2002 9:48 am

Use of undefined constant error ???

Post by cbguest »

I am trying to figure out the problem with some code.

I am receiving this error:

Notice: Use of undefined constant emailerror - assumed 'emailerror' in /usr/local/apache/htdocs/change/12validateform.php on line 31

Line 31 is:

$texts[emailerror]="You did not give a valid email.";

Can anyone explain why I may be getting that error? I am using PHP 4.2.1 if that makes a difference.

Thanks,
Chris
will
Forum Contributor
Posts: 120
Joined: Fri Jun 21, 2002 9:38 am
Location: Memphis, TN

Post by will »

does the $texts array actually have a key named "emailerror"? if not, i believe you would need to do something like this...

Code: Select all

array_merge($texts, array("emailerror" => "You did not give a valid email."));

<edit>
good point e+...not sure what i was thinking :)
</edit>
Last edited by will on Fri Jun 21, 2002 1:01 pm, edited 1 time in total.
User avatar
e+
Forum Commoner
Posts: 44
Joined: Mon Jun 17, 2002 7:07 am
Location: Essex, UK

Post by e+ »

Silly Question but what's on line 30 as php errors tend to tell you the error is on the line after the actual error (or is that just my code :? ).
cbguest
Forum Newbie
Posts: 7
Joined: Fri Jun 21, 2002 9:48 am

Post by cbguest »

I don't think there is an array statement. I am getting errors on all of the $texts statements. I am not that familar with php.

This is the code starting at line 18...the first lines are just comments.


<?php
$urlofthisfile = "http://10.9.0.6/change/12validateform.php"; // absolute url to this script.
$fromforconfirmmail = "admin@grady.k12.ga.us"; // emailaddres the visitor sees as from.
$emailfordatamail = "admin@grady.k12.ga.us"; // emailaddress to send the data to

// Some redirects to inform the visitor of your site.
$noticepage = "notice.html"; //Page to tell the author of the form that a confirmation mail has been send to his/her address
$confirmdeletepage = "cancellation.html"; // Page to redirect the authorafter he/she canceled.
$confirmsendpage = "confirmation.html"; // Page to redirect the authorafter he/she confirmed.

$maxnrofdaystopkeepdata = 2; //number of days the confirmation is valid;

// texts used in the script. Try the script a few times to figure out where these texts belong and what they say exactly.
$texts[emailerror]="You did not give a valid email."; //do not use a ', or comment it out like this: ''
$texts[blankfields]="You did not fill in"; //do not use a ', or comment it out like this: ''
$texts[errors]="The following errors occurred:"; //do not use a ', or comment it out like this: ''
$texts[novalidentry]="You didn''t use a valid confirmation link or your form data has allready expired"; //do not use a ', or comment it out like this: ''

$texts[confirmmailsubject]="Please confirm!"; // subject of confirmation mail send to visitor
$texts[datamailsubject]="Data from contact form"; //subject of data mail send to you

////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////// You do not need to change anything below this line ////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////

$maxtime = mktime() - ($maxnrofdaystopkeepdata * 86400);
$handle=opendir("data");
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
$filetime = filemtime("data/".$file);
if ($filetime < $maxtime){
unlink("data/".$file);
}
}
}
closedir($handle);

if (isset($C)){
$file = "data/".$S.".txt";
if (file_exists($file)){
if ($C == "y"){
$fp = fopen($file,"r");
$contents = fread ($fp, filesize($file));
fclose($fp);
$contents = explode("%%%%",$contents);
$email = $contents[0];
$vars = explode("#$%&%$#",$contents[1]);
$vals = explode("#$%&%$#",$contents[2]);

$fp = fopen("messages/data.txt","r");
$message = fread ($fp, filesize("messages/data.txt"));
fclose($fp);

for ($k=0;$k<count($vars);$k++){
$var = $vars[$k];
$val = $vals[$k];
if ($var!="compulsory" && (strtolower($var)!="submit" && strtolower($var)!="reset")){
$formdata .= $var.": ".$val."\n";
$message = str_replace("%$var%",$val,$message);
}
}
$message = str_replace("%formdata%",$formdata,$message);
$from = "From: " . $email;
mail($emailfordatamail, "$texts[datamailsubject]", $message, $from);
unlink($file);
header("Location: $confirmsendpage");
exit;
} elseif ($C == "n"){
unlink($file);
header("Location: $confirmdeletepage");
exit;
}
}
echo "<html>\n<head>\n<script>\n<!--\nalert('".$texts[novalidentry]."');\n";
echo "history.go(-1);\n//-->\n</script>\n</head>\n</html>";
exit;
} elseif (isset($HTTP_POST_VARS)){
$varkeys = array_keys($HTTP_POST_VARS);
$email= $email
if (!eregi ("^[a-zA-Z0-9_.-]+@grady.k12.ga.us", $email)) {
$error = $texts[emailerror];
}
if (isset($compulsory)){
for($i=0;$i<count($varkeys);$i++){
$var = $varkeys[$i];
$val = $HTTP_POST_VARS[$var];
if (in_array($var, $compulsory)&&($val==""||!isset($val))){
$errors[]=$var;
}
}
}
if (isset($error)||isset($errors)){
echo "<html>\n<head>\n<script>\n<!--\nalert('";
echo $texts[errors]."\\n";
if (isset($error)){
echo "- ".$error."\\n";
}
if (isset($errors)){
echo "- ".$texts[blankfields]." ";
for ($j=0;$j<count($errors);$j++){
echo $errors[$j]." ";
}
}
echo "');\nhistory.go(-1);\n//-->\n</script>\n</head>\n</html>";
exit;
}

$S = md5(uniqid(rand()));
$confirmlink = $urlofthisfile."?C=y&S=".$S;
$cancellink = $urlofthisfile."?C=n&S=".$S;

$fp = fopen("messages/confirm.txt","r");
$message = fread ($fp, filesize("messages/confirm.txt"));
fclose($fp);

for ($k=0;$k<count($varkeys);$k++){
$var = $varkeys[$k];
$val = stripslashes($HTTP_POST_VARS[$var]);
if ($var!="compulsory" && (strtolower($var)!="submit" && strtolower($var)!="reset")){
$formdata .= $var.": ".$val."\n";
$message = str_replace("%$var%",$val,$message);
}
}

$message = str_replace("%confirmlink%",$confirmlink,$message);
$message = str_replace("%cancellink%",$cancellink,$message);
$message = str_replace("%formdata%",$formdata,$message);

$vars = implode("#$%&%$#",$varkeys);
$vals = implode("#$%&%$#",$HTTP_POST_VARS);
$data = $email."%%%%".$vars."%%%%".$vals;

$file = "data/".$S.".txt";
$fp = fopen($file,"a");
fputs($fp,$data);
fclose($fp);

$from = "From: " . $fromforconfirmmail;
mail($email, "$texts[confirmmailsubject]", $message, $from);
header("Location: $noticepage");
exit;
} else {
exit;
}
?>
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

You have to put quotes around strings in associative arrays so this:

Code: Select all

$texts&#1111;'emailerror'] = 'You did not give a valid email.';
will stop the errors. Because you don't have quotes around it PHP checks first to see if it's a constant that you've set, when it finds that it's not it just assumes that you meant to put quotes in. This means lots of work for the PHP engine and a bunch of errors for you.

When you're referencing a part of an array by number, however, this,

Code: Select all

$texts&#1111;0] = 'blah';
is fine.

Mac
cbguest
Forum Newbie
Posts: 7
Joined: Fri Jun 21, 2002 9:48 am

Post by cbguest »

Okay that got rid of those first few errors...Excellent!!! Thanks!!!

Now I've got 3 more errors:

Notice: Undefined variable: email in /usr/local/apache/htdocs/change/12validateform.php on line 96

Notice: Use of undefined constant emailerror - assumed 'emailerror' in /usr/local/apache/htdocs/change/12validateform.php on line 97

Notice: Use of undefined constant errors - assumed 'errors' in /usr/local/apache/htdocs/change/12validateform.php on line 110


Line 90 thru 113 is:

}
echo "<html>\n<head>\n<script>\n<!--\nalert('".$texts[novalidentry]."');\n";
echo "history.go(-1);\n//-->\n</script>\n</head>\n</html>";
exit;
} elseif (isset($HTTP_POST_VARS)){
$varkeys = array_keys($HTTP_POST_VARS);
if (!eregi ("^[a-zA-Z0-9_.-]+@grady.k12.ga.us", $email)) {
$error = $texts[emailerror];
}
if (isset($compulsory)){
for($i=0;$i<count($varkeys);$i++){
$var = $varkeys[$i];
$val = $HTTP_POST_VARS[$var];
if (in_array($var, $compulsory)&&($val==""||!isset($val))){
$errors[]=$var;
}
}
}
if (isset($error)||isset($errors)){
echo "<html>\n<head>\n<script>\n<!--\nalert('";
echo $texts[errors]."\\n";
if (isset($error)){
echo "- ".$error."\\n";
}
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Line 97 and 110 are the same error as before try:

Code: Select all

$error = $texts&#1111;'emailerror'];
and

Code: Select all

echo $texts&#1111;'errors']."\n";
accordingly.

On line 96 you are trying to use a variable that hasn't been initialised, try

Code: Select all

if (isset($email) && !eregi ("^&#1111;a-zA-Z0-9_.-]+@grady.k12.ga.us", $email)) &#123;
Mac
cbguest
Forum Newbie
Posts: 7
Joined: Fri Jun 21, 2002 9:48 am

Post by cbguest »

This is crazy...everytime I fix one problem I find another one.

Now I am getting one error:
Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /usr/local/apache/htdocs/change/12validateform.php on line 81


This is lines69 thru 90:

fclose($fp);

for ($k=0;$k<count($vars);$k++){
$var = $vars[$k];
$val = $vals[$k];
if ($var!="compulsory" && (strtolower($var)!="submit" && strtolower($var)!="reset")){
$formdata .= $var.": ".$val."\n";
$message = str_replace("%$var%",$val,$message);
}
}
$message = str_replace("%formdata%",$formdata,$message);
$from = "From: " . $email;
mail($emailfordatamail, "$texts['datamailsubject']", $message, $from);
unlink($file);
header("Location: $confirmsendpage");
exit;
} elseif ($C == "n"){
unlink($file);
header("Location: $confirmdeletepage");
exit;
}
}
cbguest
Forum Newbie
Posts: 7
Joined: Fri Jun 21, 2002 9:48 am

Post by cbguest »

I took the quotes out and that appeared to fix that error message. We are at the end of the script now. I hope there are not anymore errors after these.

Now I am receiving these errors:

Notice: Undefined variable: formdata in /usr/local/apache/htdocs/change/12validateform.php on line 136

Notice: Array to string conversion in /usr/local/apache/htdocs/change/12validateform.php on line 134

Notice: Array to string conversion in /usr/local/apache/htdocs/change/12validateform.php on line 146

Notice: Undefined variable: email in /usr/local/apache/htdocs/change/12validateform.php on line 147

Notice: Undefined variable: email in /usr/local/apache/htdocs/change/12validateform.php on line 155


This is lines 122 thru 161:

}

$S = md5(uniqid(rand()));
$confirmlink = $urlofthisfile."?C=y&S=".$S;
$cancellink = $urlofthisfile."?C=n&S=".$S;

$fp = fopen("messages/confirm.txt","r");
$message = fread ($fp, filesize("messages/confirm.txt"));
fclose($fp);

for ($k=0;$k<count($varkeys);$k++){
$var = $varkeys[$k];
$val = stripslashes($HTTP_POST_VARS[$var]);
if ($var!="compulsory" && (strtolower($var)!="submit" && strtolower($var)!="reset")){
$formdata .= $var.": ".$val."\n";
$message = str_replace("%$var%",$val,$message);
}
}

$message = str_replace("%confirmlink%",$confirmlink,$message);
$message = str_replace("%cancellink%",$cancellink,$message);
$message = str_replace("%formdata%",$formdata,$message);

$vars = implode("#$%&%$#",$varkeys);
$vals = implode("#$%&%$#",$HTTP_POST_VARS);
$data = $email."%%%%".$vars."%%%%".$vals;

$file = "data/".$S.".txt";
$fp = fopen($file,"a");
fputs($fp,$data);
fclose($fp);

$from = "From: " . $fromforconfirmmail;
mail($email, "$texts[confirmmailsubject]", $message, $from);
header("Location: $noticepage");
exit;
} else {
exit;
}
?>
Post Reply