Jump to content

Recommended Posts

Hi,

 

i'm a noob, so here's a noobish question:

 

How can i crate a form, with a button so when it is clicked, letters from a form are writen to a file... Like this:

 

form.jpg

 

So i write something to it, click Submit and the txt is saved to a name.txt file on a server...

 

Is that hard to do?

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/46994-writing-to-file-from-a-form/
Share on other sites

something like this

 

<?php
// Gets data from form
$your_data = $_GET('formdata');

// Open the file and erase the contents if any
$fp = fopen("textfile_name.txt", "w");

// Write the data to the file
fwrite($fp, $your_data);

// Close the file
fclose($fp);
?>

 

remember we help you with your code we don't write it for you.

something like this

 

<?php
// Gets data from form
$your_data = $_GET('formdata');

// Open the file and erase the contents if any
$fp = fopen("textfile_name.txt", "w");

// Write the data to the file
fwrite($fp, $your_data);

// Close the file
fclose($fp);
?>

 

remember we help you with your code we don't write it for you.

$_GET('formdata') should be $_GET['formdata']

Hi there,

 

I am a bit thick in the head and I can't work out why the following script writes the "||||||"s and the date to a text file but misses out the variables.

 

I am not getting any error messages...Any help would be much appreciated!!

 

Actually, I don't think the $_POSTs are recognised at all:

 

 

<?php

 

if (isset($_POST['submit'])) {

if (empty($_POST['title'])) {

echo '<p class="error">Please enter a title<br />'; }

elseif (empty($_POST['news'])) {

echo '<p class="error">Please enter some news<br />'; }

} else {

// Setup and save the news item

 

//$line = $_POST['title'];

//$line .= "||".$_POST['user'];

//$line .= "||".$_POST['news'];

//$line .= "||".date('D m.d.y h:i:s A')."\r\n";

 

$title = $_POST['title'];

$user = $_POST['user'];

$news = $_POST['news'];

 

$line = $title;

$line .= "||".$user;

$line .= "||".$news;

$line .= "||".date('D m.d.y h:i:s A')."\r\n";

 

$fp = fopen("news.txt","a");

 

//if(!$fp) {

//    echo 'Error: Cannot open file.';

//    exit;

 

//}

 

fwrite($fp, $line);

 

fclose($fp);

 

}

 

?>

 

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

  <p>

  Title<br/>

  <input type="text" name="title" size="19" maxlength="35"><br/>

  User (who you are)<br/>

  <select name="user">

<option value="1">David</option>

<option value="2">Cochise</option>

<option value="3">Ben</option>

</select><br/>

  News text<br/>

  <textarea rows="9" name="news" cols="30"></textarea><br/><br/>

  <input type="submit" value="Post" name="submit"></p>

</form>

 

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.