Current location: Hot Scripts Forums » Programming Languages » PHP » write to file


write to file

Reply
  #1 (permalink)  
Old 01-11-04, 10:22 PM
tiny tiny is offline
Newbie Coder
 
Join Date: Jan 2004
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
write to file

I am ok with php but the script I need seems out of my league. I need a script that lets a user enter a name in a Text box and then hits Submit. Upon doing this I need the data he/she entered to write to a simple prewritten .txt file and launch a cgi program.

Here is the best example I can give.
1. user enters name (tiny)
2. hits submit
3. Name(tiny) is writen to the .txt file like this /name:tiny

4. cgi program is launched.

Example of prewrittem txt file.

<---start--->
Text jjjjjjjjj
Text lllllllll
Text jjjjjjjj
Text iiiiiiiiiii /sample /name:tiny (<-----written from script)
<---end--->

The cgi program actually reads this text file and needs it to launch the program.

Hope you all can Help


Thanks,
Tiny
Reply With Quote
  #2 (permalink)  
Old 01-11-04, 11:39 PM
digioz's Avatar
digioz digioz is offline
Community VIP
 
Join Date: Oct 2003
Location: Chicago, IL
Posts: 2,171
Thanks: 3
Thanked 9 Times in 9 Posts
Here you go:

// --------------- Code for page1.html -----------------------------------

Code:
<html><head><title>Page 1</title></head><body>
<form action="page2.php" method="post">
<table border="0" cellpadding="0" cellspacing="2">
<tr>
<td>
<p><b><font size="2">Name:</font></b></p>
</td>
<td><input type="text" name="from" size="20"></td>
</tr>
<tr>
<td></td>
<td>
<div align="right">
<input type="submit" name="ok" value="Send"></div>
</td>
</tr>
</table>
</form>
</body></html>
// --------------- Code for page2.php -----------------------------------

PHP Code:

<?php
echo "<html><head><title>Page 1</title></head><body>";
$name $_POST['name'];
echo 
"$name will be added to record on ";
echo 
$date;
echo 
"<br><br>";
$outputstring "/sample/name:$name\n";
$fp fopen("file.txt""a");
flock($fp2); 
if (!
$fp)
{
    echo 
"<p><strong> Your Addition could not be processed at this time.
         Please try again in a few minutes.</strong></p></body></html>"
;
    exit;

fwrite($fp$outputstring);
flock($fp3); 
fclose($fp);
echo 
"<center><p><b><a href=\"page3.php\">Click Here to continue</a></b></p></center>"
}
 
?>
// --------------- Code for page3.php -----------------------------------
PHP Code:

<?php
$URL
=http://yourdomain.com/cgi_file.cgi;
header ("Location: $URL");
?>
<html>
<title>Launching CGI File</title>
<head></head>
<body>
</body>
</html>
That should do it. Of course you can combine all 3 of these page into one. I hope this helps you out.
Quote:
Originally Posted by tiny
I am ok with php but the script I need seems out of my league. I need a script that lets a user
enter a name in a Text box and then hits Submit. Upon doing this I need the data he/she entered to write to a
simple prewritten .txt file and launch a cgi program.

Here is the best example I can give.
1. user enters name (tiny)
2. hits submit
3. Name(tiny) is writen to the .txt file like this /name:tiny

4. cgi program is launched.

Example of prewrittem txt file.

<---start--->
Text jjjjjjjjj
Text lllllllll
Text jjjjjjjj
Text iiiiiiiiiii /sample /name:tiny (<-----written from script)
<---end--->

The cgi program actually reads this text file and needs it to launch the program.

Hope you all can Help


Thanks,
Tiny
__________________

Last edited by digioz; 01-11-04 at 11:45 PM.
Reply With Quote
  #3 (permalink)  
Old 01-14-04, 09:07 PM
tiny tiny is offline
Newbie Coder
 
Join Date: Jan 2004
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Thumbs up

I wont be able to install this till this weekend. But thank you so much for your help. I will let you know how this goes.

Tiny
Reply With Quote
  #4 (permalink)  
Old 01-14-04, 09:51 PM
tiny tiny is offline
Newbie Coder
 
Join Date: Jan 2004
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
ok I went ahead and installed the script. I am probably doing something wrong, but I am getting a parse error.

Parse Error page2.php on line 20
Of course I have edited the script to reflect my page info.

Thanks,
Tiny
Reply With Quote
  #5 (permalink)  
Old 01-15-04, 11:42 AM
digioz's Avatar
digioz digioz is offline
Community VIP
 
Join Date: Oct 2003
Location: Chicago, IL
Posts: 2,171
Thanks: 3
Thanked 9 Times in 9 Posts
Create the file "file.txt" in that same directory and CHMOD it to 777 (give the file read, write and execute permission. Also there is an extra "}" at the end of page2.php that shouldn't be there (right before "?>") take that out as well.

Also, post your modified version of the code so we can look at it.





Quote:
Originally Posted by tiny
ok I went ahead and installed the script. I am probably doing something wrong, but I am getting a parse error.

Parse Error page2.php on line 20
Of course I have edited the script to reflect my page info.

Thanks,
Tiny
__________________

Last edited by digioz; 01-15-04 at 11:45 AM.
Reply With Quote
  #6 (permalink)  
Old 01-15-04, 07:24 PM
tiny tiny is offline
Newbie Coder
 
Join Date: Jan 2004
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Ok the page2.php works great now. It adds the needed line to the .txt file, however it adds it like this:

/sample /name:

The name I enter "tiny" does not appear.

should be like this

/sample /name:tiny

removing the } in page2.php was the ticket

Also I now am getting a parse error when clicking the "Click Here to continue" here is the error.

Parse error: parse error in /page3.php on line 2

The only thing I edited in the code was the url to reflect mine. All else remained the same.

Thank You so much for working with me on this.

Tiny
Reply With Quote
  #7 (permalink)  
Old 01-16-04, 09:27 AM
digioz's Avatar
digioz digioz is offline
Community VIP
 
Join Date: Oct 2003
Location: Chicago, IL
Posts: 2,171
Thanks: 3
Thanked 9 Times in 9 Posts
Oops.... it's because I named the field in the first page wrong. Try this for page 1:

Code:
<html><head><title>Page 1</title></head><body>
<form action="page2.php" method="post">
<table border="0" cellpadding="0" cellspacing="2">
<tr>
<td>
<p><b><font size="2">Name:</font></b></p>
</td>
<td><input type="text" name="name" size="20"></td>
</tr>
<tr>
<td></td>
<td>
<div align="right">
<input type="submit" name="ok" value="Send"></div>
</td>
</tr>
</table>
</form>
</body></html>
The only thing I change in page 1 was the following line:

Code:
<td><input type="text" name="name" size="20"></td>
__________________
Reply With Quote
  #8 (permalink)  
Old 01-16-04, 09:31 AM
digioz's Avatar
digioz digioz is offline
Community VIP
 
Join Date: Oct 2003
Location: Chicago, IL
Posts: 2,171
Thanks: 3
Thanked 9 Times in 9 Posts
Oh, and change page 3 to the following:

Code:
<?php
$URL="http://yourdomain.com/cgi_file.cgi";
header ("Location: $URL");
?>
<html>
<title>Launching CGI File</title>
<head></head>
<body>
</body>
</html> 


In other words, add quotes around the link you want to go to:

__________________
Reply With Quote
  #9 (permalink)  
Old 01-16-04, 09:20 PM
tiny tiny is offline
Newbie Coder
 
Join Date: Jan 2004
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Absolutely Beautiful. I should have found the quotations error. I appologize.

Real quick, would it be hard or even possible for the field we are adding to be erased or better yet overwritten each time instead of new lines?


Thanks a million
Tiny
Reply With Quote
  #10 (permalink)  
Old 01-17-04, 12:32 AM
digioz's Avatar
digioz digioz is offline
Community VIP
 
Join Date: Oct 2003
Location: Chicago, IL
Posts: 2,171
Thanks: 3
Thanked 9 Times in 9 Posts
Well, it depends. When you are saving the information to the text file, you can either append it (which means it will just add your text to the end of any previous content you had in that text file), or you can open it for write (which would mean any previous content in the file would be deleted before writing new content to it).

All you would have to change is to change the "a" to "w" in the following line:

Code:
@ $fp = fopen("list.txt", "w");
Does that answer your question?




Quote:
Originally Posted by tiny
Absolutely Beautiful. I should have found the quotations error. I appologize.

Real quick, would it be hard or even possible for the field we are adding to be erased or better yet overwritten each time instead of new lines?


Thanks a million
Tiny
__________________
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
php write to file tiny Script Requests 2 01-14-04 09:12 PM
Write form data to file dragge PHP 1 12-27-03 07:26 PM
PHP write to file jvfnd PHP 1 11-09-03 05:34 AM
PHP - Dir listing and write to file NickyWhicko PHP 4 08-24-03 02:42 AM
Upload file type and size limiter! Arctic ASP 1 08-02-03 07:06 PM


All times are GMT -5. The time now is 08:26 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.