Storing data from form into txt file and reading

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
fraga
Forum Newbie
Posts: 6
Joined: Thu Jun 04, 2009 5:03 am

Storing data from form into txt file and reading

Post by fraga »

Ok... i have a task to create a script that will Store data from html form into .txt file(every in a new line so later it could read everything in a new line) and then in a next page it will read from that txt file and sort everything in a list <ul>
<li></li></ul>
i can give you code that I have creatred and it stores data from form into txt (names) and it stores them each one next to one before : mikejohnpeterpatrickclarasony
And it should look like :
mike
john
peter
patrick
clara
sony
And in a next page it should dysplay everything from that txt file in this form:
  • Mike
    John
    Peter
    Patrick
    clara
    sony
    and so on...(if i add more names it would display them ;)
plss help
this is a code that i created :
html page aka form page : htmldio.html:
<body>
<form action="podaci.php" method="post">
Ime:
<input type="text" name="ime" align="left" />
<input type="submit" value="Dodaj ime ;)" />
</form>
</body>
php page for inserting data (ime) into txt : podaci.php:
<?php
$ime=$_POST['ime'];
$file="podaci.txt";
$open=fopen($file,"a") or die ("Nisam otvorio fajl sorry do mene je ;(");
fwrite($open,$ime."\r\n");
fclose($open);
echo "Uspjesno izvrseno (uz Myster Muscolo!!!)";
?>
<a href="citaj.php">Procitaj podatke</a>
And when user click on the link above (procitaj podatke) it should display this page:citaj.php
<?php
$file="podaci.txt";
$open=fopen($file,"r");
print(fread($open,10000));
?>
First 2 pages work perfectly but i dont know how to display list of names on last page citaj.php...
plss helpp!!! 8O 8O 8O 8O 8O
MeLight
Forum Commoner
Posts: 26
Joined: Sun Apr 19, 2009 12:39 pm
Location: Israel

Re: Storing data from form into txt file and reading

Post by MeLight »

Code: Select all

 
$fh = fopen("filename.txt", "r");
while(!feof($fh)) {
    echo fgets($fh);
}
fclose($fh);
 
You should read the file line by line.
fraga
Forum Newbie
Posts: 6
Joined: Thu Jun 04, 2009 5:03 am

Re: Storing data from form into txt file and reading

Post by fraga »

MeLight wrote:

Code: Select all

 
$fh = fopen("filename.txt", "r");
while(!feof($fh)) {
    echo fgets($fh);
}
fclose($fh);
 
You should read the file line by line.
hmm go to here,and enter your name in form and then click submit and then click on link(im using your code):
http://iphonemania.host22.com/htmldio.html
I need that to be each name in a new line!!!
User avatar
Chalks
Forum Contributor
Posts: 447
Joined: Thu Jul 12, 2007 7:55 am
Location: Indiana

Re: Storing data from form into txt file and reading

Post by Chalks »

fraga wrote:hmm go to here,and enter your name in form and then click submit and then click on link(im using your code):
http://iphonemania.host22.com/htmldio.html
I need that to be each name in a new line!!!
woah. My workplace blocks that website for "pornography"... a warning would be nice (or my workplace filter is overactive). Regardless, new lines for text documents are the code "\n". Just insert that at the end of each line and it should work fine.
fraga
Forum Newbie
Posts: 6
Joined: Thu Jun 04, 2009 5:03 am

Re: Storing data from form into txt file and reading

Post by fraga »

Chalks wrote:
fraga wrote:hmm go to here,and enter your name in form and then click submit and then click on link(im using your code):
http://iphonemania.host22.com/htmldio.html
I need that to be each name in a new line!!!
woah. My workplace blocks that website for "pornography"... a warning would be nice (or my workplace filter is overactive). Regardless, new lines for text documents are the code "\n". Just insert that at the end of each line and it should work fine.
ahahhah its a test page no porn... i soolved the prob thkss
User avatar
Chalks
Forum Contributor
Posts: 447
Joined: Thu Jul 12, 2007 7:55 am
Location: Indiana

Re: Storing data from form into txt file and reading

Post by Chalks »

fraga wrote:ahahhah its a test page no porn... i soolved the prob thkss
You'll definitely want to look into that though. If my workplace blocks it for that reason, I would not be surprised if other places do too. Send a strongly worded message to your host and ask them what's up. Unless, of course, you actually intend the page for that purpose. ;)
fraga
Forum Newbie
Posts: 6
Joined: Thu Jun 04, 2009 5:03 am

Re: Storing data from form into txt file and reading

Post by fraga »

Chalks wrote:
fraga wrote:ahahhah its a test page no porn... i soolved the prob thkss
You'll definitely want to look into that though. If my workplace blocks it for that reason, I would not be surprised if other places do too. Send a strongly worded message to your host and ask them what's up. Unless, of course, you actually intend the page for that purpose. ;)
Im still learning php and im practicing there on that url... iphonemania.host22.com is site about iphone made with wordpress...
Look i sorted words out but I need them to sort in list
<ul>
<li>Name#1</li>
<li>Name#2</li>
etc....
</ul>
How do i do that with php???
My reading code looks like this now:
<?php echo file_get_contents('podaci.txt'); ?>
User avatar
Chalks
Forum Contributor
Posts: 447
Joined: Thu Jul 12, 2007 7:55 am
Location: Indiana

Re: Storing data from form into txt file and reading

Post by Chalks »

If you're looking to sort data alphabetically, that's a whole different topic. To get you started though, check out php sort
MeLight
Forum Commoner
Posts: 26
Joined: Sun Apr 19, 2009 12:39 pm
Location: Israel

Re: Storing data from form into txt file and reading

Post by MeLight »

You'd want to read them in to an array (line by line, not file_get_contents), then sort the array and then print the array, again, line by line

sort
fraga
Forum Newbie
Posts: 6
Joined: Thu Jun 04, 2009 5:03 am

Re: Storing data from form into txt file and reading

Post by fraga »

Chalks wrote:If you're looking to sort data alphabetically, that's a whole different topic. To get you started though, check out php sort
not by alphabethicaly than i want to create a list... with dots before every word
User avatar
Chalks
Forum Contributor
Posts: 447
Joined: Thu Jul 12, 2007 7:55 am
Location: Indiana

Re: Storing data from form into txt file and reading

Post by Chalks »

oh. Then you're not creating a text file... you're creating an html file?

Again, just simply modify your code so that before each word it inserts <li> and after, </li>.
In otherwords:

Code: Select all

open file for writing
write "<ul>";
loop through form input:
  write "<li>" . $_POST['input'] . "</li>";
end loop
write "</ul>";
close file
This ignores escaping data (which you should be doing).
fraga
Forum Newbie
Posts: 6
Joined: Thu Jun 04, 2009 5:03 am

Re: Storing data from form into txt file and reading

Post by fraga »

Chalks wrote:oh. Then you're not creating a text file... you're creating an html file?

Again, just simply modify your code so that before each word it inserts <li> and after, </li>.
In otherwords:

Code: Select all

open file for writing
write "<ul>";
loop through form input:
  write "<li>" . $_POST['input'] . "</li>";
end loop
write "</ul>";
close file
This ignores escaping data (which you should be doing).
Now i need to strlen the names but only the names because i have "mr." im txt file just before names ex. mr. Mike
And next i have to create a security that will return "name already exists!" if the user enters ex. mike and mike exists in that txt. file ... can you help me pls.... this is of major importance... :banghead: :banghead: :banghead:
Post Reply