Current location: Hot Scripts Forums » General Community » Script Requests » HTML Form that writes to a text file on the web server...?


HTML Form that writes to a text file on the web server...?

Reply
  #1 (permalink)  
Old 02-12-09, 09:15 AM
ViciousTruth ViciousTruth is offline
New Member
 
Join Date: Feb 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
HTML Form that writes to a text file on the web server...?

Hey,

I have a simple form. The fields are:

Name
Telephone
Zip Code

{SUBMIT}


I designed it around the "mailto" action, but what I want is for the form data to be written to a text file on my web server.

How can I accomplish this?

Thanks in advance to those who reply,

=VT
Reply With Quote
  #2 (permalink)  
Old 02-12-09, 12:40 PM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
This little program uses two files (index.php and store_it.php).

index.php has the form to enter name, telephone and zip code.

store_it.php checks to see if you entered data into all three field.
If you didn't then it sends the data you did enter back to the form.
If you did then it appends the data to a file called "PhoneBook.txt".
Then it reads the data from the file and displays it.
Then it displays another form that can send you back to index.php to enter another name, telephone and zip code.

Please note that there is no error checking in this program other than to see if you have entered a value into each field.
So you will have to add in your own.

I am sure there are many other ways to setup this program,
but it does give you an example of how to 'write to' and 'read from' a file.

index.php
PHP Code:

<?php
$name 
= empty($_GET["name"]) ? "" $_GET["name"];
$phone = empty($_GET["phone"]) ? "" $_GET["phone"];
$zip = empty($_GET["zip"]) ? "" $_GET["zip"];
if(!
$name){$message1 "Please enter a name.";}
if(!
$phone){$message2 "Please enter a phone number.";}
if(!
$zip){$message3 "Please enter a zip code.";}
?>
<html>
<head>
<style>
.tdr{text-align:right;}
</style>
</head>
<body>
<form action="store_it.php" method="post">
 <table>
  <tr>
   <td class="tdr">Name:</td>
   <td><input type="text" name="name" value="<?php echo $name?>"></td>
   <td>&nbsp;<?php echo $message1?></td>
  </tr>
  <tr>
   <td class="tdr">Telephone:</td>
   <td><input type="text" name="Telephone" value="<?php echo $phone?>"></td>
   <td align="left">&nbsp;<?php echo $message2 ?></td>
  </tr>
  <tr>
   <td class="tdr">Zip Code:</td>
   <td><input type="text" name="ZipCode" value="<?php echo $zip?>"></td>
   <td align="left">&nbsp;<?php echo $message3 ?></td>
  </tr>
  <tr>
   <td colspan="3" align="center"><input type="submit" value="Submit"></td>
</body>
</html>
store_it.php
PHP Code:

<?php
$name 
= empty($_POST["name"]) ? "" $_POST["name"];
$Telephone = empty($_POST["Telephone"]) ? "" $_POST["Telephone"];
$ZipCode = empty($_POST["ZipCode"]) ? "" $_POST["ZipCode"];
if(!
$name || !$Telephone || !$ZipCode){header("location:index.php?name=$name&phone=$Telephone&zip=$ZipCode");}
else
{
 
$file "PhoneBook.txt";
 
$a fopen($file"a");
 
fwrite($a,$name."\r\n");
 
fwrite($a,$Telephone."\r\n");
 
fwrite($a,$ZipCode."\r\n");
 
fclose($a);
 
$a fopen($file"r");
 echo 
"<table border='1'>";
 echo 
"<tr><th>Name</th><th>Telephone</th><th>Zip Code</th></tr>";
 while(!
feof($a))
 {
  echo 
"<tr><td>".fgets($a)."</td><td>".fgets($a)."</td><td>".fgets($a)."</td></tr>";
  }
 echo 
"</table>";
 
fclose($a);
 }
?>
<form action="index.php">
<input type="submit" value="Add another">
</form>
__________________
Jerry Broughton

Last edited by job0107; 02-12-09 at 12:44 PM.
Reply With Quote
  #3 (permalink)  
Old 02-12-09, 03:17 PM
ViciousTruth ViciousTruth is offline
New Member
 
Join Date: Feb 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Jerry,

THANK YOU!


-VT
Reply With Quote
  #4 (permalink)  
Old 02-19-09, 06:49 AM
crodriguez1a crodriguez1a is offline
Newbie Coder
 
Join Date: Feb 2009
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
overwrite the file

How would I overwrite the existing information as opposed to appending it?
Reply With Quote
  #5 (permalink)  
Old 02-19-09, 07:04 AM
boland boland is offline
New Member
 
Join Date: Feb 2009
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
$a = fopen($file, "a");

Change "a" to the correct parameter, "w" i think.
Reply With Quote
  #6 (permalink)  
Old 02-19-09, 07:07 AM
crodriguez1a crodriguez1a is offline
Newbie Coder
 
Join Date: Feb 2009
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Excellent! Thanks
Reply With Quote
  #7 (permalink)  
Old 02-19-09, 07:09 AM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
It's easiest if you just go to the manual and read it.
http://us2.php.net/fopen
__________________
Jerry Broughton
Reply With Quote
  #8 (permalink)  
Old 03-03-09, 09:31 PM
crodriguez1a crodriguez1a is offline
Newbie Coder
 
Join Date: Feb 2009
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Creating and uploading to a dynamic directory

Is it possible to create a dynamic directory and then upload to it on the fly?

here is my code: I can create the directory and I can upload files but I cannot combine the two. The $custinfo variable is being delivered from flashvars and is recognized fine.

<?php
$custinfo = $_POST['custinfo'];
if( $custinfo == true )
{
mkdir("$custinfo", 0700);
}
move_uploaded_file($_FILES['Filedata']['tmp_name'], "$custinfo".$_FILES['Filedata']['name']);
chmod("$custinfo".$_FILES['Filedata']['name'], 0777);
?>

Thanks
Reply With Quote
  #9 (permalink)  
Old 03-04-09, 07:09 AM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
Quote:
Originally Posted by crodriguez1a View Post
Is it possible to create a dynamic directory and then upload to it on the fly?

here is my code: I can create the directory and I can upload files but I cannot combine the two. The $custinfo variable is being delivered from flashvars and is recognized fine.

<?php
$custinfo = $_POST['custinfo'];
if( $custinfo == true )
{
mkdir("$custinfo", 0700);
}
move_uploaded_file($_FILES['Filedata']['tmp_name'], "$custinfo".$_FILES['Filedata']['name']);
chmod("$custinfo".$_FILES['Filedata']['name'], 0777);
?>

Thanks
What is the exact value of $custinfo ?
__________________
Jerry Broughton
Reply With Quote
  #10 (permalink)  
Old 03-04-09, 07:18 AM
crodriguez1a crodriguez1a is offline
Newbie Coder
 
Join Date: Feb 2009
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
$custinfo

it is a variable that comes through flash and is dynamic. It consist of the letters in the customers' last name and their customer id number. no spaces no dashes (eg; 75491Smith)
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
text box with scroll bar silvermane CSS 7 01-16-09 03:03 AM
Using a form to replace data in a text file dnb198 ASP 1 12-22-08 06:26 AM
[SOLVED] Multitype Form Data - Images and Text into SQL Server digitalkemical ASP 2 12-04-08 01:01 PM
Writes to a text file gamextremer2003 JavaScript 4 09-11-03 09:43 AM
Need to submit form to database and text file - Any ideas how? dpreiss ASP 1 08-21-03 06:02 PM


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