-2

Original: I am trying to make the PHP get the input that was put into the text box and write it to name.txt. I am also getting an error message that says "Expected tag name. Got '?' instead." on line 6.

<html>
    <body>
        <p>Enter name here:</p>
        <input type="text" id="name"/>
        <button onclick="[activate PHP]">Enter</button>
        <?php
            $fp = fopen('name.txt', 'w');
            fwrite($fp, '[name entered]');
            fclose($fp);
        ?>
    </body>
</html>

I am not familiar with PHP, so please explain what your fixed code does when you answer.

New: This was made by @Vlad Gincher. The problem is, the code is not creating a .txt file, which leads me to believe that the PHP is executing as soon as the page loads. Is there a way to activate the PHP when the form is submitted?

<?php
  if(isset($_POST["textareaValue"])) {
    $fp = fopen('name.txt', 'w');
    fwrite($fp, $_POST["textareaValue"]);
    fclose($fp);
  }
?>
<html>
  <body>
    <p>Enter name here:</p>
    <form method="post">
      <input type="text" id="name" name="textareaValue" />
      <input type="submit" value="Enter" />
      </form>
  </body>
</html>

Again, I am inexperienced with PHP, it could be an error of mine.

Bryson Noble
  • 337
  • 1
  • 11

2 Answers2

1

PHP runs on the server side. After the server finishes, it returns the output to the client. In that point, you can't use PHP, but you can force the client to send another HTTP request so the PHP would be activated.

Here is a code where it runs, checks if the client sent information to the server, and if so, add the information the the file. If not, it does nothing.

I'm using HTML's form element to tell the browser to send the information back to the server using post. Then, I can get the value using PHP's $_POST, and with the name of the input that I want to get the data from (textareaValue)

<?php
  if(isset($_POST["textareaValue"])) {
    $fp = fopen('name.txt', 'w');
    fwrite($fp, $_POST["textareaValue"]);
    fclose($fp);
  }
?>
<html>
  <body>
    <p>Enter name here:</p>
    <form method="post">
      <input type="text" id="name" name="textareaValue" />
      <input type="submit" value="Enter" />
    </form>
  </body>
</html>
Vlad Gincher
  • 1,052
  • 11
  • 20
  • @BrysonNoble - Vlad's answer here is a good one. He is demonstrating how to send the form to the very same file that contains the HTML form (so the web page is "submitting" the form to itself). Great answer: +1 – cssyphus Jan 26 '19 at 21:37
0

When the web page has been rendered (displayed to user), the PHP is finished. PHP cannot interact with a user - for that you need either (a) an HTML form that is submitted to a .php file (even the same one that contains the form), or -- and this is far more popular these days -- javascript with AJAX.

Here is another answer that discusses both:

How can I make, when clicking a button, send an email with the data included from the form?

HTML forms get "submitted" to a back-end PHP file that receives the data from the form fields. Each form field has a name= attribute on the HTML tag -- that becomes the variable name, and the contents of the HTML element becomes the variable's data.

A very similar thing happens with ajax, except that it is more flexible (the HTML form system is very rigid/static), and (most importantly) the web page need not refresh.

cssyphus
  • 37,875
  • 18
  • 96
  • 111