PHPNewbie55 Posted January 7, 2008 Share Posted January 7, 2008 I just need someone to explain to me why I am getting this error.. Undefined variable: action OK the $action variable is being passed along with the URL to the following page... example page.php?action=dothis so on that page.php if there is no action I get the error Undefined variable: action So how can I DEFINE the action...?? I have tried putting $action = ""; but that just makes all the actions blank and the page does nothing no matter what the variable is in the url... I have tried putting $action = "$action"; but that is the same as no action being defined... It is just really confusing me why I would have to define any $action..? The code says if there is no ACTION show index.. I can't figure out how to define the $action without screwing up the rest of the real $action's.... <?php if (!$action) { show index.... } if ($action = "dothis") { do_it... } ?> Quote Link to comment https://forums.phpfreaks.com/topic/84906-solved-undefined-variable-uh/ Share on other sites More sharing options...
revraz Posted January 7, 2008 Share Posted January 7, 2008 Don't you want $_GET['action'] ? Quote Link to comment https://forums.phpfreaks.com/topic/84906-solved-undefined-variable-uh/#findComment-432891 Share on other sites More sharing options...
dsaba Posted January 7, 2008 Share Posted January 7, 2008 to call variables passed through HTTP GET, you must say: url.com?variableName=whatever echo $_GET['variableName']; Also I noticed this: if (!$action) { this if statement will only be entered IF $action is equal to the boolean value of false or the integer value of 0 if you want to check if the var $action has BEEN SET or DEFINED you better say: if (isset($action)) { or in your case: <?php if (isset($_GET['action'])) { echo 'woo hoo! I know how to access variables passed through GET, you better watch out POST, because you are next!'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/84906-solved-undefined-variable-uh/#findComment-432895 Share on other sites More sharing options...
PHPNewbie55 Posted January 7, 2008 Author Share Posted January 7, 2008 That is what it is basically... I think... The code is all on one page... If there is no action set then the FORM is shown.. If the action is SUBMIT then the form is submitted... If the action is RESULTS then the form submission results are shown.. So one of the first lines in the code is <?php if (!$action) { show index.... } ?> Then in my forms: <?php <form id="form1" name="form1" method="post" action="'.$_SERVER['PHP_SELF'].'?action=submit"> ?> Quote Link to comment https://forums.phpfreaks.com/topic/84906-solved-undefined-variable-uh/#findComment-432896 Share on other sites More sharing options...
revraz Posted January 7, 2008 Share Posted January 7, 2008 But this code doesn't look at the URL passed, so it's useless <?php if (!$action) { show index.... } ?> Quote Link to comment https://forums.phpfreaks.com/topic/84906-solved-undefined-variable-uh/#findComment-432898 Share on other sites More sharing options...
dsaba Posted January 7, 2008 Share Posted January 7, 2008 <?php if (!$_GET['var']) { echo 'hello'; //it echoes hello //this also delivers a warning, if you have warnings turned on, nevertheless isn't it good to have no warnings? } if (!isset($_GET['var'])) { //it just echoes hello echo 'hello'; } ?> This raises an interesting question, is it good programming practice to on default make undefined variables equal boolean false? Apparently, PHP does do this on undefined variables. I think there should be a distinction between false and an undefined variable. Quote Link to comment https://forums.phpfreaks.com/topic/84906-solved-undefined-variable-uh/#findComment-432900 Share on other sites More sharing options...
Ken2k7 Posted January 7, 2008 Share Posted January 7, 2008 <?php if (!$_GET['action']){ // show index // } ?> Quote Link to comment https://forums.phpfreaks.com/topic/84906-solved-undefined-variable-uh/#findComment-432901 Share on other sites More sharing options...
revraz Posted January 7, 2008 Share Posted January 7, 2008 <?php if ($_GET['action'] == "SUBMIT") { //do form submitted stuff } elseif ($_GET['action'] == "RESULTS") { //do results stuff } else { //form not submitted, show form } ?> But instead, you should check if the form is submitted by checking your submit name, not in the URL. Quote Link to comment https://forums.phpfreaks.com/topic/84906-solved-undefined-variable-uh/#findComment-432907 Share on other sites More sharing options...
chronister Posted January 7, 2008 Share Posted January 7, 2008 This is a warning. You can avoid seeing it by setting error reporting to a higher level, however that don't fix it, but just hides it. It is best to verify that a var is set first. <?php if(isset($_GET['action'])) { $action = $_GET['action']; } if (!isset($action)) { show index.... } elseif (isset($action) && $action = "dothis") { do_it... } ?> Thats what I would do for the code you have listed. Quote Link to comment https://forums.phpfreaks.com/topic/84906-solved-undefined-variable-uh/#findComment-432910 Share on other sites More sharing options...
PHPNewbie55 Posted January 7, 2008 Author Share Posted January 7, 2008 If I use any of these examples it just kills the entire script... <?php if (isset($_GET['action'])) if (!$_GET['action']){ ?> And if I take this error reporting out error_reporting(E_ALL); The script works as expected...... So I just don't get it... I'll keep hacking away until I do get it though... it's just frustrating Quote Link to comment https://forums.phpfreaks.com/topic/84906-solved-undefined-variable-uh/#findComment-432942 Share on other sites More sharing options...
PHPNewbie55 Posted January 7, 2008 Author Share Posted January 7, 2008 Maybe this... Instead of just using the $action in the url... What I may need to do is use another variable that I can define to grab the $action from the url.... Something like:::: <?php $var = "$action"; // this would call the variable passed in the url... if (!$var) { show index.... } if ($var = "dothis") { do_it... } ?> But I would probably still get the error message error Undefined variable: action ??? Quote Link to comment https://forums.phpfreaks.com/topic/84906-solved-undefined-variable-uh/#findComment-432957 Share on other sites More sharing options...
revraz Posted January 7, 2008 Share Posted January 7, 2008 You are not using $action in the URL, you are using action= which uses $_GET['action']. They are not the same thing. Maybe this... Instead of just using the $action in the url... Quote Link to comment https://forums.phpfreaks.com/topic/84906-solved-undefined-variable-uh/#findComment-432961 Share on other sites More sharing options...
PHPNewbie55 Posted January 7, 2008 Author Share Posted January 7, 2008 I don't get it... the form on the first page is: <form id="form1" name="form1" method="post" action="'.$_SERVER['PHP_SELF'].'?action=submit"> Then once the form is submitted it goes to another section of the script... <?php if ($action = 'submit') do this... ?> And without the error reprting turned on it does the submit function... So when you say I am not using the $action from the url.. where else is the action coming from that says "submit".... I pass a lot of variables through the url:: mydomain.com?id=12345 to grab the mysql info from the database for id#12345 and it has always worked fine..... Maybe I just didn't get enough coffee this morning or something...... Quote Link to comment https://forums.phpfreaks.com/topic/84906-solved-undefined-variable-uh/#findComment-432972 Share on other sites More sharing options...
chronister Posted January 7, 2008 Share Posted January 7, 2008 This is a warning. You can avoid seeing it by setting error reporting to a higher level, however that don't fix it, but just hides it. It is best to verify that a var is set first. <?php if(isset($_GET['action'])) { $action = $_GET['action']; } if (!isset($action)) { show index.... } elseif (isset($action) && $action = "dothis") { do_it... } ?> Thats what I would do for the code you have listed. did you try this?? It works for me.. when I don't pass a var called action in the URL, I get Show Index on screen, when I pass a var called index with a value of dothis it gives me do_it... when a var is passed via the URL, you have to access it as $_GET['var'], you cannot just access it as $var. Take a look at my code again, it is correct and works just fine. Some servers are set to auto register super globals, but I would not rely on it. Always address url passed vars through the $_GET[] superglobal. Quote Link to comment https://forums.phpfreaks.com/topic/84906-solved-undefined-variable-uh/#findComment-432976 Share on other sites More sharing options...
trq Posted January 7, 2008 Share Posted January 7, 2008 You are programming assuming the register globals is on. By default (and for security reasons) register globals have been off for a long time in php. Use the $_GET and $_POST superglobals. Quote Link to comment https://forums.phpfreaks.com/topic/84906-solved-undefined-variable-uh/#findComment-432977 Share on other sites More sharing options...
Ken2k7 Posted January 7, 2008 Share Posted January 7, 2008 PHPNewbie55, this may help. $action is a variable. What you're doing is placing a string "action=whatever" in the URL field, am I right so far? Well, you can't ASSUME $action will have the string "whatever" in it. $action is a variable. IT IS NOT USED TO GET DATA FROM THE URL! To get the data of action in the URL, you need to use $_GET['action']. $_GET['action'] = "whatever". So when you just say if (!$action) or something like that, you never defined the variable $action. You need to read up on these things so you don't end up confusing yourself and making the same mistakes EVEN AFTER people have given you the correct code, all written out for you and all you had to do was copy and paste. I hope you understand. Quote Link to comment https://forums.phpfreaks.com/topic/84906-solved-undefined-variable-uh/#findComment-432982 Share on other sites More sharing options...
PHPNewbie55 Posted January 7, 2008 Author Share Posted January 7, 2008 OH ok... I guess since I am doing my own thing on my own server I shouldn't assume everyone's server is set up like mine.. So I should use the code that is set to work on a generic system... mmm... I guess I have a lot of work recoding some pages that don't use the superglobals Ken2k7 cool I understand.. but it's not as simple as copy and paste.. what I posted for the example is just a simple script that would give you an idea of what I was talking about... actually transferring the changes to this script will be a job... Quote Link to comment https://forums.phpfreaks.com/topic/84906-solved-undefined-variable-uh/#findComment-432996 Share on other sites More sharing options...
revraz Posted January 7, 2008 Share Posted January 7, 2008 You should code for what is set to work now and in the future, not for what is being depreciated in upcoming versions. OH ok... I guess since I am doing my own thing on my own server I shouldn't assume everyone's server is set up like mine.. So I should use the code that is set to work on a generic system... Quote Link to comment https://forums.phpfreaks.com/topic/84906-solved-undefined-variable-uh/#findComment-433000 Share on other sites More sharing options...
trq Posted January 7, 2008 Share Posted January 7, 2008 As I said, register globals has been off by default (depricated) for a long time in php. Since php-4.1.0 (10 Dec 2001) in fact. Quote Link to comment https://forums.phpfreaks.com/topic/84906-solved-undefined-variable-uh/#findComment-433003 Share on other sites More sharing options...
simcoweb Posted January 7, 2008 Share Posted January 7, 2008 Heh... why couldn't he just put: $action = $_GET['action']; right after the opening <?php tag? That would be a simple way to populate the variable and allow his code to run. Quote Link to comment https://forums.phpfreaks.com/topic/84906-solved-undefined-variable-uh/#findComment-433015 Share on other sites More sharing options...
chronister Posted January 7, 2008 Share Posted January 7, 2008 Heh... why couldn't he just put: $action = $_GET['action']; right after the opening <?php tag? That would be a simple way to populate the variable and allow his code to run. I already hit on that one about 3 posts ago Quote Link to comment https://forums.phpfreaks.com/topic/84906-solved-undefined-variable-uh/#findComment-433029 Share on other sites More sharing options...
PHPNewbie55 Posted January 7, 2008 Author Share Posted January 7, 2008 Just to explain how ignorant I am.. I didn't even know what a global or a superglobal was.. and I still have to do some research on what they are and how to use them... Oh and it wasn't that hard to fix that code... I just had to paste one line of code... <?php if(isset($_GET['action'])) { $action = $_GET['action']; } else { $action = ""; } ?> Now it all works fine with no errors... I thought I would have to change every instance of $action and that would be a lot harder... Thank you for the answer...!!!!!! simcoweb - that is kind of what I did.... Quote Link to comment https://forums.phpfreaks.com/topic/84906-solved-undefined-variable-uh/#findComment-433032 Share on other sites More sharing options...
PHPNewbie55 Posted January 7, 2008 Author Share Posted January 7, 2008 Sorry about the confusion on my part... I am not a programmer.. and I really don't know what I am doing at all.. so I am lucky that I can get anything to run correctly... every time I do it amazes me... LOL NOOB................ Quote Link to comment https://forums.phpfreaks.com/topic/84906-solved-undefined-variable-uh/#findComment-433040 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.