phpretard Posted September 10, 2009 Share Posted September 10, 2009 I an trying to validate a form . if (empty($_POST['question'])){ // here is what I usually use $alert = "<script>alert('This Work Fine')</script>"; } I would like the above in a function so I tried this: ------------------------------------------------------------ <?php function validate($var){ $alert = "<script>alert('$var')</script>"; } if (empty($_POST['question'])){ validate('The question is Empty'); } echo $alert; --> The function script is on a different page and included() when the form is submitted. Is their something obvious that I am missing? It doesn't work as a function Quote Link to comment Share on other sites More sharing options...
Garethp Posted September 10, 2009 Share Posted September 10, 2009 http://au.php.net/manual/en/language.variables.scope.php http://us2.php.net/manual/en/function.return.php Quote Link to comment Share on other sites More sharing options...
phpretard Posted September 10, 2009 Author Share Posted September 10, 2009 function validate($var){ $alert = "<script>alert('$var')</script>"; return $alert; } // this doesn't work either I always research before posting. Quote Link to comment Share on other sites More sharing options...
Mark Baker Posted September 10, 2009 Share Posted September 10, 2009 function validate($var){ $alert = "<script>alert('$var')</script>"; return $alert; } if (empty($_POST['question'])){ $result = validate('The question is Empty'); } echo $result; Quote Link to comment Share on other sites More sharing options...
Garethp Posted September 10, 2009 Share Posted September 10, 2009 Now that you've returned it, you need to pass it into your script. Like so $alert = validate('The question is Empty'); Because the value of validate('The question is Empty'); is what you return. However, it doesn't actually edit the variables outside your function, unless you specifically let it. So try this <?php function validate($var) { $alert = "<script>alert('$var')</script>"; return $alert; } if (empty($_POST['question'])) { $alert = validate('The question is Empty'); } echo $alert; ?> Quote Link to comment Share on other sites More sharing options...
phpretard Posted September 10, 2009 Author Share Posted September 10, 2009 Thank for the explination,,, It works great. Do you mind if I complicate it a little? <?php // FUNTION PAGE.PHP function validate_empty($post, $pop) { if (empty($_POST[''])) { $alert = "<script>alert('$var')</script>"; $pass = "NO"; } return $alert; return $pass; } ?> // HTML PAGE.PHP <? include ("the page with the functions"); if (isset($_POST['submit'])) { $result = validate_empty("question", "Empty") } ?> // HTML HERE <? echo $result; ?> What do you think about this? You have no idea how much time this script would save me! Quote Link to comment Share on other sites More sharing options...
Mark Baker Posted September 10, 2009 Share Posted September 10, 2009 function validate_empty($post, $pop) { if (empty($_POST[''])) { $alert = "<script>alert('$var')</script>"; $pass = "NO"; } return $alert; return $pass; } will return the value of $alert. The act of returning will terminate execution of the function, so the next line (return $pass) will not be executed. If you needed to return both values: return array('alert'=>$alert,'pass'=>$pass); Quote Link to comment Share on other sites More sharing options...
phpretard Posted September 10, 2009 Author Share Posted September 10, 2009 I do need to pass both... should the array go into the funtion? function validate_empty($post, $pop) { if (empty($_POST[''])) { $alert = "<script>alert('$var')</script>"; $pass = "NO"; } return array('alert'=>$alert,'pass'=>$pass); } Quote Link to comment Share on other sites More sharing options...
Mark Baker Posted September 10, 2009 Share Posted September 10, 2009 I do need to pass both... should the array go into the funtion? Yes, but note that the function is now returning an array rather than a simple scalar value (string, integer, float, blloean). function validate_empty($post, $pop) { if (empty($_POST['post'])) { $alert = "<script>alert('$post')</script>"; $pass = "NO"; } return array('alert'=>$alert,'pass'=>$pass); } if (isset($_POST['submit'])) { $result = validate_empty("question", "Empty") echo '<pre>'; print_r($result) echo '</pre>'; } Quote Link to comment Share on other sites More sharing options...
Garethp Posted September 10, 2009 Share Posted September 10, 2009 Just as another explanation, the reason it won't work if you use return twice is because the functions stops when it reaches the first return. So anything after that is ignored. This is useful for error checking, like if(error){return 0;} then nothing else will happen if there is an error. Just thought it might help you in the future Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.