Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-20-2013, 06:22 AM   PM User | #1
Frump
New Coder

 
Join Date: Sep 2010
Posts: 47
Thanks: 9
Thanked 0 Times in 0 Posts
Frump is an unknown quantity at this point
PHP errors?

I am running Lubuntu 12.10 and having installed Xampp-1.8.1 started to learn PHP. All went well until i followed a tutorial which showed how a PHP script passes data to an html form.

The html form
Code:
<html>
<head>
</head>
<body>

<form action="form_script.php" method="POST">

<p>Name: <input type="text"  name="name" size="30"/></p>

<p>Shirt size:
<select name="size">
<option value="small">Small</option>
<option value="medium">Medium</option>
<option value="large">Large</option>
</select>
</p>

<p>Gender:
<input type="radio" name="gender" value="girl"/>Girl
<input type="radio" name="gender" value="boy"/>Boy
</p>

<input type="submit" name="submit" value="Submit"/>

</body>
</html>
The php script
Code:
<?php

$name=$_POST['name'];
$size=$_POST['size'];
$gender=$_POST['gender'];

print "<p>Name: $name<br />Size: $size<br />Gender: $gender</p>";

?>
When i try to run the code all i get is:
Code:
Notice: Undefined index: name in /opt/lampp/htdocs/form_script.php on line 3

Notice: Undefined index: size in /opt/lampp/htdocs/form_script.php on line 4

Notice: Undefined index: gender in /opt/lampp/htdocs/form_script.php on line 5

Name:
Size:
Gender:
Could someone help me to resolve this problem please.
Frump is offline   Reply With Quote
Old 01-20-2013, 11:57 AM   PM User | #2
sunfighter
Senior Coder

 
Join Date: Jan 2011
Location: Missouri
Posts: 2,399
Thanks: 18
Thanked 352 Times in 351 Posts
sunfighter is on a distinguished road
I don't have Lubuntu 12.10 or Xampp-1.8.1, but could the missing close tag for the form have any effect?

Code:
....
<input type="submit" name="submit" value="Submit"/>
</form>  <!--      ADD THIS-->
</body>
</html>
sunfighter is offline   Reply With Quote
Old 01-20-2013, 03:58 PM   PM User | #3
Frump
New Coder

 
Join Date: Sep 2010
Posts: 47
Thanks: 9
Thanked 0 Times in 0 Posts
Frump is an unknown quantity at this point
Hi sunfighter

Trust me to have made that mistake. But i have now rectfied the error.
There's still no improvement in the situation. For some reason when i call the html form it is only displayed as code?

Shall get the html form up to standard and validated.
Frump is offline   Reply With Quote
Old 01-20-2013, 04:10 PM   PM User | #4
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,521
Thanks: 45
Thanked 440 Times in 429 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
Try this and get back to us with the results:

PHP Code:
<?php

var_dump
($_POST);

$name=$_POST['name'];
$size=$_POST['size'];
$gender=$_POST['gender'];

print 
"<p>Name: $name<br />Size: $size<br />Gender: $gender</p>";

?>
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is online now   Reply With Quote
Old 01-20-2013, 05:11 PM   PM User | #5
Frump
New Coder

 
Join Date: Sep 2010
Posts: 47
Thanks: 9
Thanked 0 Times in 0 Posts
Frump is an unknown quantity at this point
Have finally worked it out by myself,lol. I was (as a noob) calling the php script via Firefox instead of the html form.

For example i was openning Firefox and typing: localhost/form_script.php and it was this that generated the errors.

On the other hand when i open Firefox and typed: localhost/simple_form.html
the form duly appeared and i successfully entered the required data.

Thanks for the help and advice all
Frump is offline   Reply With Quote
Old 01-20-2013, 05:14 PM   PM User | #6
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
That can be fixed by checking the isset status:
PHP Code:
<?php

if (isset($_POST['name'], $_POST['size'], $_POST['gender']))
{
    
$name=$_POST['name'];
    
$size=$_POST['size'];
    
$gender=$_POST['gender'];

    print 
"<p>Name: $name<br />Size: $size<br />Gender: $gender</p>";
}
else
{
    
header('Location: http://yoursite.com/yourform.html');
    exit();
}
?>
Would do it. You'll get very familiar with the isset construct in PHP.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 01-20-2013, 06:02 PM   PM User | #7
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,521
Thanks: 45
Thanked 440 Times in 429 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
Except he wasn't even submitting the form lol. Instead he was browsing to it lol.
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is online now   Reply With Quote
Old 01-20-2013, 07:24 PM   PM User | #8
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Yep I know. But it would have redirected to the form assuming the header was changed for it.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:42 AM.


Advertisement
Log in to turn off these ads.