9 Replies - 838 Views - Last Post: 08 November 2013 - 06:19 PM Rate Topic: -----

#1 Inuinu172  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 55
  • Joined: 17-July 13

[problem] php write to file using html textbox

Posted 08 November 2013 - 02:53 PM

What im trying to do:

Write a user input value from a text box into a file when button is clicked (using php).

What is going wrong:

I enter some text, Click a button, get the echo message telling me the button was clicked, but it wont write to the file.

Code:


<form method="POST" action=''>
<input type="text" name="something" value="<?=$textdata?>"/>
<input type="submit" name="button1"  value="Click">
</form>
<?php
if (isset($_POST['button1'])) 
{ 
   echo "button1 has been pressed"; 
   $f = fopen("pastes/testfile.txt", "w");
   fwrite($f, $textdata); 
   fclose($f);
} 
?>


Additional information:

When I click the button, The text file is created, so I assume is has something to do with the $textdata variable, but I dont exactly know what I have done wrong.


I am still new to php, so please forgive me if it is a basic mistake.

Thanks in advance ^^

Is This A Good Question/Topic? 0
  • +

Replies To: [problem] php write to file using html textbox

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10816
  • View blog
  • Posts: 40,317
  • Joined: 27-December 08

Re: [problem] php write to file using html textbox

Posted 08 November 2013 - 02:58 PM

It looks like you didn't declare the $textdata variable, let alone assign it anything. That's why nothing was written.
Was This Post Helpful? 1
  • +
  • -

#3 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3835
  • View blog
  • Posts: 13,583
  • Joined: 12-December 12

Re: [problem] php write to file using html textbox

Posted 08 November 2013 - 03:01 PM

In addition, if you are expecting to write the value they entered into the textbox into the file then you should be reading $_POST['something']; that is, the value they have typed.
Was This Post Helpful? 2
  • +
  • -

#4 Inuinu172  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 55
  • Joined: 17-July 13

Re: [problem] php write to file using html textbox

Posted 08 November 2013 - 03:09 PM

View Postmacosxnerd101, on 08 November 2013 - 02:58 PM, said:

It looks like you didn't declare the $textdata variable, let alone assign it anything. That's why nothing was written.



View Postandrewsw, on 08 November 2013 - 03:01 PM, said:

In addition, if you are expecting to write the value they entered into the textbox into the file then you should be reading $_POST['something']; that is, the value they have typed.



Shoot, I totally didn't realize that.
Thanks a bunch guys.
(I kind of feel stupid now)


Full working code:


<form method="POST" action=''>
<input type="text" name="something" value="Some text">
<input type="submit" name="button1"  value="Click">
</form>
<?php
$name = $_POST['something'];
if (isset($_POST['button1'])) 
{ 
   echo "Wrote: $name to file";
   $f = fopen("pastes/testfile.txt", "w");
   fwrite($f, $name); 
   fclose($f);
} 
?>



This post has been edited by Inuinu172: 08 November 2013 - 03:14 PM

Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10816
  • View blog
  • Posts: 40,317
  • Joined: 27-December 08

Re: [problem] php write to file using html textbox

Posted 08 November 2013 - 03:09 PM

Glad we could help! :)
Was This Post Helpful? 1
  • +
  • -

#6 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3835
  • View blog
  • Posts: 13,583
  • Joined: 12-December 12

Re: [problem] php write to file using html textbox

Posted 08 November 2013 - 03:12 PM

Shouldn't that be fwrite($f, $name); ? ;)
Was This Post Helpful? 1
  • +
  • -

#7 Inuinu172  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 55
  • Joined: 17-July 13

Re: [problem] php write to file using html textbox

Posted 08 November 2013 - 03:14 PM

View Postandrewsw, on 08 November 2013 - 03:12 PM, said:

Shouldn't that be fwrite($f, $name); ? ;)/>


I had it edited in my web files, Sorry.
Ill fix the post.

Was This Post Helpful? 0
  • +
  • -

#8 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3835
  • View blog
  • Posts: 13,583
  • Joined: 12-December 12

Re: [problem] php write to file using html textbox

Posted 08 November 2013 - 03:39 PM

No need to apologise. Glad you sorted it.
Was This Post Helpful? 1
  • +
  • -

#9 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3730
  • View blog
  • Posts: 6,017
  • Joined: 08-June 10

Re: [problem] php write to file using html textbox

Posted 08 November 2013 - 04:47 PM

Now that you've got things working, allow me to poke a few holes in your code :)

There are two things in the code you posted that are less than ideal:

  • On line 7 you use isset to check if the submit button is sent, to verify that the form has indeed been submitted. However, on line 6 you declare $name to be the value of the textarea... before you know if the form has actually been submitted.

    So consider what happens when you first open the page; before you submit the form. Line 6 is reached, and PHP dutifully assigns $_POST["something"] to $name. The problem is, $_POST["something"] doesn't exist; the form hasn't been submitted, so it has no value. - This won't "break" anything, or cause any real problems. PHP will just assume you didn't check the post data properly before assigning it, and assigns null instead. However if error_reporting is set to show or log notices, PHP will give you an "undefined index" notice.

    What should really be happening in your code is that line 6 should be inside the IF block defined on line 7, so that the POST data is only accessed once you've verified that the POST data is in fact set.


  • Where you check that the form has been submitted, on line 7, you check if the submit button was sent. That's a common technique, but ultimately not an ideal method. You don't really care if the submit button was sent. What you do care about is whether the data was sent. So shouldn't you really be checking if the data is there, rather than the button?

    Fact is, in older browsers, there was no guarantee that the submit button would even be sent with the data. Depending on the browser and the method of submission - like, actual mouse clicks on the button vs pressing the enter key while the text input has focus - the submit button could very well be missing, while the data is present. This isn't a concern today, since these inconsistencies have been ironed out of modern browsers, but should still be considered.

    So, the bottom line: verify that the data exists, not the submit button. If you really think about it, it just makes more sense that way.

Was This Post Helpful? 2
  • +
  • -

#10 Inuinu172  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 55
  • Joined: 17-July 13

Re: [problem] php write to file using html textbox

Posted 08 November 2013 - 06:19 PM

Thanks for shining some light on the subject Atli!
I'll take into consideration to what you said and implement it into my code in the future.
You have been a huge help, Thanks again :D

This post has been edited by andrewsw: 09 November 2013 - 01:33 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1