Jump to content

[SOLVED] Add item to a .txt file [Newby here]


dennisvdz

Recommended Posts

I'm quite new in PHP, so I want a example only.

Please don't post any parts, becouse I can learn only if I see a code and self find what ever line does :).

 

Can someone make me a little example script, that will add every time, someone presses on a button, the text, that is containing in the textbox, get added above a .txt file?

Like

 

[TEXTBOX] [bUTTON]

[TXT FILE:

 

END TXT FILE]

 

If I will type 'Hi' in the textbox, and then press on the button, it will be like this:

 

[TEXTBOX] [bUTTON]

[TXT FILE:

Hi

END TXT FILE]

 

If I will type now 'Rofl' in the box, and press again, it will be like this:

 

[TEXTBOX] [bUTTON]

[TXT FILE:

Rofl

Hi

END TXT FILE]

 

But if I typed notting in the textbox, notting happens too when I press the button.

 

If it's possible, it would be fine if, when the button has been pressed, you will get redirected to the page 'succes.php'

 

Thank you,

Dennis.

Link to comment
Share on other sites

Can someone make me a little example script, that will add every time, someone presses on a button, the text, that is containing in the textbox, get added above a .txt file?

 

There are plenty of examples online, why don't you give it a try, fwrite & submit.

Link to comment
Share on other sites

Aha.

 

I understand fwrite() now a bit.

I actually don't know anything about connecting them.

 

I found this about sumbit.

How can I activate a pice of PHP code, that takes the value into the box 'text' with it?

 

<html>

<body>

 

<form action="Something.php" method="post">

Text: <input type="text" name="text" />

<input type="submit" />

</form>

 

</body>

</html>

Link to comment
Share on other sites

<?php
$filename = "test01.txt"; //the name of our file.
$content = "This is our test file"; //what we will be writing to our file.
$strlength = strlen($content); //gets the length of our $content string.
$create = fopen($filename, "w"); //uses fopen to create our file.
$write = fwrite($create, $content, $strlength); //writes our string to our file.
$close = fclose($create); //closes our file
echo("File Created, Click <a href='$filename'> Here </a> to view it.");
?>

Link to comment
Share on other sites

This will post to the itself, (the same page you're on):

 



if(isset($_POST['submit']))
{
   echo "POST text: " . $_POST['text'];
   //execute code
}
?>
   
</pre>
<form action="<?php%20echo%20%24_SERVER%5B'PHP_SELF'%5D;%20?>" method="POST">
Text: 

</form>
<br><b

 

NOTE - Please use


tags around code.

Link to comment
Share on other sites

Ok.

I tried this stuff:

 

SITE

- store.txt

- add.php

- index.php

 

ERROR:

Parse error: parse error, unexpected '\"', expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/www/testserver/add.php on line 3

 

FILES:

store.txt:

Empty

 

add.php:

<?php

$filename = 'store.txt';

$somecontent = "Name: $_POST["name"] \n";

 

// Let's make sure the file exists and is writable first.

if (is_writable($filename)) {

 

    // In our example we're opening $filename in append mode.

    // The file pointer is at the bottom of the file hence

    // that's where $somecontent will go when we fwrite() it.

    if (!$handle = fopen($filename, 'a')) {

        echo "Cannot open file ($filename)";

        exit;

    }

 

    // Write $somecontent to our opened file.

    if (fwrite($handle, $somecontent) === FALSE) {

        echo "Cannot write to file ($filename)";

        exit;

    }

 

    echo "Success, wrote ($somecontent) to file ($filename)";

 

    fclose($handle);

 

} else {

    echo "The file $filename is not writable";

}

?>

 

index.php:

<html>

<body>

 

<form action="add.php" method="post">

Name: <input type="text" name="name" />

<input type="submit" />

</form>

 

</body>

</html>

 

 

Does someone knows whats wrong? :).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.