Jump to content

PHP print help


gigyani

Recommended Posts

Hello. 

i am totally new to php and just started to learn now. i just dont understand why the following code is not printing the username that i enter on the page. 

Please note that the code itself is saved with the name "basicForm.php". 

Thanks.

 

<html>
<head>
<title> Basic Form </title>
<?php
 
$username = $_POST['username'];
print ($username);
 
?>
 
 </head>
 
<body>
<form name"form1" method="POST" action="basicForm.php">
<input type="text" value="username" name="username">
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
Link to comment
https://forums.phpfreaks.com/topic/294017-php-print-help/
Share on other sites

Try this:

 

<?php
error_reporting(E_ALL | E_NOTICE);
ini_set('display_errors', '1');
if (isset($_POST{'username']))
{
   $username = $_POST['username'];
   echo "You entered: $username";
   exit();
}
?>
<html>
<head>
<title> Basic Form </title>
</head>
<body>
<form name="form1" method="POST" action="basicForm.php">
<input type="text" name="username" value="username">
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>

- Fixed the name= error

- Organized your code to keep html separate from your php

- added error checking

Link to comment
https://forums.phpfreaks.com/topic/294017-php-print-help/#findComment-1503274
Share on other sites

Where is this basicForm.php located? Are you running it on a server supporting php?

The code itself is working.

 

Note though that there is a typo

<form name"form1" method="POST" action="basicForm.php">

This however is not the cause of your problem.

I am using a wamp local server and the file is located in the www directory without any subfolders. 

 

Try this:

 

<?php
error_reporting(E_ALL | E_NOTICE);
ini_set('display_errors', '1');
if (isset($_POST{'username']))
{
   $username = $_POST['username'];
   echo "You entered: $username";
   exit();
}
?>
<html>
<head>
<title> Basic Form </title>
</head>
<body>
<form name="form1" method="POST" action="basicForm.php">
<input type="text" name="username" value="username">
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>

- Fixed the name= error

- Organized your code to keep html separate from your php

- added error checking

 

Thanks alot. let me try this. meanwhile since i am totally a newbie so can you kindly explain whats happeneing here. will it display an error incase it couldnt located the file or the form field name? thanks a lot.

Link to comment
https://forums.phpfreaks.com/topic/294017-php-print-help/#findComment-1503314
Share on other sites

I will let you learn from experience. That means do as I said: Try It! And then look up the things you don't know in the manual - you know, the official PHP online manual that everyone has access to. Look that up too!

 

You'll never learn anything if you don't experiment.

Link to comment
https://forums.phpfreaks.com/topic/294017-php-print-help/#findComment-1503318
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

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.