Question

problem with superglobal variables

Asked by: nagulas

I have an html form whose values will be sent to php script using _post superglobal variable.
When I click on submit button it gives me the following in the PHP page.

Notice: Use of undefined constant table_name - assumed 'table_name' in F:\courses\CSCI5633\Spring2005\nagulas\pages\samples\do_showfielddef.php on line 2

Notice: Use of undefined constant num_fields - assumed 'num_fields' in F:\courses\CSCI5633\Spring2005\nagulas\pages\samples\do_showfielddef.php on line 2


here table_name and num_fileds are the names of text boxs present in html form. As these values will be stored in _POST super global variable (I have used post method in html form) I tried usinig these in php script it shows me the above errors.

Please help me

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2005-02-25 at 09:44:54ID21329380
Tags

php

,

undefined

,

assumed

,

superglobal

Topic

PHP Scripting Language

Participating Experts
5
Points
125
Comments
19

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Unknown error messages RE superglobals
    I am getting this error message that I've never seen before or heard of. I am trying to use some of the built-in variables in php. register_globals is on so that's not the problem... when ever I try to use one of the superglobals for example $_POST['last_name'] or $_...
  2. Why is this GLOBAL Variable not working?
    If I have a index.php that sets a variable and I then do a redirect to a second script test.php can I not have access to $GLOBALS? I understand there are SuperGlobals that are available regardless of the Register_Globals setting. I've tried on and off BTW - althought I expec...
  3. Global variables in PHP
    I am using mssql and php, so i trying to get the query result when the user enters the query and submits the execute button. i want to differentiate the syntactically correct query and the incorrect query with differ colors. here i am posting my code if (-1 == $connectio...
  4. PHP Variable Issues
    I recently spent days trying to get an ASP page to work, only to find out that the Customer site uses PHP on Linux and Linux doesn't do ASP. So I used ASP2PHP, but now I keep getting these funky Undefined Variable Errors Here's the Code **************************** 49 ec...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: BatalfPosted on 2005-02-25 at 09:48:16ID: 13405225

You shouldn't care that much about notices. If a checkbox called table_name isn't checked off before you post the form, this message would occure if you try to refer to it.

Example:
echo $_POST['table_name']; would produce that notice from PHP if the checkbox "table_name" isn't checked off.

Batalf

 

by: BatalfPosted on 2005-02-25 at 09:49:01ID: 13405230

In my example, you could replace

echo $_POST['table_name'];

with

if(isset($_POST['table_name']))

and get rid of the notice.

 

by: Diablo84Posted on 2005-02-25 at 10:29:13ID: 13405618

Batalf, please think before you tell users that they should not care about notices. While it is true that in most cases a problem that generates a notice will not be a serious issue and should not affect the functionality of the script it IS still an error. It comes under the same category as the opinion that "notices don't matter because PHP can be configured to not show them", my opinion on that is you can hide the error but it will still be there.

Further more the cause you described is a little off the mark. That would cause an undefined index error not undefined constant. The undefined constant errors are usually caused by referencing $_POST[inputname] instead of $_POST['inputname']. Without the single quotes PHP takes it as you are referencing a defined constant so it is very important to include the quotes.

Take note that the interpreter assumes that you meant $_POST['inputname'] if the constant is not defined even if you left the quotes out so the code should still work as expected even with the notice displayed. This can make getting rid of the error a little difficult if you are not familiar with the cause.

In short: make sure you include the single quotes.

 

by: BatalfPosted on 2005-02-25 at 10:43:29ID: 13405779

Diablo84,

Sorry!

You're probably right on the spot when it comes to the solution of this problem.

It wasn't my intention to let the user think that notices doesn't matter at all. My point was to inform that's there's a difference between notices and Parse/Fatal errors. It probably came out wrong in my post. I'm tired after a long day:-)

To use $_POST['inputname'] instead of $_POST[inputname] sounds logical as the solution here.

Batalf

 

by: Diablo84Posted on 2005-02-25 at 11:05:53ID: 13405970

Batalf,

That's no problem :). The vast majority of questions asked are from users who are still learning and so trust and rely on us to provide them with accurate information. If we can set them on the right path from the start they will benefit from it in the long run.

There are so many experts who are point hungry (and so post as quick as they can to make sure they get the points) that the quality of the answers has been decreasing over time. This is the main reason why i do not participate as much now. If my comments ever seem blunt it is as a result of many months of having to work around these people to maintain the standard, not to cause offence. My intention is to educate and sometimes being blunt is the most direct path.

Best Wishes,

Diablo84

 

by: gruntarPosted on 2005-02-25 at 11:56:49ID: 13406396

Hi, nagulas.

Your MAIN problem with superglobal vars is that you're calling it like this

$_POST[table_name] instead of  $_POST['table_name']  /* note the quote around table_name */

second problem with unset vars that are called is solved with checking if it is set

$_POST[table_name] = (isset($_POST[table_name]) ? $_POST[table_name] : '');
/* that way you retain scope of variable and "avoid" notices */

Hope it is clear now.
cheers

 

by: nagulasPosted on 2005-02-25 at 14:48:06ID: 13407808

When I placed quotes as $_POST['table_name'] it is giving me an error saying

Notice: Undefined index: 'table_name' in F:\courses\CSCI5633\Spring2005\nagulas\pages\samples\do_showfielddef.php on line 13

what should I do now

 

by: BatalfPosted on 2005-02-25 at 14:51:50ID: 13407847

You could use isset to make sure that the variable exists.

Example:

instead of

if($_POST['table_name']

you should write

if(isset($_POST['table_name'])

To be able to give you more specific advice, you should post parts of the codes where the problem is.

Batalf

 

by: nagulasPosted on 2005-02-25 at 15:02:13ID: 13407916

I got it, we should use single quotes only when superglobal is not enclosed in double quotes if it is enclosed in double quotes there is no need to give single quotes within superglobal variable

Problem is resolved

 

by: Diablo84Posted on 2005-02-25 at 17:48:05ID: 13408595

I don't think you quite have it, or i may just not have followed what you have said. Either way:

When referencing array indexes (including the super globals) always use single quotes as a general rule. Without the quotes it looks for a constant of the specified name. If you are not familiar with constants you may wish to look here: http://us2.php.net/manual/en/function.define.php

There is also a difference between double quotes and single quotes in PHP,
http://us2.php.net/manual/en/language.types.string.php
The main factor is double quoted strings are parsed, single quoted strings are not, eg:

$var = 'my variable string';

echo "This is a string including $var"; //outputs: This is a string including my variable string

echo 'This is a string including $var'; //outputs: This is a string including $var

If you don't need to output variable values in a string then use single quotes so there is no unnecessary interpolation.

 

by: ZhaawZPosted on 2005-02-25 at 17:59:53ID: 13408621

nagulas, if you're using arrays (superglobals or other ones) enclosed in double quotes, you don't need to enclose an index in quotes only if you're using simple syntax (not a complex one) or if type of index is not string.

$arr['xxx'] = 'some string';
echo "blaa blaa $array[xxx]"; // simple syntax - don't need single quotes
echo "blaa blaa {$array['xxx']}"; // complex syntax - need single quotes, if index is string

$arr[123] = 'some string';
echo "blaa blaa $array[123]"; // simple syntax - don't need single quotes
echo "blaa blaa {$array[123]}"; // complex syntax - works fine without single quotes because an index is not a string

why to use complex syntax? sometimes you have to use multidimensional arrays:
$array['x1']['y1'] = 'some string';
echo "blaa blaa $array[x1][y1]"; // this will output 'blaa blaa Array[y1]'
echo "blaa blaa {$array[x1][y1]}"; // this will output 'blaa blaa some string'

 

by: gruntarPosted on 2005-02-25 at 23:22:05ID: 13409319

I made a mistake copying your code instead of mine. So again, to avoid those notices you must add this line of code for each var that generate notice..

$_POST['table_name'] = (isset($_POST['table_name']) ? $_POST['table_name'] : '');
/* just replace table_name with var that you're trying to use */

cherrs

 

by: nagulasPosted on 2005-02-26 at 08:37:36ID: 13410864

HI Diablo84,

I am having problems with these superglobal variables on my server, but there is no need for me to keep in quotes on my desktop machine, why is that different on my server there is an old version of PHP it is 4.XX, but on my desktop I have installed latest version, is it because of version that I need to keep quotes.

 

by: nagulasPosted on 2005-02-26 at 08:48:20ID: 13410911

Do we need to make any modifications in php.ini file inorder to turn on error reporting.

 

by: ZhaawZPosted on 2005-02-26 at 12:38:45ID: 13411857

You should modify your php.ini file.

To show all errors and notices:
error_reporting  =  E_ALL

To show errors, but hide notices:
error_reporting  =  E_ALL & ~E_NOTICE

Don't forget to restart Apache (or other server) after changing php.ini file

 

by: gruntarPosted on 2005-02-26 at 23:38:04ID: 13413293

Hi,

you don't have to change PHP.ini (usually, you don't have permission to do this on hosting provider).

just add this line of code at the beginning of a file (best way would be to put it in a file that is included, that way you have to change only one file).

error_reporting(E_ALL ^ E_NOTICE);

cheers

 

by: Diablo84Posted on 2005-02-27 at 13:37:37ID: 13415867

nagulas,

"but there is no need for me to keep in quotes on my desktop machine"

It's not that there isn't a need for it, it's just your local machine is configured differently to your server so the error isn't displayed. As i mentioned before even though the error is not shown it is still there so it is good practice to get in the habit of writing clean code (ie. in this particular case make sure you include the quotes).

The usual set up is to have the error level configured to E_ALL on the development server to show ALL errors and the live server configured at a slightly lower level (therefore if anything should go wrong on the live site there are no unprofessional looking errors cluttering up the page). In any case the ideal situation is to have all errors shown using E_ALL as it will encourage you to write better code and obviously provides the extra aid with debugging.

 

by: hujiPosted on 2006-02-03 at 03:43:04ID: 15861886

No comment has been added to this question in more than 21 days, so it is now classified as abandoned..
I will leave the following recommendation for this question in the Cleanup topic area:
Accept Batalf's comment {http:#13405230} as answer.

Any objections should be posted here in the next 4 days. After that time, the question will be closed.

Huji
EE Cleanup Volunteer

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...