music_lp90 Posted October 4, 2007 Share Posted October 4, 2007 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 More sharing options...
Aureole Posted October 4, 2007 Share Posted October 4, 2007 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.. Link to comment Share on other sites More sharing options...
jhecht Posted October 4, 2007 Share Posted October 4, 2007 Why not just use javascript to check if all fields are entered so that you dont have to refresh the page? Link to comment Share on other sites More sharing options...
music_lp90 Posted October 4, 2007 Author Share Posted October 4, 2007 Well, I don't really know much javascript right now. I will be learning about it in a class pretty soon, but I was also thinking this might be good if someone didn't have javascript enabled. However, I guess not many people would have javascript disabled. Link to comment Share on other sites More sharing options...
boen_robot Posted October 4, 2007 Share Posted October 4, 2007 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 likefunction 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 More sharing options...
music_lp90 Posted October 4, 2007 Author Share Posted October 4, 2007 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 More sharing options...
justsomeguy Posted October 4, 2007 Share Posted October 4, 2007 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 More sharing options...
music_lp90 Posted October 4, 2007 Author Share Posted October 4, 2007 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 More sharing options...
justsomeguy Posted October 4, 2007 Share Posted October 4, 2007 Yeah, empty checks for the empty string. If you want to use isset you should be able to do one of these: function check (&$var){ if (!isset($var)) echo "bah!!!!"; function check ($varname){ if (!isset($GLOBALS[$varname]))... Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.