Forum Moderators: coopster
$a = "";
$your_name = "";
function hasContent($x, $y){
if(strlen($x) == 0){
global $a;
$a = $y."_error";
setError($y, "This Field has to have content.");
}
}
function setError($x, $y){
global $$a;
$$a .= $y;
}hasContent($your_name,"your_name");
echo $your_name_error;
<?
$a = "jim";function myFunction(){
global $$a;
$$a = "my string";
}echo $jim;
?>
Still doesn't work ... seems like it should ... :/ ... if someone could help please do. Problme is still that $jim have no value outside of the function ... need ot make it global, don't know how ... but is isn't always going to be $jim ... hence the variable variable problem ...
$your_name = "";
$your_name_error = "Nothing";
function hasContent($x, $y){
if(strlen($x) == 0){
$y = "This Field has to have content.";
}
}hasContent($your_name,$your_name_error);
echo = $your_name_error;
I'd want that to echo out "This Field has to have content." ... but is continues to echo out "Nothing" even though it does get into the check the sets it. What am I doing wrong here?
$your_name = "";
$your_name_error = "Nothing";
function hasContent($x, $y){
if(strlen($x) == 0){
$y = "This Field has to have content.";
}
}
hasContent($your_name,$your_name_error);
echo = $your_name_error;
You could try echo $y;
Could you not use something like -
echo $your_name? $your_name : 'This field has to have content';
[edited by: PHP_Chimp at 10:10 pm (utc) on Jan. 23, 2008]
i think you're confused about how to do what you want to do: serverside validation. And you 're doing it the complicated way.
What you want is check each submitted field, validate those that needs to be validated, and if there are errors, cancel the SAVE and display the errors.
Here is a sample on how to do that: depending on the form's method (POST or GET), the submitted values are sent in an array (in php: $_POST or $_GET). What we'll do is store in another associated array each error, with the same key as your forms variables.
To get back to your example, say user should submit an email, but forgets to write the message and hit "submit". The form is sent to the server, the server detects there is no message so it shows the form back with the error message.
var $errors= array();
$message= trim($_GET['email_message']);
$subject= trim($_GET['email_subject']);
if (strlen($message)<1){
// no message! send an error, make him bite its fingers!
$errors['email_message']='You must write a message, fool!';
}
if (strlen($subject)<1){
// no message! send an error, make him bite its fingers!
$errors['email_subject']='You must put a subject to your emai , idiot!';
}// other validations....
...
if(count($errors)<1){
mail($to,$from,$subject,$message);
die("message sent!");
}else
{
echo '<pre>';
print_r($errors);
echo '</pre>';
}// now display your html form
in your html form you could also do that:
<textarea name="email_message"></textarea>
<? if (isset($errors['email_message'])){
echo '<p style="color:red;font-size:200%">'.$errors['email_message'].'</p>';