Jump to content

PHP print help


gigyani
Go to solution Solved by Ch0cu3r,

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
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.

Edited by dde
Link to comment
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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...

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.