Jump to content
xisto Community
Baniboy

Writing Form Content On A File With Php [resolved] Yep, I need some help on this...

Recommended Posts

Well I was just trying to make a simple way to add comments to articles but then I faced a problem. I'm not getting any error with this code:

<form action="comment.php" method="post">Name: <input type="text" name="name" /><br/>Comment: <input type="text" name="comment" /><br/><input type="submit" value="comment this article" /><br/></form>

Then the script:

<?php$name = $_POST['name'];$comment = $_POST['comment'];$file=fopen("comments.php","a+");fwrite ($file, "$name", " " . "$comment");fclose ($file);?>

Am I doing something wrong or can't php write form data in files? It creates the file but writes nothing in it...
Edited by baniboy (see edit history)

Share this post


Link to post
Share on other sites

I'll post a sample of a code I use as soon as I find it in my account. Hold on a second or 12.*edit*check for the file resource being available by adding this line before you do the write.

 /***************************************************************************write the information to a flat (text) file as a fallback storage method**************************************************************************** */if ( $f_name && $l_name && $email && $body) { $file_name = "message_data.txt"; $handle = @fopen($file_name, "a"); if (!$handle) { echo "File Handle Not Available For Use"; exit; } $body = strip_tags(str_replace( "\r" , ' ' , $body )); $body = str_replace( "\n" , ' ' , $body ); $body = str_replace( "\'" , '&' , $body ); $body = str_replace( "\"" , '&' , $body ); $message_string = $date . ' , ' . $name . ' , ' . $email . ',' . $body . ',' . $_SERVER['REMOTE_ADDR']."\r\n"; fwrite($handle, $message_string); //close the open file prior to ending  fclose($handle); 

Notice that I strip the newlines and returns from the data and place one at the end of the string? That might be the solution, too.Post back if it fails to fix the issue.

Share this post


Link to post
Share on other sites

I'll post a sample of a code I use as soon as I find it in my account. Hold on a second or 12.
*edit*

check for the file resource being available by adding this line before you do the write.

$file=fopen("comments.php","a+"); if (!$file) { echo "File Handle Not Available For Use"; exit; } fwrite ($file, "$name", " " . "$comment");

I'm not getting any errors and it's still doesn't write anything into the file, I'm running this on WAMP,if you have wampy pampy too, can you test if the code I've written works on your php server?

Share this post


Link to post
Share on other sites

By the way, baniboy, are you writing this in the script page itself???? Or are you using that as an example? In your code you're executing comments.php and at the same time opening it to add text?? Please check and tell me if that's what you want to do.

Else this a code you could also use and try if what I said up there was OK:

<?php$filename = 'filetext.txt';$somecontent = "Add this text to the file\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";//}?>

Edited by Pankyy (see edit history)

Share this post


Link to post
Share on other sites

By the way, baniboy, are you writing this in the script page itself???? Or are you using that as an example? In your code you're executing comments.php and at the same time opening it to add text??

No the script is in file "comment.php" and it's trying to open "comments.php" .

I can do that what you coded, it's just it doesn't work when it comes from a form... uuhh.. I'm getting so frustrated, I'm off to sleep now, bye.

Share this post


Link to post
Share on other sites

Sorry, to apply it to your code, change the following lines:

$filename = 'filetext.txt';$somecontent = "Add this text to the file\n";to$filename = 'comments.php';$somecontent = "\nName: {$_POST['name']}\nComment: {$_POST['comment']}\n";

And that's it. But if it still doesn't work change comments.php to comments.txt and try that way.

Share this post


Link to post
Share on other sites

The problem is located in the fwrite function:

fwrite ($file, "$name", " " . "$comment");
Why? Because you're passing it a third parameter that is pure text. What happens when you try to convert a string into an integer the PHP way when the string has no integer in the beginning of it (in your case there is a space character and who knows what $comment equals)? If there is no number starting in the beginning of it but a non-numerical character, then when PHP casts the string to an integer in the background, it'll become 0. Since the third parameter tells fwrite how many bytes not to go over concerning the filesize of the file, you are telling fwrite to write to the file while making sure the filesize remains at 0 bytes. Therefore you are experiencing no errors though there is nothing in the file. The correct way of writing fwrite would be:
fwrite($file, $name.":<br/>".$comment);

Share this post


Link to post
Share on other sites

Good news dear helpers! I made a hydrid from truefusions and Pankyy's code and it wrote on the file. Thanks for all your help people!
haslip I noticed your code a lil' while ago, it worked after a little adjustments to my code but I decided to go with the other one since it was shorter.

But with this code that does write on the file:

<?php$somecontent = "\nName: {$_POST['name']}\nComment: {$_POST['comment']}\n";$hr = "<hr/>";$file=fopen("comments.xml","a+");if (!$file) { echo "File Handle Not Available For Use"; exit; };fwrite($file, $somecontent);fclose ($file);?>

The problem is when I try adding "<br/> and "<hr/>" to $somecontent, it doesn't write them and gives me an parse error!

How can I add br and hr tags in $somecontent? I tried this:

$somecontent = "<br/>" + "\nName: {$_POST['name']}\nComment: {$_POST['comment']}\n";

Also would like to add br into the middle of 'name' and 'comment'
Edited by baniboy (see edit history)

Share this post


Link to post
Share on other sites

The problem is when I try adding "<br/> and "<hr/>" to $somecontent, it doesn't write them and gives me an parse error!
How can I add br and hr tags in $somecontent? I tried this:

$somecontent = "<br/>" + "\nName: {$_POST['name']}\nComment: {$_POST['comment']}\n";

Also would like to add br into the middle of 'name' and 'comment'
I'm guessing the plus sign in the code snippet is from your knowledge in another language. PHP doesn't do plus signs when combining strings with (and) variables, it does dots. However, the proper way of doing what you want would be (taking a guess at where you want <hr/>):
$somecontent = "<br/>\nName: {$_POST['name']}<br/>\nComment: {$_POST['comment']}\n<hr/>";

Share this post


Link to post
Share on other sites

I'm guessing the plus sign in the code snippet is from your knowledge in another language. PHP doesn't do plus signs when combining strings with (and) variables, it does dots. However, the proper way of doing what you want would be (taking a guess at where you want <hr/>):

$somecontent = "<br/>\nName: {$_POST['name']}<br/>\nComment: {$_POST['comment']}\n<hr/>";
Yea I didn't know that you can't plus variables in php. I thought that if you can put echo (bani + boy); and it'll type baniboy then you can plus variables in the same way. Thanks for the help truefusion.

EDIT: Could some mod please make the topic [resolved] or will I have to do it via report button or something?
Edited by baniboy (see edit history)

Share this post


Link to post
Share on other sites

Topic is resolved.Please PM any moderator to continue this discussion. Until then, this topic is closed.

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.