Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Good day

I am a beginner in PHP. I want to create a PHP webpage with user input. I checked the link http://www.w3schools.com/php/php_forms.asp[^]on w3schools to create a form with html and php with user input.

Here is the code
//HTML FORM

XML
<html>
 <body>

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

</body>
 </html>


//PHP FORM (welcome.php)
XML
<html>
 <body>

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

</body>
 </html>


How can I modify the code that I do not need to create two webpages(PHP and HTML)? All the code must be on a single page. I do not want to get the welcome.php form the html page.
Posted
Updated 9-Mar-12 0:26am
v6
Comments
I.explore.code 9-Mar-12 6:49am    
Try setting the "action=#" and move your PHP code in the HTML page, but add null checks before accessing $_POST array. Haven't done PHP in a while so not entirely sure, but "#" would mean that the form would post the data back to itself and then the null checks before accessing the data from PHP would make sure that you don't get data errors. Try and let me know :)

1 solution

What i get is that you want welcome.php code in HTML page??
Make your form like this
PHP
<form action="<?php $_Server['PHP_SELF]??>" method="post">
 Name: <input type="text" name="fname" />
 Age: <input type="text" name="age" />
 <input type="submit" />
 </form>


and on the top of the page(before ) you can write php scripts(ie code of welcome.php)

Hope it'll help you
 
Share this answer
 
Comments
JacoBosch 9-Mar-12 7:19am    
That's correct. I want the php code in the HTML page as well. Thanx. I will try that.
Syed Salman Raza Zaidi 9-Mar-12 7:28am    
Accept it as answer if it worked :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900