LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-29-2010, 04:06 PM   #1
mindsport
Member
 
Registered: Apr 2005
Location: Florida
Distribution: Slackware 13.1-64bit / 2.6.33.4
Posts: 46

Rep: Reputation: 17
PHP - POST not working?


The following script keeps telling me: Notice: Undefined index: name in /usr/files/www/rage.php on line 6 Name:

any ideas?
PHP Code:
<html>
    <?php
        error_reporting
(E_ALL);
        
ini_set('display_errors',1);

        echo 
"Name: ".$_POST['name']."<br>\n";
    
?>

    <br><br><br>
    <div align='center'>
        <form method="post" action="rage.php">
            Name:   <input type='text'   name='name'  size=40>
            URL:    <input type='text'   name='url'   size=60>
            Genre:  <input type='text'   name='genre' size=40>
            <br><br><input type='submit' value='Submit'>
        </form>
    </div>
</html>
 
Old 12-29-2010, 04:51 PM   #2
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 13,163
Blog Entries: 1

Rep: Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032
You can turn off notices, by setting display_errors to 0:
PHP Code:
<html>
    <?php
        error_reporting
(E_ALL);
        
ini_set('display_errors',0);

        echo 
"Name: ".$_POST['name']."<br>\n";
    
?>

    <br><br><br>
    <div align='center'>
        <form method="post" action="rage.php">
            Name:   <input type='text'   name='name'  size=40>
            URL:    <input type='text'   name='url'   size=60>
            Genre:  <input type='text'   name='genre' size=40>
            <br><br><input type='submit' value='Submit'>
        </form>
    </div>
</html>
Regards
 
Old 12-29-2010, 04:53 PM   #3
mindsport
Member
 
Registered: Apr 2005
Location: Florida
Distribution: Slackware 13.1-64bit / 2.6.33.4
Posts: 46

Original Poster
Rep: Reputation: 17
I put the notices on because the script isn't working. When I put information into the form it gives the error because the values aren't being written to the index in the array and I cant figure out why?
 
Old 12-29-2010, 05:06 PM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,704

Rep: Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896
Actually it is working. The variable does not exist until you press the form submit button. As stated setting display errors to 0 will not print the error to the screen.
 
Old 12-29-2010, 05:13 PM   #5
mindsport
Member
 
Registered: Apr 2005
Location: Florida
Distribution: Slackware 13.1-64bit / 2.6.33.4
Posts: 46

Original Poster
Rep: Reputation: 17
I'm saying I've ran the script with putting information into the fields and pressed submit and its not printing the value for name that I put in there, its giving me the error instead.. Removing this just prints nothing..
 
Old 12-29-2010, 06:14 PM   #6
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Your example produces malformed HTML. Browsers may understand it, or they may not. This works:
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><?PHP
header('Content-Type: text/html; charset=UTF-8');
$name = @$_POST['name'];
$safename = htmlentities($name, ENT_COMPAT, 'UTF-8');
?><html>
<head>
<title> Test page </title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<p>Name: <?PHP echo $safename; ?></p>
<form method="POST" action="?" accept-charset="UTF-8">
<input type="text" name="name" value="<?PHP echo $safename; ?>">
<input type="submit" value="Submit">
</form>
</body>
</html>
  • Don't trust a browser. Always prefer standards, for example HTML 4.01 and CSS 2. The W3C Markup Validator is your friend.
  • Browsers like HTML attributes with double quotes. Bugs are known to occur without quotes and with single quotes.
  • Here, $name is either the "name" variable from POST, or null. @ suppresses the warning if "name" does not exist in $_POST.
  • Beware of nasty input. $safename is the "name" variable with special characters and double quotes converted to entity references; the result can be used anywhere in HTML.
  • If you were to save $name to a database or a file, use appropriate escaping functions to $name. When reading from a database or file, first do the converse de-escaping, then derive $safename using htmlentities() or something similar. Use $safename everywhere in HTML output.
  • You can look up any PHP function using http://www.php.net/function.

Hope this helps,
Nominal Animal

Last edited by Nominal Animal; 03-21-2011 at 01:50 AM.
 
Old 12-30-2010, 02:54 AM   #7
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
You can wrap you diagnostic around an isset() call
PHP Code:
if (isset($_POST['name']))
   echo 
"Name: ".$_POST['name']."<br>\n";
else
   echo 
"Form not yet submitted." 
 
Old 01-02-2011, 09:36 PM   #8
mindsport
Member
 
Registered: Apr 2005
Location: Florida
Distribution: Slackware 13.1-64bit / 2.6.33.4
Posts: 46

Original Poster
Rep: Reputation: 17
I still couldn't get this to work, I was using lighttpd and I'm guessing it was the problem because when I installed apache everything just the way it was before worked this time. Thanks for all the help!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP: code says POST, but uses GET konqi Programming 2 05-18-2008 03:00 AM
Post current IP address to .php PatheticalPanic Linux - Networking 3 06-27-2006 12:37 AM
php post action server-solution Programming 2 05-09-2006 07:46 AM
PHP Question about POST data BrianK Programming 7 11-26-2005 01:33 PM
PHP Post and GET method dai Programming 4 02-26-2003 09:45 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:00 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration