Prash Posted December 5, 2007 Posted December 5, 2007 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!
ShannenName Posted December 8, 2007 Posted December 8, 2007 *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!"; ?>
Prash Posted December 8, 2007 Author Posted December 8, 2007 Thanks mate! but could you tell me what was wrong with the script i used?
ShannenName Posted December 12, 2007 Posted December 12, 2007 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>"; } ?>
Prash Posted January 10, 2008 Author Posted January 10, 2008 ^^ Thanks! But is it because the php was embedded into html why it didn't work?
ShannenName Posted January 10, 2008 Posted January 10, 2008 Some servers don't like that but some accept but it was all the errors that stuffed you up
Prash Posted January 13, 2008 Author Posted January 13, 2008 Right... Thanks alot for your help mate Appreciated - Prash
teh silly Posted January 13, 2008 Posted January 13, 2008 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.
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now