473,326 Members | 2,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Notice: Undefined variable

5
Thank you. I have another problem with Form.
I have this code in my first formpage01.php
Expand|Select|Wrap|Line Numbers
  1. <form method="post" action="hiUser.php">
  2. Please type your name: <input type="text" name="userName" value="" />
  3. <br />
  4. <input type="submit" />
  5. </form>

And here's my code in hiUser.php
Expand|Select|Wrap|Line Numbers
  1. <?php 
  2. print "<h3>Hi there, $userName</h3>";
  3. ?>
The ERROR when I hit the Submit button is:
Notice: Undefined variable: userName in C:\wamp\www\phpPractice\hiUser.php on line 13
Thanks in advance
Mar 23 '10 #1

✓ answered by Atli

This is because you need to use the $_POST or $_GET arrays to read data posted to PHP by a <form>. Which on you should use depends on the "method" attribute of the form.

In your case, you should use $_POST:
Expand|Select|Wrap|Line Numbers
  1. $userName = $_POST['userName'];
  2. print "<h3>Hi there, $userName</h3>";
Also, for future reference, when you print "external" or "unsafe" data -- which is basically everything that is not "hard coded" into your code -- into a HTML page, you should run it through htmlentities before printing it.
Expand|Select|Wrap|Line Numbers
  1. $userName = htmlentities($_POST['userName'], ENT_QUOTES, "ISO-8859-1");
  2. print "<h3>Hi there, $userName</h3>";
(Note that if you use a different charset, like UTF-8, you need to change the third parameter to reflect that.)

P.S.
What you did in your code is possible, using the now obsolete register_globals directive. But that feature has been made deprecated for security reasons and will be removed in future versions of PHP, so it is a bad idea to keep using it.

P.P.S.
I've split this question from your other thread into it's own thread. Please post new questions in new threads.

4 5412
Atli
5,058 Expert 4TB
This is because you need to use the $_POST or $_GET arrays to read data posted to PHP by a <form>. Which on you should use depends on the "method" attribute of the form.

In your case, you should use $_POST:
Expand|Select|Wrap|Line Numbers
  1. $userName = $_POST['userName'];
  2. print "<h3>Hi there, $userName</h3>";
Also, for future reference, when you print "external" or "unsafe" data -- which is basically everything that is not "hard coded" into your code -- into a HTML page, you should run it through htmlentities before printing it.
Expand|Select|Wrap|Line Numbers
  1. $userName = htmlentities($_POST['userName'], ENT_QUOTES, "ISO-8859-1");
  2. print "<h3>Hi there, $userName</h3>";
(Note that if you use a different charset, like UTF-8, you need to change the third parameter to reflect that.)

P.S.
What you did in your code is possible, using the now obsolete register_globals directive. But that feature has been made deprecated for security reasons and will be removed in future versions of PHP, so it is a bad idea to keep using it.

P.P.S.
I've split this question from your other thread into it's own thread. Please post new questions in new threads.
Mar 23 '10 #2
Bentot
5
Thanks for replying. Sorry about the new question. Actually I solved my problem while waiting for an answer. I Googled again and I found out that register_global is probably off so I wrote the following code at the top of my hiUser.php page:
Expand|Select|Wrap|Line Numbers
  1. <?php 
  2. $userName = $_REQUEST["userName"];
  3. ?>
I also tried your suggestion $userName = $_POST['userName']; just now and it worked as well. Which one should I use?

Thanks
Mar 23 '10 #3
Atli
5,058 Expert 4TB
It's generally better to avoid using $_REQUEST. It contains all the values of $_GET, $_POST and $_COOKIE, meaning that if any of them contain elements with the same name, one will overwrite the other.

You will also never be sure where the variable is coming from if you use $_REQUEST. It could be coming from any of the three sources. For example, if you use $_REQUEST in your script, and rather than using the form to post to it you just enter the URL of the "action" page directly, like so:
- http://example.com/action.php?userName=xyz
This will also be considered valid, even tho you didn't go through the form.
Mar 23 '10 #4
Bentot
5
ok I'll do that.

Thank you very much.
Mar 23 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Mike | last post by:
I'm new to PHP - moving over from ASP. I have a number of include files, the first of which sets the value of a variable $loginmsg. I use that variable in a subsequent include file, but get a...
1
by: prabhunew2005 | last post by:
hi I am doing web portal design using php. I gave part of one of my screen coding. <html> <head> <script language = "javascript"> function list_all_click()
4
by: dmain1971 | last post by:
I have a class with an empty array like so: private $detail = array(); Then I have a function later on in the class: function get_array_value() { $detail_info = ' ';
3
by: number1yan | last post by:
Can anyone help me, i am creating a website and am using a php script that recomends the website to other people. I keep getting the same error and can not work out why. The error is: Notice:...
2
by: Reggie | last post by:
am trying to create a a upload file.am uploading files ok but i recieve this message. Notice: Undefined variable: uploaded_size in /home/fhlinux169/c/ clashoff.co.uk/user/htdocs/upload.php on...
3
by: printline | last post by:
Can anyone help me with why i am getting this error message: Notice: Undefined variable: SalesRepID in C:\Inetpub\wwwroot\index.php on line 158 Here is my php code: <?php session_start();...
5
by: movieking81 | last post by:
Another PHP newbie here, I trolled the boards here trying some of the different solutions but I keep getting the errors over an over. maybe my problem is specific. I keep getting the following when...
5
by: Pseudonyme | last post by:
Dear All : Ever had an httpd error_log bigger than the httpd access log ? We are using Linux-Apache-Fedora-Httpd 2006 configuration. The PHP lines code that lead too tons of errors are : ...
1
by: francsutherland | last post by:
Notice: Undefined index: send in D:\Domains\workingdata.co.uk\wwwroot\contact_text.php on line 7 Hi, I've started getting this error in the contact page form of my website. The web hosting...
10
by: FutureShock | last post by:
I have since recently turned up my error reporting on a production site to E_ALL to ensure I am using 'best practices' when writing out code. So far it has helped me discipline myself with...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.