Jump to content

How to write form and variable data to a text file


rbotham

Recommended Posts

Hi All,

Beginner here :)

I've done a fair bit of reading but can't seem to get an answer on this one.

I've a simple one input box html form with some test preceeding the input box.

I need to be able to write the text and the variable data in the input box to a file.

 

So if I input "Richard" into the text box I expect to get 'My Name Richard' written to the file

 

Code below

 

 

<html>

<body>

 

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

!<br>

My Name <input type="text" name="name" /> <br>

!<br>

<input type="submit" />

</form>

</font>

</body>

</html>

 

! php code

<?php

$filetowrite = "test2.txt";

$fh = fopen($filetowrite,"w") or die  ("Cannot open the file");

$name = $_POST["name"];

fwrite($fh, "$name\n");

fclose($fh);

?>

 

Any answers gratefully appreciated

 

Rich

Link to comment
Share on other sites

<html>

<body>

 

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

!

 

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

 

!

 

<input type="submit" />

</form>

</font>

</body>

</html>

 

! php code

<?php

$filetowrite = "test2.txt";

$fh = fopen($filetowrite,"w") or die  ("Cannot open the file");

$name = $_POST["name"];

fwrite($fh, "$name\n");

fclose($fh);

?>

 

Any answers gratefully appreciated

 

Rich

 

Hey Rich,

 

try

 

<?php
$fh = fopen("test2.txt", "w");
$name = htmlspecialchars(addslashes(trim($_POST['name']));
$outputstring = "My name is ". $name ."\n";
fwrite($fh, $outputstring);
fclose($fh)
?>

 

There is also a php5 function that does them all combined, called file_put_contents.

 

Sam

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.