-3

I know this is a relatively basic question, but I cannot seem to find a complete answer anywhere. How may I submit an HTML form to a flat .txt file without changing the content of the browser, besides a reload. Here is the content of my form inside my HTML file:

<form name="input" action="form.php" method="post">
Zip: <input type="text" name="zip">
<input type="submit" value="Submit">
</form>

and here is my php:

<?php

$myFile = "data.txt";

$fh = fopen($myFile, 'a');

$zip = $_POST["zip"];

$comma_delmited_list = explode(",", $zip) . "\n";

fwrite($fh, $comma_delmited_list);

fclose($fh);

?>

where form.php, data.txt, and the .html file are in the same directory, any help is appreciated. Thanks.

Deprecated
  • 163
  • 1
  • 3
  • 14

1 Answers1

2

If you are seeing PHP code in your browser, then the server (if there is one) is not set up to handle PHP content. Did you install PHP? What is the environment you are using? Apache? nginx? Go Daddy, or some other host?

To do what you actually want (once your environment works properly)...

You need to use AJAX to do what you're asking. Go read about jQuery or some other JavaScript library, or even just use basic JavaScript. It's not that hard to do. The PHP file would run in the background and create your file, while your page stared blankly at you, unless you do something to indicate that the file was created.

Ian Atkin
  • 6,302
  • 2
  • 17
  • 24
  • It's just a local Apache Tomcat, but I will be using GoDaddy, and in which case, i'll just need to make the php executable, right? I'm familiar with JQuery and Javascript, i'll look into what I should do. – Deprecated Dec 11 '12 at 03:58
  • PHP works with Go Daddy out of the box as long as it's the right kind of instance. If you have to use Go Daddy, go with a Linux server, not Windows. Do you have PHP installed and set up to work with Tomcat? See http://stackoverflow.com/questions/779246/run-a-php-app-using-tomcat to find out how to do it. – Ian Atkin Dec 11 '12 at 04:02