Newbie needing basic help in php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
leegee3
Forum Newbie
Posts: 10
Joined: Thu Mar 29, 2007 7:28 am

Newbie needing basic help in php

Post by leegee3 »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hey guys, below is my software spec...

Win XP Pro
IE7
PHP5
Apache 2
I am running all files locally on the machine, they are not hosted anywhere...

As I said, I am completely new to html, php, mysql, and am learning from a simple book, my problem is this...

I have a simple html form that the user can insert their name and select a few options.  When they click the submit button, the information is supposed to be passed to a PHP file that  displays a little welcome message which includes their name and the options from the previous page.  But the information is not getting passed for some reason.
Do I need to enable something on my IE7 to allow data to get passed at all.  Below is the code

HTML file...

[syntax="html"]<html><head><title> Your Favourites </title></head>
<body>

<form action="fav.php" method="post">
<b> Please enter your name: </b> <br>
<input type="text" size="45" name="username"> <br><br>
<b> Please select your favourite wine </b><br>
<input type="radio" name="colour" Value="White"> White
<input type="radio" name="colour" Value="Rosé"> Rosé
<input type="radio" name="colour" Value="Red"> Red <br><br>
<b> Please enter your favourite dish </b><br>
<input type="text" size="45" name="dish"> <br><br>
<input type="submit" value="Submit">
</form>

</body>
</html>

PHP file...[/syntax]

Code: Select all

<html><head><title> Your Submission </title></head>
<body>

<?
	if($username !=null)
{
	echo("Thanks for your selection $username<hr>");
}


	if( ($colour !=null) && ($dish !=null) )
{
	$msg="You really enjoy $dish<br>";
	$msg.="- especially with a nice $colour wine";
	echo ($msg);
	
}

?>

</body>
</html>
Please help...

Lee


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

Change $username, $colour and $dish to $_POST['username'], $_POST['colour'] and $_POST['dish'].

Your book assumes superglobals are on, which is often considered bad practice. When accepting and dealing with variables passed from a HTML form, and the form method attribute is set to post, always reference the variables how I have demonstrated.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

If the book you are using is teaching you to rely on register_globals being on, burn the book and go get a real book.

You should always fetch your form data through the $_POST super global array and then check it, validate it and report errors to your users so they can't screw with your app.
leegee3
Forum Newbie
Posts: 10
Joined: Thu Mar 29, 2007 7:28 am

Post by leegee3 »

Hey guys, thanks for this information. I have changed what you said but I get an error message in explorer now which I cant understand. I have checked the php code over and over but cant find anything...

Code: Select all

<html><head><title> Your Submission </title></head>
<body>

<?
	if($_POST['username'] !=null)
{
	echo("Thanks for your selection $_POST['username']<hr>");
}


	if( ($_POST['colour'] !=null) && ($_POST['dish'] !=null) )
{
	$msg="You really enjoy $_POST['dish']<br>";
	$msg.="- especially with a nice $_POST['colour'] wine";
	echo ($msg);
	
}

?>

</body>
</html>
The error message in IE is as follows...

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\Program Files\xampp\htdocs\fav.php on line 9
leegee3
Forum Newbie
Posts: 10
Joined: Thu Mar 29, 2007 7:28 am

Post by leegee3 »

Sorry guys to be a pain. I found the problem...

I removed the quotation marks from the field names...

From $_POST['username']
To $_POST[username]


Thanks a lot for your help though. Just out of curiosity, how would I turn on the global attributes you were talking about??

Thanks again, you were a big help
User avatar
guitarlvr
Forum Contributor
Posts: 245
Joined: Wed Mar 21, 2007 10:35 pm

Post by guitarlvr »

you can turn register globals on or off via the php.ini file with the directive "register_globals=". Once you change anything in your php.ini file, you will need to restart you webserver.

Wayne
leegee3
Forum Newbie
Posts: 10
Joined: Thu Mar 29, 2007 7:28 am

Post by leegee3 »

Found it, thanks...

Lee
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

DO NOT TURN REGISTER_GLOBALS ON! Aside from that advice, try this as well. It is a little cleaner than the way you were doing it.

Code: Select all

<?php
if (!empty($_POST['username'])
{
    echo 'Thanks for your selection ' . $_POST['username'] . '<hr>';
}

if (!empty($_POST['colour']) && !empty($_POST['dish']))
{
    $msg = 'You really enjoy ' . $_POST['dish'] . '<br>';
    $msg .= '- especially with a nice ' . $_POST['colour'] . ' wine';
    echo $msg;
}
?>
Post Reply