0

I'm working on learning PHP and I'm trying to essentially have the user enter input into two different text areas, and having the content downloaded into a single .txt file once they click the button. I know my code is extremely rough, I haven't found any solutions online for my specific case-scenario, but here it is:

<!DOCTYPE html>

<html>

<head>
  <title>Home</title>
  <meta charset="UTF-8" />
  <link rel="stylesheet" href="./style.css" />

</head>

<div class="navbar">
  <a href="adminIndex.html">Home</a>
  <a href="adminResume.html">Resume</a>
  <a href="adminProjects.html">Projects</a>
  <a href="adminContact.html">Contact</a>
  <a href="adminSocial.html">Social</a>
  <a href="logout.html">Logout</a>
</div>

<body>
  <label for="textbox">Professional Summary</label>
  <textarea id="textbox"></textarea>

  <label for="bio">Brief Biography</label>
  <textarea id="bio"></textarea>

  <input type="button" id="homebt" value="Submit" />

</body>

</html>

<?php
if (isset($_POST['submit'])) {
    $text = $_POST['bio'];
    print ($text);

    $filename = 'index.txt';
    $string = $text;

    $fp = fopen($filename, "w");
    fwrite($fp, $string);
    fclose($fp);

    header('Content-disposition: attachment; filename=test.txt');
    header('Content-type: application/txt');
    readfile('test.txt');
    die;
}

Currently nothing happens when the button is clicked. The entire code is in a file called adminIndex.php, and I have Apache Web Server running to open the file in my browser for testing purposes. If anyone could potentially help me out with what I'm doing wrong, I'd really appreciate it!

Edit (made the following changes to the end of the HTML code):

<form name='form' method='post'>

    <label for="textbox">Professional Summary</label>
    <textarea id="textbox"></textarea>

    <label for="bio">Brief Biography</label>
    <textarea id="bio"></textarea>

    <input type="submit" id="homebt" value="Submit" name="submit" />
    
  </form>

</body>
giotto1
  • 49
  • 5
  • You're missing a `
    ` to contain all your inputs, so no data can ever be sent. Also your button should be of `type="submit"` and it should have `name="submit"` since that's what you check in your `php`
    – Andrea Olivato Apr 11 '22 at 02:16
  • @AndreaOlivato thanks! I put in a form and made the edits, however now I am getting a "POST http://localhost/Assignment%202/adminIndex.php 500 (Internal Server Error)" – giotto1 Apr 11 '22 at 02:23
  • 500 is a general error. You need to debug it https://stackoverflow.com/questions/22170864/500-internal-server-error-how-to-debug – Andrea Olivato Apr 11 '22 at 02:39
  • Also you should move your PHP code at top of the file. `header` functions can not work after you already have some output. [Read here](https://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php) for more – Andrea Olivato Apr 11 '22 at 02:41

0 Answers0