Jump to content

Recommended Posts

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...
}
?>

Link to comment
https://forums.phpfreaks.com/topic/84906-solved-undefined-variable-uh/
Share on other sites

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

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

 

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

 

 

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

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.

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

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

???

 

 

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...... ;D

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.

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. :)

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...

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...

 

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....

 

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.