# PHP form ....



## Da_iMac_Daddy (Jan 2, 2003)

Can someone give me and example of how I would create a page that would first ask for a password and user name then send me to a page where I would be able to fill in a text box then have that saved into another text file and automatically add the date to the top. Then I need to plop that sucker into another html Page and format it. 

OK well you probably won't give me and example or write it for me but could someone lead me to some info that would help me to do it myself?


----------



## Da_iMac_Daddy (Jan 2, 2003)

OK let me bust this up a little ... I have figured out how to write to a text file. now what I want to do is be able to have the text I write from the file come from a field which I can fill out in my browser and hit a button like submit or something and have it send it to the variable $string


```
<?php
$filename ="abc.txt";
$handle= fopen($filename,'a');
$string="Hello World";
fputs($handle, $string);
fclose($handle);
?>
```

:edit:

just uploaded the page I would like to use to deal with the php code it's  here 
now how do I go about linking it to php to edit a text file?


----------



## twister (Jan 2, 2003)

Just make a regular form with dreamweaver or whatever you like.  Then the comments field would be called 'comments'

then have it go to input.php when they hit submit.  that page would have this code


```
<?php
$filename ="abc.txt";
$handle= fopen($filename,'a');
$string=$comments;
fputs($handle, $string);
fclose($handle);
header("Location:[url]http://yoursite.com/apage.htm[/url]");
exit();
?>
```

You might need another piece of code.  $_POST but i can't figure out how that goes right now.  something like

$string=$POST_['comments'];

Hope i didn't just confuse you.

Twister

PHP.net for all kinds of PHP stuff


----------



## Da_iMac_Daddy (Jan 2, 2003)

I thought it was $string = $_POST['comments'];

OK now I've got that all set up but when it tries to edit the file I get some errors 


```
Warning: fopen("news.txt", "a") - Permission denied in /home/dekster/www/www/process.php on line 6

Warning: fputs(): supplied argument is not a valid File-Handle resource in /home/dekster/www/www/process.php on line 6

Warning: fclose(): supplied argument is not a valid File-Handle resource in /home/dekster/www/www/process.php on line 6
```


----------



## Da_iMac_Daddy (Jan 2, 2003)

This is the code for the process.php
I tried changing the permissions on news.txt and that doesn't seem to change anything 
news.txt is in /www ... ooops changed the code and now it just shows a blank page when I hit submit .. gotta go check and see if it worked :\


```
<?php
$filename = 'news.txt';
$comment = $_POST['textarea'];
$handle = fopen("ftp://nathacof:*******@ftp.dekster.com/www/news.txt", 'r+');
fputs($handle, $comment);
fclose($handle);
exit();
?>
```


----------



## Da_iMac_Daddy (Jan 2, 2003)

hmm it didn't write anything to news.txt ... weird ... :\ ACK! I'm excited I think I'm actually getting close to getting this


----------



## twister (Jan 2, 2003)

get info on the text file.  you have to make sure there are read and write privledges on it.

Twister


----------



## Da_iMac_Daddy (Jan 2, 2003)

already did that :\


----------



## twister (Jan 2, 2003)

sorry i didn't read your code

this:  $handle = fopen("ftp://nathacof:*******@ftp.dekster.com/www/news.txt", 'r+');

should be: $handle = fopen("http://yourwebsite.com/news.txt", 'r+');

Try that


----------



## twister (Jan 2, 2003)

this is what mine looks like.  See if you can get it to work for you. 


```
<?
    global $linkAddress, $category, $new_category, $linkDescription;

 $flat_file  =  "TMCLinks.txt";
 $links_url  =  "http://127.0.0.1/~twister/index.php";
 $dups_url  =  "http://127.0.0.1/~twister/index2.php";


        $links_file =  fopen ( $flat_file, "a+" );
     if ( $linkDescription ) {
      $new_link =  $category . "\t" . $linkAddress . "\t" . $linkDescription . "\n";
     }
     else {
      $new_link =  $category . "\t" . $linkAddress . "\n";
     }
        fwrite( $links_file, $new_link );
        fclose( $links_file );    }

 header( "Location: $links_url" );
?>
```


----------



## Da_iMac_Daddy (Jan 2, 2003)

changing it to fopen("http://dekster.com/news.txt ", "r+"); didn't work ... 

I have no idea whats going on ... the page is www.dekster.com/test.html, I dunno I use ftp to deal with all my site stuff so I figured I would use ftp to change stuff. This is tough.


----------



## twister (Jan 2, 2003)

i didn't get an error. Did you get my test?

If you want send me the php file also and i'll look at it.

twister@twistermc.com


----------



## Da_iMac_Daddy (Jan 2, 2003)

The file news.txt hasn't changed 

also before I had the PHP file embedded into html  Now I've taken it out ... does it matter either way?


----------



## twister (Jan 2, 2003)

no it shouldn't matter.  Can i see your php file?

Twister


----------



## Da_iMac_Daddy (Jan 3, 2003)

OK now I have it so that what is entered will be written to news.txt but it also writes over anything in it's way. I used the r+ flag to start at the beginning of the file but that's no use to me if it just writes over everything anyway. Any help here?


----------



## Da_iMac_Daddy (Jan 3, 2003)

I tried to add 

```
header("Location: [url]http://www.dekster.com/[/url]");
exit;
```

to my process.php script but it gives me this error


```
Warning: Cannot add header information -
 headers already sent by (output started at 
/home/dekster/www/www/process.php:10) in 
/home/dekster/www/www/process.php on line 11
```


----------



## slur (Jan 4, 2003)

Yes indeed, you can't print any content - including whitespace - before a header(). Make sure there's nothing before the first <? in your script.

This is documented quite clearly.


----------



## Da_iMac_Daddy (Jan 5, 2003)

I got it


----------

