Running php script from HTML file WAMP - php

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

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.

HTML Form post request results in 502 bad gateway when calling php file

I have two files:
Homepage.html:
<html lang="en">
<script type="text/javascript" src="app.js"></script>
<head>
<meta charset="UTF-8">
<title>Home</title>
</head>
<link rel = "stylesheet"
type = "text/css"
href = "style.css" />
<form action="zipTest.php" method="post" accept-charset="utf-8">
<input placeholder="Enter your zip code to start" class = "zipInput">
</form>
</html>
and
zipTest.php:
<?php
print("Post successful");
?>
I should see "Post successful" in the console with this code right? But I am getting a 502 Bad Gateway error after submitting the form. What is wrong?
You don't need additional HTML tags or javascript for this to work.
If you dont do anything weird in your app.js this should work by pressing enter in the input.
The text won't bevisible in the console but rather in the browser page.
Check this:
Are both Homepage.html and zipTest.php in the same directory? Lets say Homepage.html is in the webroot then the url for this page is: http://localhost/Homepage.html. If you press enter in the input it should change url to: http://localhost/zipTest.php. This doesn't have to be via POST. Just go to this url in the browser and it should output the text you defined. If it doesn't the url is wrong, for example both files are in different directories.
does your server even support php files? Go to location of the php file directly in your browser and find out. Do other php files work?

simple php code not been executed

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

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.

localhost/test.php returns nothing

I am following a tutorial "PHP & MySQL for dummies" to build a web application. I created a simple test.php file in web space (/var/www/html). The problem is that when I type localhost/test.php in the browser address window, it returns me an empty page. I tried localhost/php.info and it worked well but I could not find why test.php does not work.
This is the test.php code:
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<p>This is an HTML line
<p>
<?php
echo “This is a PHP line”;
phpinfo();
?>
</body></html>
I do appreciate any information.
Thanks.
You said that you put your script in /var/www/html
So the link might be
localhost/html/test.php
Also check the error log file for PHP error.
# cat /var/log/apache2/error.log
EDIT: Change your quote, ” are for Word document and not programming ;-)
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<p>This is an HTML line</p>
<?php
echo "This is a PHP line";
phpinfo();
?>
</body>
</html>
I have tried with this..just type the word localhost followed with name of the work eg.. localhost/test.php in the navigate bar on your browser then click enter

Categories