Jump to content

separate forms + write to text files


hsub

Recommended Posts

Hi everyone.

I have 3 separate simple php forms, each with its own submit button, that are writing to a text file. Every time someone submits a message, that message is sent to a text file and the previous message in the text file is over written. This is what i want.

 

The problem, however, is that when i submit one form and if a message has been sent previously with the either of the other two forms, each text file is over written. Essentially, what happens is that there are two blank text files, and one with the current message. I would like for the newest message to be kept in the two text files that are not being used each time someone choses a form to submit. Only the one chosen should be over written.

 

thank you for any help on this....

 

Here is the code, i think this will probably help clarify what i am saying.

 

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method='post'>

<p class="style2">Jamil Message Updater:<br><input type="text" name="name" /></p>

<p class="style2"><input type="submit" Value="Build"/></p>

</form>

 

 

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method='post'>

<p class="style2">Razy Message Updater:<br><input type="text" name="name1" /></p>

<p class="style2"><input type="submit" Value="Build"/></p>

</form>

 

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method='post'>

<p class="style2">Stephanie Message Updater: <input type="text" name="name2" /></p>

<p class="style2"><input type="submit" Value="Build"/></p>

</form>

 

<? $myFile = "message.txt";

$fh = fopen($myFile, 'w') or die("can't open file");

// $stringData = "Bobby Bopper\n";

fwrite($fh, $_POST['name']);

fclose($fh); ?>       

 

<?php echo htmlspecialchars($_POST['name']); ?>

<?$myFile1 = "message1.txt";

$fh1 = fopen($myFile1, 'w') or die("can't open file");

// $stringData = "Bobby Bopper\n";

fwrite($fh1, $_POST['name1']);

fclose($fh1);

?>       

 

<?php echo htmlspecialchars($_POST['name1']); ?>

 

<? $myFile2 = "message2.txt";

$fh2 = fopen($myFile2, 'w') or die("can't open file");

// $stringData = "Bobby Bopper\n";

fwrite($fh2, $_POST['name2']);

fclose($fh2);

?>

       

<?php echo htmlspecialchars($_POST['name2']); ?>

 

Link to comment
Share on other sites

Congratulations, your code made my head hurt so bad, I was forced to rewrite it...

 

<?php
if ($_POST['message'] != "") {
switch ($_POST['name']) {
	case 'Jamil':
		$file = 'jamil.txt';
		break;
	case 'Razy':
		$file = 'razy.txt';
		break;
	case 'Stephanie':
		$file = 'stephanie.txt';
		break;
}

/*
	Alternate filename, which would eliminate the switch above:
	$file = $_POST['name'] . ".txt";
*/

if (file_put_contents($file, htmlspecialchars(stripslashes($_POST['message']))) {
	echo "Your message was recorded";
} else {
	echo "An error occurred";
}
}
?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method='post'>
<p class="style2">
<select name="name">
	<option>Jamil</option>
	<option>Razy</option>
	<option>Stephanie</option>
</select>
 
Message Updater:
</p>
<p class="style2">
<input type="text" name="message" />
</p>
<p class="style2">
<input type="submit" Value="Build"/>
</p>
</form>

Link to comment
Share on other sites

Hi Hitman,

thanks for such a quick reply and sorry for the headache. I should have taken better care is structuring the code. Will remember that next time i am seeking help from others. In any case, i'm in the processing of trying out your code and i'm running into an error. The error states..

 

Fatal error: Call to undefined function: file_put_contents() in /home/hsubr3/public_html/fbook/index3.php on line 28..

 

so i checked up on this, an you can only use file_put_contents() in php 5, my sever supports only 4.4.4. go figure.

so i am look at the alternatives to this and found an the way you would work around if you didn't have php 5. So

 

Now i have new errors which i can't seem to fix. I am looking, but thought i would speed things up by asking for some help.

 

<?php
// if (file_put_contents($file, htmlspecialchars(stripslashes($_POST['message'])))) {		
if (!function_exists('file_put_contents')) {
function file_put_contents($file, htmlspecialchars(stripslashes($_POST['message'])))  {
$fh = fopen($file, 'w') or die("can't open file");

if(!$fh) {
trigger_error('file_put_contents cannot write in file.', E_USER_ERROR);
return;
}
fputs($fh, $file);
fclose($fh);
}
}
echo "Your message was recorded";
} else {
echo "An error occurred";
}
}
?>

 

any ideas on how to solve this.

Link to comment
Share on other sites

You have to define the function before you use it:

 

<?php
if (!function_exists('file_put_contents')) {
function file_put_contents($file, htmlspecialchars(stripslashes($_POST['message'])))  {
	$fh = fopen($file, 'w') or die("can't open file");

	if(!$fh) {
		trigger_error('file_put_contents cannot write in file.', E_USER_ERROR);
		return;
	}

	fputs($fh, $file);
	fclose($fh);
}
}

if (file_put_contents($file, htmlspecialchars(stripslashes($_POST['message'])))) {		
echo "Your message was recorded";
} else {
echo "An error occurred";
}
?>

Link to comment
Share on other sites

This was the error i was getting and still am.

I am quite new at this...so bare with me.

 

Parse error: syntax error, unexpected T_STRING, expecting '&' or T_VARIABLE or T_CONST in /home/hsubr3/public_html/fbook/index3.php on line 24:

 

	function file_put_contents($file, htmlspecialchars(stripslashes($_POST['message'])))  {

 

 

Link to comment
Share on other sites

if (!function_exists('file_put_contents')) {
function file_put_contents($file, $contents)  {
	$fh = fopen($file, 'w') or die("can't open file");

	if(!$fh) {
		trigger_error('file_put_contents cannot write in file.', E_USER_ERROR);
		return;
	}

	fwrite($fh, $contents);
	fclose($fh);
}
}

 

didn't notice that the function declaration was wrong...

Link to comment
Share on other sites

Hello again...

I tried a separate to make sure that the files i am writing to are writable. They all work. However, when i run the entire script with the form, doesn't seem to what to open the file.

 

Any ideas as to what's wrong.

thank you again for all your help.

 

<?php
if ($_POST['message'] != "") {
switch ($_POST['name']) {
	case 'Jamil':
		$file = 'message.txt';
		break;
	case 'Razy':
		$file = 'message1.txt';
		break;
	case 'Stephanie':
		$file = 'message2.txt';
		break;
}
}
?>
<?php
if (!function_exists('file_put_contents')) {
function file_put_contents($file, $contents)  {
	$fh = fopen($file, 'w') or die("can't open file");

	if(!$fh) {
		trigger_error('file_put_contents cannot write in file.', E_USER_ERROR);
		return;
	}

	fwrite($fh, $contents);
	fclose($fh);
}
}

if (file_put_contents($file, htmlspecialchars(stripslashes($_POST['message'])))) {		
echo "Your message was recorded";
} else {
echo "An error occurred";
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method='post'>
<p class="style2">
<select name="name">
	<option>Jamil</option>
	<option>Razy</option>
	<option>Stephanie</option>
</select>
 
Message Updater:
</p>
<p class="style2">
<input type="text" name="message" />
</p>
<p class="style2">
<input type="submit" Value="Build"/>
</p>
</form>

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.