Jump to content
xisto Community
Baniboy

Simple Php Page Not Working? why? I cant figure out.

Recommended Posts

I'm learning php, I made this simple form but it doesn't work. HELP!

Here's the form:

<html><head><title>Php</title></head><body><form action="process.php" method="post">Write your name: <input type="text" name="name" /></form></body></html>

And here's the php process page:

<?php$answer = "Mike";if ($name == $answer){	print "You've won, $name!";}else {	print "You aren't $answer, you didn't win";}?>

As far as I know this should work.
It might be something very simple that has gone wrong but I don't know what.

When I run this on wampserver it says "undefined variable" something like that.

Share this post


Link to post
Share on other sites

The first code section looks good. It is a simple form that uses the POST method to supply a value to the processing page.
In the second code section, you can't reference the variable named "$name". You must reference the $_POST['name'] variable instead.
Try this: (not tested)

<?php$answer = "Mike";if ($_POST[name] == $answer){	echo "You've won, $_POST[name] !";}else {	echo "You aren't $answer, you didn't win";}?>

Edit: tested and working. Changed the prints to echo statements. http://forums.xisto.com/no_longer_exists/

Share this post


Link to post
Share on other sites

Yes..You need to use _POST to receive the value first, as jlhaslip told. You are getting error because, you are using the varialbe $name in the process page, which is not defined and doesn't have any value.

Share this post


Link to post
Share on other sites

The first code section looks good. It is a simple form that uses the POST method to supply a value to the processing page.

In the second code section, you can't reference the variable named "$name". You must reference the $_POST['name'] variable instead.

Try this: (not tested)

<?php  $answer = "Mike";  if ($_POST['name'] == $answer) {	 print "You've won, $_POST['name'] !"; } else {	 print "You aren't $answer, you didn't win"; }  ?>

I used that but I get the following error:

Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in C:\wamp\www\phpharj\process.php on line 8

What's that supposed to mean? :P:P

Share this post


Link to post
Share on other sites

Try this......It will work...

<?php$answer = "Mike";$name = $_POST['name'];if ($name == $answer){	print "You've won, $name!";}else {	print "You aren't $answer, you didn't win";}?>

or modify this in jlhaslips code

print "You've won, {$_POST['name']} !";

Share this post


Link to post
Share on other sites

Thank you very much xpress, you really are a tech geek. It appears that the php tutorial I was reading was far too old and that you have to use $_post nowadays in newer versions of php. Now I can start learning again instead of fighting with my problems hehee. thanks a lot :P

Share this post


Link to post
Share on other sites

You're Welcome baniboy... There are many ebooks and PHP tutorials available on the net. Just google for them. You'll find many latest ones...

 

Coming to the $_POST issue, lets observe it from your HTML page. Check your <form> tag in your page. You are using the method="POST" which means you are sending the data to the server using POST method. And you are sending some value through it, so you need to receive that value in your PHP script, right?

Thats why we are using $name=$_POST['name'] to receive that name value. If you use method="GET" instead of POST then you'll use $name=$_GET['name']. Thats pretty simple....

 

And you are going in the right way...We'll learn much more effectively if we face errors. Facing errors is must to understand programming deeply....No errors, No fun. Start programming...we're here to help you.... :P

Share this post


Link to post
Share on other sites

Xpress, thanks for the backup... nice catch on the braces for the array output. I was in a hurry this morning, I guess...baniboy,See if you can find a book by Larry Ullman in your Local Library. Excellent resource to learn from. He has several books on php/mysql to study. Try to find the second edition of "PHP for the World Wide Web: Visual QuickStart Guide".

Share this post


Link to post
Share on other sites

As a note, using $name directly instead of $_POST['name'] like was done in the original code posted requires register_globals to be on, something which has been not suggested for some time and can pose security risks.

Edited by Nabb (see edit history)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.