01-22-2004, 03:07 AM
It's hard form me to admit failure but it seems as I get older I must be getting dumber.
For some reason I am not able to connect the dots with this dynamic HTML / PHP stuff.
Once the light bulb goes off, and I slap myself because it was so obvious at what I was missing, I'll be able to go on and do some really cool stuff.
I thought I was on the right track. I created a very simple HTML doc that has three text fields and a submit button using the POST method.
I created a simple .php file that supposed take the three variables and write them to a text file. When I test it the next thing you know it wants to know if I want to download and save the php file. I've seen pages w/submit buttons before that don't work anything like this. Nope! I don't want to download anything! Clearly I don't understand what the *&$&*$* I'm doing or or how this #**$^& enviroment works with respect to GET, POST, submit, etc. Ack!
Could someone please point me to some decent documentation that is in a clear, consise, step by step well planned out format that is not just a bunch of code snippets but actually has examples of a complete HTML file and a complete php file with some explanation about what is transpiring with the communication between the files. I give many thanks in advance.
Eventually, I would like to use this in conjuntion within my Flash animation stuff be able to tie databases and other things using PHP.
01-22-2004, 03:46 AM
Weyall....here's some code:
<title>This is a test</title>
<form action="process_form.php" method="POST">
Name: <input type="text" name="username" /><br/>
Age: <input type="text" name="age" /><br/>
Gender: <input type="text" name="gender" /><br/>
<input type="Submit" value="Submit it!" />
and the php to process it:
/* This file takes 3 values from a form, and writes them to a text file */
/* here we take the values in the "$_POST" array (which contains the values from the form), and place them into easier to read variables (for demo purposes) */
$username = $_POST["username"];
$age = $_POST["age"];
$gender = $_POST["gender"];
/* Now we want to write those values to a text file. */
$my_file = "form_results.txt";
/* w+ means open the file for writing, and if it doesn't exist, create it. */
$file_pointer = fopen($my_file,"w+");
/* write to the file. the \r\n include a newline at the end, and the 1024 is the number of bytes to write. (use this number by default) */
fputs($file_pointer, "Username: " . $username . "\r\n",1024);
fputs($file_pointer, "Age: " . $age . "\r\n",1024);
fputs($file_pointer, "Gender: " . $gender . "\r\n",1024);
/* close the file_pointer */
print "Congrats....we should be done here!";
I didn't test that, but it should be right. The fact that you got a prompt to download the file, seems to mean that the server that hosts your website doesn't support php. Usually, when a server isn't specifically told (in a configuration file) to process a file type (like .php or .php3 or .asp), then it doesn't process it on the server, it just spits it out for download.
Are you using your own webserver (like, in your house), or is it one somewhere else?
To put it simply:
if you had a url like http://www.someserver.com/file.php?username=bob&age=21&gender=female, then php would use the $_GET array to store those values. (GET values are stored in name/value pairs IN THE URL).
$_POST is used when the "post" method is used..think of this is....just not going through the url, but via some "underground railroad." ;)
Again, understanding the concepts isn't terribly important at this point. I think your problem is that your server isn't configured correctly.
01-22-2004, 03:49 AM
I doubt you're getting dumber probably just more set in your ways which is a curse making everything harder to learn.
If your browser wants you to download it perhaps the issue is of php not being set up properly on your server, not your actual code. You can't just write a php file and expect it to run directly off your computer, you have to put it thorugh the php interpreter first.
If you want to test on your computer rather than the hosting server you need to set it up that way. phpdev mentioned in the annoucements of this forum can do that if you want. You could also set it all up yourself but that's quite a pain for a newbie which explains why things like phpdev are so popular.
If neither of the above appeals you're going to have to upload it to a php enanbled host's server and view on the net.
01-22-2004, 07:26 AM
Thanks everyone for the reponse. Celtboy, I'm going to try the files that you posted to see what happens.
I'm running an XP box with Apache installed if I go to http://localhost/ the Apache test page comes up. From my understaing at least what is said that if you see this apache is running correctly.
I have (i think) the most current stable version of PHP installed. If I create a simple php file called tstPHP.php that has the following code:
echo 'Hello World';
in the browser if I type http://localhost/tstPHP.php i see the following formatted pretty fancy I might add...:
PHP Version 4.3.4
System Windows NT MAGIC777 5.1 build 2600
Build Date Nov 2 2003 23:43:42
Server API Apache 2.0 Handler
Virtual Directory Support enabled
Configuration File (php.ini) Path C:\Apache2\php.ini
PHP API 20020918
PHP Extension 20020429
Zend Extension 20021010
Debug Build no Thread Safety enabled Registered PHP Streams php, http, ftp, compress.zlib
and on and on and on and on for many more pages.
I also tested MySQL by writing a small database creation and retrieval program. Everything worked at least to the specifications that were put out in the installation process.
I made the assumption that it was working. I have created other .php files that did some stuff. this is the first time I tried to pass variables using HTML.
I am going to list three files:
FILE #1) is a .php called ztest.php it is a file that updates a counter and this file works just fine every time I refresh the page it updates and displays the new number.
FILE #2) HTML File I was using as my first attempt at passing vars using POST.
FILE #3) The PHP file that was supposed to process the vars passed by the previous file.
I hope that clears up some ambiguity about my system configuration.
I am always open to suggestions about my system configuration if I don't have something correct it needs to be changed.
File #1) (THIS WORKS FINE...)
$CounterFile = 'Counter.txt';
ignore_user_abort(true); ## kill ability to abort and trash the file
$fd = fopen($CounterFile, 'r+');
if (flock($fd, LOCK_EX))
$curCount = chop(fread($fd, filesize($CounterFile)));
$fd = fopen($CounterFile, 'w+');
ignore_user_abort(false); ## back to normal
echo 'Current count is ',$curCount;
File #2) HTML file: (This works OK it's when I hit the submit button that things mess up)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<form action="mail.php" method="post">
Your Name: <input type="text" name="name"><br>
E-mail: <input type="text" name = "email"><br><br>
<input type="submit" value="Submit">
FILE #3 the PHP file
echo "HERE IN THE MAIL PROGRAM\n";
$outFile = "test.txt";
echo $name, $email, $comments, $outFile;
$message="name = $name \n email = $email \n comments = $comments \n outFile = $outFile\n ";
$fd = fopen($outFile, 'a+');
01-22-2004, 07:54 AM
Well, everyone should get at least a small chuckle out this one.
As I said I'm new to this environment I'm still cobbling together my development enviroment. Lots of unknowns. So far I've come up with using PHPEdit vo.8.0.25 for php and a host of other editors for HTML one in particular that I just found and installed ins HTML-Kit. HTHL-Kit has a preview and that is what I have been using to test the latest POST stuff.
I had an epiphany. I tested what I was trying to do in my browser and guess what? It worked. DOH!
Thanks for all of your help!
01-22-2004, 05:00 PM
Hehe. I like simple solutions like that.
Don't feel bad, it happens to the best of us. (note that I count myself among the best. :D :thumbsup: )