Jump to content

Very Simple Function Poblem


phpretard

Recommended Posts

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

Link to comment
Share on other sites

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;
?>

Link to comment
Share on other sites

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!

 

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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>';
}

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.