PHP variables not showing up after submitting form (closed) - php

I'm just starting to learn form and PHP. I am testing a simple HTML file from W3Schools here with the following code:
<html>
<body>
<form action="welcome.php" method="get">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
</body>
</html>
It is supposed to pass the information to a PHP file called welcome.php, which looks like this:
<html>
<body>
Welcome <?php echo $_GET["name"]; ?><br>
Your email address is: <?php echo $_GET["email"]; ?><br>
Random thing: <?php $rand = "bananas"; echo "$rand"; ?>
</body>
</html>
When I run the HTML file on Chrome, fill in the name and the email and press submit, the page looks like this:
Welcome
Your email address is:
Random thing:
While the HTML part works, the name variable, email variable and rand variable isn't printed.
[EDIT]: I solved it by transferring the files to a server and running it by going onto the actual webpage and it worked. Also Azeez Kallayi suggested running it on Xamp.

I think you are not using any server, just opening in broswer without any server. Also correct semicolon as in the above comment.
Since PHP is a server side programming language , you need a server to execute the PHP scripts.
There are many applications available that you can use as local servers and run your application. Some of them are below.
Wamp, Xamp, Lamp
Hope this will help you

Change this
echo "$rand";
To this
echo $rand;

Firstly you should get knowledge what PHP is and how you can use it.
PHP is a server-side scripting language. So if you try to run it like a html file you will not see the expected output. You need to understand what is a server side server-side scripting is.
If you have have jumped into coding is very essential you should know how you should debug to resolve your error.
One easy way is to enable error reporting.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
?>
Hope this will give you a start .

Related

What program is used to process php files within an html context?

I'm just following w3schools like a normal person
For me getting php to work is harder than learning the language itself...
If somebody could please provide an extensive guide on common problems such as this one that would be heavily resourceful
I'd also like to know the ins and outs of running php scripts within an html context, not having the php extension on the browser would be preferred in most cases
I can't get the following script to run upon submitting the form. It ends up opening up a window asking me which program should be used to run the php file.
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form action="welcome.php" method="get">
Name: <input type="text" name="name"> <br>
E-mail: <input type="text" name="email"> <br>
<input type="submit">
</body>
</form>
Opening the php file using firefox accomplishes the same thing.
If so, which program should I use? or do I need to update some configuration
in order to get this simple script to work
<html>
<body>
Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?>
</body>
</html>
One of the easiest ways to get PHP up and running for a newbie would be to use XAMPP. See this Wikihow article on how to get that done: https://www.wikihow.com/Set-up-a-Personal-Web-Server-with-XAMPP
Good luck!

HTML PHP form post showing up blank?

I have two files, testpage.html and testpage.php in the same folder.
This is testpage.html
<!DOCTYPE HTML>
<html>
<body>
<form action="testpage.php" method="post">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
</body>
</html>
This is testpage.php
<html>
<body>
Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?><br>
</body>
</html>
The form seems to work okay but when I hit submit nothing shows up on the next page. no matter what I enter int he form, it always reads "Welcome" "Your email address is:" with nothing entered after that like its supposed to.
Do I have something configured incorrectly? Am I using the wrong browser (firefox)?
Thanks!
"I am accessing it via file:///C:/... should I be accessing it some other way instead?"
Just as I thought.
There you go. A web browser will not parse PHP directives that way.
It must be accessed as http://localhost/yourfile.xxx
Plus, a webserver and PHP need to be installed in order to parse PHP directives.
"I don't think I have Xampp or any virtual server set up... I'll look into that, any tips on where to start?"
You need to install one. Here are a few links to get you started and depending on the platform you are using.
https://www.apachefriends.org/
http://www.wampserver.com/
https://www.mamp.info
http://www.easyphp.org/
Pick your flavour.

PHP - Submit button does not work

I just started to learn PHP. I use a "Missing Manual" series book. I downloaded PHP 5, I installed it with the option "Do not setup a web server". From command line I can launch a php test program. But I can launch from browser.
The HTML code :
<html>
<head></head>
<body>
<h1>Welcome!</h1>
<form action="scripts/sayHelloWeb.php" method="POST">
<p><i>Enter your name:</i>
<input type="text" name="name" size="20" /></p>
<p><input type="submit" value="Say Hello" /></p>
</form>
</body>
</html>
and sayHelloWeb.php code is :
<html>
<head></head>
<body>
<h1>Hello, <?php echo $_REQUEST['name']; ?></h1>
</form>
</body>
</html>
Well, the HTML works : the text input and the buttons are displayed
, but php does not display any name. That is the "name" variable is empty. It displays only : "Hello, ". The folder scripts exists, the path is correct.
Where I did wrong ?
Thank you.
You have to setup a web server otherwise you can't run php file. if you check the source of sayHelloWeb.php after loading it, you will see that the php code is commented which means that it's not runned.
Make sure to install a web server. Wampserver is a good choise for beginners.
PHP is a Hypertext Preprocessor meaning that its not meant to be be interpreted on the client but rather on the server end, prior to the delivery of the HTML document itself. Installing a PHP interpreter along with a capable web server is required to make PHP work.
If you are seeing PHP code in your browser you can be sure that PHP is not set up correctly with your web server. Installing PHP itself merely installs a local PHP interpreter without setting it up to handle incoming web requests, for that you need a "proper" web server like Apache or IIS.
A good way to test if PHP is working is by creating this document:
<?php
phpinfo();
and loading it into your browser. Upon successful installation of PHP, a detailed (extremely verbose) status of PHP and web server components can be seen.
First check if is submit btn clicket. Than get value from field. Whitout that u will get notice
<?php
if ($_POST['submit']) {
$msg = $_POST['name'];
}
?>
<html>
<head></head>
<body>
<h1>Hello, <?php echo $msg ?></h1>
</form>
</body>
</html>
Try to use isset() and empty() on $_REQUEST
if (isset($_REQUEST['param']))
{
if(empty($_REQUEST['param']))
{
///
}
}
or
if ((isset($_REQUEST['submit'])) && (!empty($_REQUEST['name'])))
{
$name= $_REQUEST['name'];
}
<html>
<body>
<h1>Hello, <?php echo $_POST['name']; ?></h1>
</body>
</html>
Should work?
These fields are stored in $_POST array.
Try this: echo $_POST['name'];

form method post using php

I'm trying to make a simple form that echo's the answer with php to another page.
I have an html sheet:
<!DOCTYPE html>
<html>
<body>
<form action="gegevens.php" method="post">
First name: <input type="text" name="firstname"><br>
Last name: <input type="text" name="lastname"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
and a php sheet
<!DOCTYPE html>
<html>
<body>
<?php
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
echo "Firstname: ". $firstname . ".<br />";
echo "Lastname : ". $lastname. ;
?>
</body>
</html>
Unfortunately this doesn't work when I run it in a browser.
Can anyone help me with this?
It cannot work in a mere browser. You need to run it with a webserver such as: XAMPP
PHP (PHP: Hypertext Preprocessor) is a scripting language not a markup language. Opposite to html where you write static xml pages with php you are writing scripts that produce output. Therefore it needs an interpreter to process that script.
See http://php.net/
As #dot-sp0t stated, you need to have some time of webserver running behind the PHP code. The web browser does not have PHP webserver built into it.
Suggestions (Thanks to #dot-sp0t for this one): XAMPP
PHP Built in Webserver
And there are much more.
PHP is an server side scripting language.Browser cant run php scripts
you can run it with a webserver such as: XAMPP
or test it online on available sites like this

Very basic php/html form

I'm very new to php/html, and I'm trying to teach myself the basics of creating and processing an html form.
I created a folder called Website. In it I created an html file index.html. I also created a file submit.php.
(this code is taken from: http://www.w3schools.com/php/php_forms.asp)
In index.html I have:
<html>
<body>
<form action="submit.php" method="post">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
</body>
</html>
In submit.php I have:
<html>
<body>
Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?>
</body>
</html>
When I open the html file in chrome and fill in the blanks and press submit, I get redirected to a page with the code in submit.php:
<html>
<body>
Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?>
</body>
</html>
I should be getting this output:
Welcome Hannah
Your email address is Hannah#example.com
What am I doing wrong that the output isn't working?
Thanks!
PHP is processed on a server, so you can't treat it like HTML, it needs to be placed on a server that has apache installed. You can install one on your computer, using something like the following:
WAMP
LAMP
MAMP
XAMPP
Sounds like you are doing this via files on your desktop and not via a web server. Either on the Internet or via your local machine. I took your example 100% as presented, placed it within my htdocs folder in MAMP (LAMP for the Macintosh) and it behaves 100% as expected.
The difference between loading files on your desktop versus a web server is a web server will process the PHP. If you just do it as files, then the file gets loaded & doesn’t get parsed.

Categories