Jump to content

Help with PHP


Prash

Recommended Posts

Hey guys,

 

I've tried making a PHP form that asks you your name and age (post to welcome.php) and on the welcome.php it says

 

Welcome $namevar.

you are $agevar years old.

 

BUT, if i try this it doesn't work,

 

when i press submit it says Welcome, you are years old.

 

This is the script:

 

1.htm:

<html>
<body>

<form action="welcome.php" method="post">
Name: <input type="text" name="name" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>

</body>
</html>

 

welcome.php:

 

<html>
<body>

Welcome <?php echo $_POST["name"]; ?>.<br />
You are <?php echo $_POST["age"]; ?> years old.

</body>
</html>

 

and yes they are both in the same folder.

 

-Thanks!

 

 

 

Link to comment
Share on other sites

*NOTE* Tested and working!

There are many mistakes and hopefully you will learn by taking a look at these:

1.php

<?php

/**
* @author ShannenName
*/
print "<form action='welcome.php' method=post>
Name: <input type='text' name='name' value='Your Name Here'>
Age: <input type='text' name='age' value='Your Age Here'>
<input type='submit' value='Go'></form>";

?>

welcome.php

<?php

/**
* @author ShannenName
* @copyright 2007
*/

print "Welcome {$_POST['name']}!
You are {$_POST['age']} years old!";

?>

Link to comment
Share on other sites

EDIT

One little mistake should work now!

/EDIT

You should use <?php at the start of the page and ?> at the end not in the middle oh a html document also,

the basic html page consists of <html><head><title><body> not just <html> and <body> tags and if you are going to use echo (exact same as print) you have to have quotes, eg

echo "hello";

NOT echo hello;

and for the form inputs you don't end it with /> you just end with >

 

Also if should of posted the simplified version of this script (consists of only one page) so I will do so now

<?php
/**
* @author ShannenName
* @copyright 2007
*/
if($_POST['submit'])
{
print "Welcome {$_POST['name']}!
You are {$_POST['age']} years old!";
}
else
{
print "<form action='welcome.php' method=post>
Name: <input type='text' name='name' value='Your Name Here'>
Age: <input type='text' name='age' value='Your Age Here'>
<input type='submit' name='submit' value='Go'></form>";
}
?>

Link to comment
Share on other sites

  • 5 weeks later...

Actually, I believe the use of inline PHP is more efficient than just having <?php ...entire file... ?> and displaying text with echos. Inline tags should be fine.

 

Also, using the "/" at the end of inputs is fine as well. It's the xhtml standard to terminate unpaired tags with "/".

 

Try running your html through the W3C validator sometime, it's fun.

I ran the one fragment of php I still have that I wrote long, long ago through it and it found something like 20 or 30 errors. :P

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...