form method post using php - 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

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!

PHP variables not showing up after submitting form (closed)

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 .

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'];

passing data from html to php

dear all
I am a beginer in PHP world (PHP 5.3.5) my web server is IIS fastCGI on win xp
I tried to pass values from the HTML form to the php but the data are not passed
this is my html file
<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>
and this is my php file welcome.php
<html>
<body>
Welcome <?php echo $_POST["fname"]; ?>!<br />
You are <?php echo $_POST["age"]; ?> years old.
</body>
</html>
After pressing submit the output was like this
Welcome!
You are years old.
but it should be like this
Welcome John!
You are 28 years old.
Can you please help me with this.
on IIS change in the form: method="post" to method="POST" <-- UPPERCASE should solve your issue.
Anyway what about the apache? and PHP version around just 5, flat five :)
5.3.5 on IIS for the beginner? sounds like to get the Mount Everest by night without gloves
That is pretty standard simple code, and looks correct. One thing you might look at is the way PHP is configured on your server. Also, try using $_REQUEST in place of $_POST on your welcome.php page and see if it still does the same thing.
Try naming your form as well
After looking at your code it seems that it must be a configuration issue.
Steps to resolve this:
Firstly is the code that spits out the page actually welcome.php
Secondly add the following to your php block
var_dump($_POST);
Click submit and if this still shows
array (
)
if so then do
var_dump($_REQUEST);
and post the contents to your post, and show us.

Categories