-1

How to save contact form's messages in server? I used PHP to save messages, but it doesn't work and got an "HTTP ERROR 405" error.

My HTML code:

<body>
<form action="contact.php" method="POST">
    <p>Name</p><input type="text" name="name">
    <p>Email</p><input type="text" name="email">
    <p>Message</p><textarea name="message" rows="6" cols="25"></textarea><br/>
    <input type="submit" value="Send"><input type="reset" value="Clear">
</form>
</body>

My PHP Code

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$myfile = fopen("textfile.txt", "a") or die("Unable to open file!");
fwrite($myfile, $name);
fwrite($myfile, $email);
fwrite($myfile, $message);
fclose($myfile);
?>

Or is there an easier way to save messages in server?

Sunchi
  • 85
  • 1
  • 9

2 Answers2

0

405 Method Not Allowed is shown because POST is not supported by your server. Try GET method

<body>
<form action="contact.php" method="GET">
    <p>Name</p><input type="text" name="name">
    <p>Email</p><input type="text" name="email">
    <p>Messages</p><textarea name="messages" rows="6" cols="25"></textarea><br/>
    <input type="submit" value="Send"><input type="reset" value="Clear">
</form>
</body>

php

<?php
$name = $_GET['name'];
$email = $_GET['email'];
$message = $_GET['message'];
$myfile = fopen("messages.txt", "a") or die("Unable to open file!");
fwrite($myfile, $name);
fwrite($myfile, $email);
fwrite($myfile, $message);
fclose($myfile);
?>
anoopknr
  • 3,177
  • 2
  • 23
  • 33
0

The issue was solved after the following process.

  1. Re-install LAMP (instruction video)
  2. Modified the apache2.conf file (instruction)
    set AllowOverride of the directory /var/www
  3. Change the permission for /var/www/html folder (instruction1, instruction2)
  4. Put your html and php files in the /var/www/html folder

Finally, go to http://localhost/contact.html, the contact form can save messages in a text file.

Sunchi
  • 85
  • 1
  • 9