Okay so I am total noob in php since I recently started watching videos on php. So what I am trying to do is I have this html file:
<!DOCTYPE html>
<html>
<head>
<title>Testing PHP</title>
</head>
<body>
<form action="action.php" method="POST">
<input type="text" name="first">
<input type="text" name="second">
<input type="submit" value="Submitto!">
</form>
</body>
</html>
and then I have my action.php file which has this code:
<!DOCTYPE html>
<html>
<head>
<title>Testing PHP</title>
</head>
<body>
<?php
$first = $_POST['first'];
$second = $_POST['second'];
echo $first + $second;
?>
</body>
</html>
However when I click on the submit button in my html file it sends me to a blank page. By the url I see that it has sent me to the right file but seems like the code doesn't execute.
PS: I tried searching on google but what I found was mostly stuff about apache not executing php which doesn't work for me since I try to run the files locally on my machine.
For Blank Page Problem:
First you have to install a local server on your machine like apache then you have to request this page from the local server to run PHP locally on your machine.
Here is the download page.
For String concatenation:
In php string concatenation operator is . sign not the + sign,so you have to use . operator instead of + operator.
echo $first . $second;
action.php
<!DOCTYPE html>
<html>
<head>
<title>Testing PHP</title>
</head>
<body>
<?php
$first = $_POST['first'];
$second = $_POST['second'];
echo $first . $second;
?>
</body>
</html>
For more knowledge about String Operators read this
Your code works (I just actually checked it), but you still need a web server, even if you run your code locally.
Recent versions of php include the built-in webserver, so first try this (in the folder where your html and php files are placed):
php -S localhost:8888
Then open http://localhost:8888/your.html in the browser.
If your php is too old (older than 5.4.0), you will need to setup apache or ngnix to be able to run you php code.
Related
In firefox I repeatedly tried to get PHP working, but no luck
My code is as follows:
<!DOCTYPE html>
<html>
<body>
<h1>My first PHP page</h1>
<?php
echo "Hello World!";
?>
</body>
</html>
I used PHP format, but it wanted to save the script again.
XML threw a boatload of errors.
HTML did not show the PHP
I tried to host a simple website on my Mac. My goal is to make a form that everyone can enter text in, then the piece of text will be shown in the main HTML page. I encountered two problems:
I could see updates in mylog.log when I run php submit.php, but I can't when using a browser (Safari) and visiting the site itself.
Even if I managed to write to the file, I don't know how to make the HTML interact with it to display the contents.
This is the code in index.html:
<form action="submit.php" method="post" target="_self">
<input type="text" id="textform2" name="msg">
<input type="submit">
</form>
And submit.php:
<!DOCTYPE html>
<html>
<head>
<title>Processing</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<?php
$message = 'hi';
$file = 'mylog.log';
$handle = fopen($file, 'w');
fwrite($handle, $message);
fclose($handle);
?>
<p class="par">Our server is processing your request. You will be redirected…</p>
<script> location.replace("http://johann.local"); </script>
</body>
</html>
I put these files in Macintosh/Library/WebServer/Documents.
Sanitisation is not required since it will only be hosted on my private WiFi network.
Firstly, you need to run PHP Web Server to make your submit.php working.
Go to your directory with index.html and submit.php, and run this:
php -S localhost:5000
Then your website is available at http://localhost:5000 .
About your second question, if you want easily write into the file, you can use file_put_contents function instead of fopen/fwrite/fclose, and vice versa, you can easily read the content via file_get_contents.
This is example of usage:
<!DOCTYPE html>
<html>
<head>
<title>Processing</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<?php
$message = 'hi';
$file = 'mylog.log';
file_put_contents($file, $message); // save $message into $file
?>
<p class="par">Our server is processing your request. You will be redirected…</p>
<p>Contents of the log: <?php echo file_get_contents($file); ?></p>
</body>
</html>
(I removed the redirection to see the content)
With echo function combined with file_get_contents, you can print the contents of the log file into the HTML.
I am trying to create a simple php page which contain only an echo, if I saved the page as ".html" it will be opened on google chrome but nothing is executed, if ".php" the page never open. any help? PS: I am new on programing
<html>
<head>
<title> test</title>
</head>
<body>
echo "hello World";
</body>
</html>
You have to write your PHP code between PHP open <?php and close ?> tags.
Try this :
<html>
<head>
<title> test</title>
</head>
<body>
<?php echo "hello World"; ?>
</body>
</html>
Php file don't run .html format and without server . Please read and understand what is php and how it is work ?. follow below link or find youtube tutorial for more visual understanding .
https://www.w3schools.com/php/default.asp
http://php.net/manual/en/
Maybe you try this :
<html>
<head>
<title> test</title>
</head>
<body>
<?php echo "hello World"; ?>
</body>
</html>
Save file as .php format and run through a localhost server that you prefer . if you don't know what is server is
follow link :
https://www.w3schools.com/php/php_install.asp
PHP server on local machine?
Download XAMPP, change your code to
<html>
<head>
<title> test</title>
</head>
<body>
<?php echo "hello World"; ?>
</body>
</html>
Locate the folder C:/xampp/htdocs, and create the file index.php containing your code, now type "localhost" in your web browser and you're done
So I have been following this tutorial here
I have installed WAMP and the server is up and running just fine. (As the green light in the taskbar indicates).
In the www folder I have the following 2 files:
<!DOCTYPE html>
<html>
<head>
<title>First Form</title>
</head>
<body>
<form action="postForm.php" method="post">
<TextArea
name="microBlog" id="microBlog" cols="30" rows=“10">
</TextArea>
<input type="submit">
</form>
</body>
</html>
and
<!DOCTYPE html>
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php
$microBlog = $_POST['microBlog'];
echo $microBlog;
?>
</body>
</html>
However, when I run the html file and input "hello" into the field, clicking the submit button returns a blank screen.
Here is the html screen on open:
and on clicking submit the following is in the address bar:
file:///C:/wamp/www/postForm.php
Any ideas why the blank screen?
Does the PHP (postForm) file require HTML tags or can I get rid of them?
Open the file via http://localhost/...
The reason for the blank screen is:
You can open a html-file with the explorer and it will work fine because it will be rendered by the default-web-browser. you need a parser for php-scripts. The Webserver will use this parser if you add the extension .php but if you open the file in the Explorer you don't use the Webserver (in your case apache). If you want to open the file in the explorer set the action-attribute to action="http://localhost/postForm.php" and it will work.
You should use
action="http://localhost/postForm.php"
instead of
action="postForm.php"
hope this help :)
I'm trying to embed some php code in a .html file but it doesn't seem to work. I read that in order for this to work the file has to be saved with the .php extension, but when I do that and open it up in a browser the browser just displays all my code. What am I doing wrong?
<!DOCTYPE HTML>
<html>
<head>
<title>Welcome</title>
<link rel="stylesheet" href="normalize.css">
<link rel="stylesheet" href="style.css">
</head>
<body>
<?php
$x = 3;
$y = 2;
$ans = $x + $y;
?>
<h1 style = "text-align: center;"> The answer is: <?php= $ans ?> </h1>
<section class="loginform cf">
<form name="start page" action="index_submit" method="get" accept-charset="utf-8">
<label style = "text-align: center;" >What type of property?</label>
<input type="button" value = "Commercial" onClick="parent.location='commercial.html'">
<input type="button" value = "Residential" onClick="parent.location='residential.html'">
</form>
</section>
</body>
</html>
Your browser doesn't understand PHP. What you need to do is upload your file to a web server that knows what to do with it. Most commercial web hosts are set up this way. Alternatively, you can set up a server on your own computer. If you search the web for LAMP + PHP (or perhaps WAMP if you're using Windows), you should find instructions on what to do next.
In the server, files with names ending in .php are handled by a PHP server module, which looks for code between the <?php and ?> tags and executes it before sending the results on to the browser that requested the page.
Your php code will not work with .html; Change file extension to .php.
You should run your php code from the server. You can download AppServ or WAMP