embeded php code in html5 - php

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

Related

Mac PHP can't write to .log file

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.

Does my file upload form work in PWA, and if not, how can I implement it?

I am trying to develop a PWA, and one of the main features is to be able to upload files to my server. Below is the code I am using which does work in a laptop setting (i.e. opening in Chrome on a laptop):
<!doctype html>
<html lang="en">
<head>
<link rel="stylesheet" href="./css/style.css">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body class="fullscreen">
<div class="container">
<form enctype="multipart/form-data" action="upload.php" method="POST">
<p>Upload your file</p>
<input type="file" name="uploaded_file"></input>
<input id="submit" type="submit" value="Upload"></input>
</form>
</div>
</body>
</html>
<?PHP
if(!empty($_FILES['uploaded_file']))
{
$path = "uploads/test/";
$path = $path . basename($_FILES['uploaded_file']['name']);
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $path)) {
return;
} else{
echo "There was an error uploading the file, please try again!";
}
}
?>
The above code, however, when I use it on my phone as the PWA, does not load at all. It gives me the option to upload, and I am able to select files, but once I press submit, the page just begins to load, but never completes loading/uploading the file to my server.
I am assuming it is an issue with using PHP code in the PWA,because the HTML works just fine on the PWA. I've tried looking for file upload solutions in Javascript, but they still use a bit of PHP.
If you want to test out the file upload form on my site in action, click Here. Click the next link for the root page to install the PWA on mobile to test Here.
To sum up, I need to make a file upload form work on the PWA. Any help in the right direction is much appreciated. Thanks!

Display contents of a file on django website

Im trying to display the contents of the text file with no luck. I have tried php which looked like an easy way to do it with no luck
base.html
{% load i18n static %}
<!DOCTYPE html>
<html>
{% load static %}
<head>
<head lang=en>
<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>
</head>
<body>
<div >
<div class="jumbotron" style="background-color: white; padding: 1.5rem;margin-bottom:i 100.5rem">
<div class="row">
<div class="col-md-3"><img src="{% static 'css/images/hadoop.png' %}"</div>
<div class="col-md-6">
<h2>Impala Query Metrics</h2>
<hr class="my-2" href="{% url 'impala' %}">
</div>
<?php
error_reporting(E_ALL); ini_set('display_errors', 1);
$f = fopen("pathtofile/test.txt", "r");
// Read line from the text file and write the contents to the client
echo fgets($f);
fclose($f);
?>
I have also tried
<?php
error_reporting(E_ALL); ini_set('display_errors', 1);
readfile("pathtofile/test.txt");
?>
Also:
<?php
include("pathtofile/test.txt");
?>
Im not getting any errors nor seeing any results
There are a few issues here; hopefully this provides some thoughts on where to go.
First, it appears that you want to run PHP code inside an HTML file, as the name of the file is base.html. The PHP interpreter is not configured (by default) to allow this, as it will only run files with the php extension. The fix for this would be to tell your web server to associate html files with the application/x-httpd-php mime type, so html files are ran through the php interpreter.
Second, I noticed the question is tagged with django, which is a Python framework. Do you have a PHP interpreter installed on the server? In order to execute PHP code, you'll need a PHP interpreter, which Python is not. To fix this, you would need to install a PHP interpreter (https://secure.php.net/ - right sidebar)
Third, should you really do this? Python and PHP both serve the same need, to provide server side processing to a web site. I would argue that you should use one or the other, but not both. Since you're already running Django, I would advise that you look for the Python-esque way of accomplishing a file include.
assuming your file is text-file and you want the a html file that your data wrapped inside a div or so then :
I'm not a python fan but for python some links , not exactly same title but may good for beginning:
https://python-forum.io/Thread-read-text-file-using-python-and-display-its-output-to-html-using-django
Django displaying upload file content
https://djangobook.com/generating-non-html-content/
or in php: 0.php
<!DOCTYPE html>
<html>
<body>
<div class="content-wrapper">
<?php
echo file_get_contents( "YOURFILE" ); // get the contents, and echo it out.
?>
</div>
</body>
</html>

PHP Code Doesn't Execute (new to php)

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.

Running php script from HTML file WAMP

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 :)

Categories