Return/ ECho acutall Form Field NAme

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
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

Return/ ECho acutall Form Field NAme

Post 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
User avatar
~J~R~R
Forum Newbie
Posts: 20
Joined: Wed Sep 18, 2002 12:19 pm
Location: Amsterdam, the Netherlanda

Post 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.
User avatar
gite_ashish
Forum Contributor
Posts: 118
Joined: Sat Aug 31, 2002 11:38 am
Location: India

Post 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
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

Returning VAlues

Post 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?
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

Echo Field Name Variables

Post 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
User avatar
gite_ashish
Forum Contributor
Posts: 118
Joined: Sat Aug 31, 2002 11:38 am
Location: India

Post 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-->";
User avatar
gite_ashish
Forum Contributor
Posts: 118
Joined: Sat Aug 31, 2002 11:38 am
Location: India

Post 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.
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

Oops (Field Name)

Post 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;
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post 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;
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

get the

Post 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:
Post Reply