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

<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

You only can receive what is inputted in the box.. so if you want "My Name is Richard" instead of just "Richard" (which is what they type)

 

On your php page do this:

 

$name = "My Name Is ".$_POST['name'];

 

That will put "My Name Is" before their name

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.