Problem:
I have created a simple web server with apache hosted by google cloud services and I am having a hard time figuring out how to get my PHP code to execute when a form has been submitted.
Objective:
I want the user to be able to input some information into a form and have it sent to the server. From there the server will run some PHP code and write the information to a file in the same directory.
How it works now:
When I submit the form instead of executing the action it just opens up the file as a text document in the browser.
Form from Main.html
<form action="processForm.php" method="POST">
Spotify URI: <input type="text" name="uri">
<br/>
Description: <input type="text" name="description">
<br/>
<input type="submit">
</form>
processForm.php
<?php
if(isset($_POST['uri']) && isset($_POST['description'])) {
$data = $_POST['uri'] . '-' . $_POST['description'] . "
";
$ret = file_put_contents('data.txt', $data, FILE_APPEND | LOCK_EX);
if($ret === false) {
die('There was an error writing this file');
}
else {
echo "$ret bytes written to file";
}
}
else {
die('no post data to process');
}
I don't believe it is an issue with PHP being installed improperly. When I run php -v
it echoes
PHP 5.6.29-0+deb8u1 (cli) (built: Dec 13 2016 16:02:08)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies