Jump to content

Help with this function


music_lp90
 Share

Recommended Posts

Hi, I'm writing my first user defined function and could use some help. 1st I want this function to redirect the user to another page if a variable is not set. I have this part working successfully, but second I want to return to them the variable that was not set.So for instance if the user submitted a form and the month was not set, it would send them back to the form and say *month must be setHere's what I've done for the function:

function not_set_then_go($var, $url) {	if (!isset($var)) {		header("location: $url"); 	} }

Thanks for your help!

Link to comment
Share on other sites

Perhaps when you use the header() function you could add a query string to the url then use $_GET[''] to show the message? I don't know I'm new to this..
+1 to this idea.You could add the variable to the URL, and then on the other page, test for it, and add a message if it's present like
function not_set_then_go($var, $url) {	if (!isset($var)) {		header("location: $url . '&' . $var"); 	}}

And on the other page

<?phpif (isset($_GET['varname']) {echo 'You never entered varname!';}?>

Link to comment
Share on other sites

Thanks for your help. I couldn't get it to work exactly as you had it, but I got it to work like this:The function:

function not_set_then_go($var, $url, $varname) {	if (!isset($var)) {		$go = $url . "?error=" . $varname;		header("location: $go"); 	} }

Using the function:

<?php  	not_set_then_go($_POST['month'], "calendar_form.php", month); ?>

Checking for error:

<?phpif (isset($_GET['error'])) { 	$error = $_GET['error'];	}?>

Thanks again!

Link to comment
Share on other sites

I want to mention something about isset. isset checks if a variable has been set, right? So it would seem to me that in this case:

function not_set_then_go($var, $url, $varname) {	if (!isset($var)) {

The variable would always be set, because it's defined in the function definition. The variable would always be set, but wouldn't necessarily have a value. You could use the empty function to check if the variable is empty, but I would think it would always be set. For any variable you could instead pass it by reference, and I think that would work (although passing the variable to the function at all might still set it at that point), or for a global variable you could just pass the name of the variable and then check if $GLOBALS[$varname] is set.

Link to comment
Share on other sites

Thanks for the reply, justsomeguy. I thought about that after I posted, so I added || $var == "")so it looks like this:

function not_set_then_go($var, $url, $varname) {	if (!isset($var) || $var == "") {		$go = $url . "?error=" . $varname;		header("location: $go"); 	} }

is $var == "" the same thing as saying empty($var) ?I get a little confused when knowing whether to use NULL or empty or saying == ""Thanks for your help.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...