2

I'm using a PHP form as part of my website for people to get in touch. The form will post the results to another webpage. At the moment, for testing it is just plain but it will be password protected. It doesn't have to be secure however, this isn't a commercial thing, just a project. Form code:

<form action="contactform.php" method="post">
<p><label>Name: <input name="name" placeholder="eg: John Doe"></label></p>
<p><label>Subject: <input name="subject" required placeholder="eg: App Updates?"></label></p>
<p><label>Content: <input name="content" required placeholder="eg: When will the next update to your app be released?"></label></p>
<p><label>Email Address: <input name="email" required placeholder="eg: withheld@xxxx.com" type="email"></label></p>
<p><button input type="submit">Submit form</button></p>
</form>

PHP code:

    <?php

$name = htmlspecialchars($_POST['name']);
$email = htmlspecialchars($_POST['email']);
$subject  = htmlspecialchars($_POST['subject']);
$content = htmlspecialchars($_POST['content']);
echo  $name, $email, $subject, $content;
$text = "NAME: $name <br>
 EMAIL: $email<br>
 SUBJECT: $subject<br>  
 CONTENT: $content<br><br><br>";
$file = fopen("formresults.html","a+");
fwrite($file, $text);
fclose($file);


?>

I don't know why it isn't just posting, whenever I test it in my browser it just downloads the PHP page

reuben
  • 21
  • 1
  • This is your first php code? Your http server is configured to process php files? In the browser, you load the script via 'file://' or via 'http://' syntax? – fusion3k Mar 13 '16 at 00:35

1 Answers1

1

Your code should work. It sounds like you're trying to run a PHP file in your browser with an incorrectly configured (or non-existent) server. Are you running server software such as Apache or NGINX?

PHP is an interpreted language, and to view pages written in PHP in a browser you need to set up server software which will intercept requests for PHP files and run them through the PHP interpreter. If you're developing on a Windows machine, you can use software like WAMP or XAMPP to make the process easier and install PHP, Apache and commonly used tools such as a database engine.

Dan Abrey
  • 696
  • 5
  • 9
  • If you are running Apache, ensure that you have libapache2-mod-php5 installed. – Jake Psimos Mar 13 '16 at 01:00
  • I'm just running it locally at the moment, as in off the hard drive. Would that explain it? I've bought a server for hosting but still waiting for the DNS to propagate. – reuben Mar 13 '16 at 01:19
  • It would - you still need to install PHP and configure a server locally (or one of the packages I mentioned) to be able to load PHP webpages in your browser. – Dan Abrey Mar 13 '16 at 01:22
  • Got it working. Installed apache2 and PHP5. However, it's not writing the results to the file? – reuben Mar 13 '16 at 14:12
  • Do you see any errors, or are there any errors in the Apache log file? You may need to [turn on PHP's error display](http://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php) to see some useful errors. It could be that you (or the server) don't have permissions to write to the file, or that the file isn't being found. – Dan Abrey Mar 13 '16 at 14:50
  • Permissions needed changing. Cheers mate. Now got to figure out why the thing isn't hosting right. – reuben Mar 14 '16 at 12:15
  • Great! If you could mark the answer I'd appreciate it :) – Dan Abrey Mar 14 '16 at 16:38