Page 1 of 1

Return/ ECho acutall Form Field NAme

Posted: Wed Sep 18, 2002 12:17 pm
by kendall
Hi,

I have a Validation function that validates a form field and if flase calls error function

the error function needs to take the Field Name as a paremeter to echo that "this" field is and Error (determined byt the field)

how do i print an acutal field name
any suggestion on how to better design this error function for the above purpose

here is the code

function ErrorGenerate($Field,$Error)
{
$file = "register.php";
include($file);
$Replacement = "<!--".$Field." Error-->";
return $Replacement;
return $Error;
}
havent quite figured it out yet

Posted: Wed Sep 18, 2002 12:23 pm
by ~J~R~R
In every case it isn't $Error isn't returned, because the function stops when you call return(). See http://www.php.net/manual/en/function.return.php for more info.

Posted: Wed Sep 18, 2002 12:27 pm
by gite_ashish
hi,

You can have some fixed pattern which will get replaced by the actual field name.

You can try this:

Code: Select all

&lt;?php
function ErrorGenerate($Field,$Error) 
{ 
$file = "register.php"; 
include($file); 

// ***
$Error = str_replace( "(-FIELD-)", $Field, $Error );
// ***

return $Error; 
} 
?&gt;
And call it like:

Code: Select all

&lt;?php
ErrorGenerate( "Email Id", "Pls enter proper (-FIELD-) else you will not get emails !" );
?&gt;

See php man:

:idea: http://www.php.net/manual/en/function.str-replace.php

Returning VAlues

Posted: Wed Sep 18, 2002 12:32 pm
by kendall
Well i have used it and it works the problem was that i need to get the acutal form field name to cross in order to get the right error

thus if i got the form field name EMail it will also return the corresponding error


But for advice what woud u have done?

Echo Field Name Variables

Posted: Wed Sep 18, 2002 12:41 pm
by kendall
Exactly what i did...well sort of

See the include file has some if statements at intervals in a html template

i used comments like <!---Name Error-->
so if the field was name
i used a string manipulation and then have the comment replaced by the Error in the included file
its a bit untidy though but it works !!

trying to neaten it up

Posted: Wed Sep 18, 2002 12:47 pm
by gite_ashish
hi,

if u r problem is -- the field is NOT getting displayed in the browser, then


remove the html comment from the $Replacement:

Old:

Code: Select all

&lt;?php
$Replacement = "&lt;!--".$Field." Error--&gt;";
?&gt;



New:

Code: Select all

&lt;?php
$Replacement = $Field." Error"; 
?&gt;

In the code provided by me, the error message is formatted more properly. Ok if u r error messages are of fixed format you can remove the str_replace() with use of concatenation (.) operator also, like:

$Error = $Field . " $Error";

I think u missed the '$' in the code provided by u, so i provided the code.

u wrote => $Replacement = "<!--".$Field." Error-->";
u might wanted to write => $Replacement = "<!--".$Field." $Error-->";

Posted: Wed Sep 18, 2002 12:55 pm
by gite_ashish
hi,

ok... anyway we don't have the "register.php" code of yours.

i was composing my earlier post while u posted you last message.

Oops (Field Name)

Posted: Wed Sep 18, 2002 1:05 pm
by kendall
Sorry,

what i meant to do was pass the field name in the Parameter

the comments are what is going to be replaced. so i had the comments syntax like <!-- FormFieldName Error-->.
Thus if we validating the email field

then

ErrorGenerate($Field,$error)

$Replace = "<!--".$Field."-->";
there fore int he included file we where the if statement said
if ($replacement == "<!--$Field Error-->")
echo $Error;

Posted: Wed Sep 18, 2002 3:35 pm
by Takuma
Ok...

Will this work?

Code: Select all

&lt;?php

foreach($_POST as $key =&gt; $value) {
  echo "Key: $key, Value: $value";
}

function ErrorGenerate($field,$error) {
  $string = "&lt;!-- $field --&gt;";
  $fh = fopen($file,"r");
  $content = fread($fh, filesize($file));
  $content = str_replace($string, $error, $content);
  return($content);
}
?&gt;

get the

Posted: Thu Sep 19, 2002 5:49 pm
by kendall
i have the following code that checkas for a empty field

---code----
CheckFields($requiredFields);
function CheckFields($requiredFields)
{
foreach ($requiredFields as $FieldName => $Value)
{
if ($Value == "")
{
$inValidFields = $FieldName;
}
}

}
---end-------

i want to pass it back into and array
who do i do dat?
Kendall :cry: