简体   繁体   中英

Sending info from HTML form to a a text file using PHP

I was wondering how I could send information from an HTMLform to aa text file using PHP. My current code is not adding anything to the text file. The HTML and PHP files are below. Hopefully someone can help me if any more information is needed please let me know and I will provide you with the information in the comments.

HTML

<form action="results.php" method="post">
    <table border="1">
        <tr>
            <th colspan="7">Survey (CSC443)</th>
        </tr>
        <tr>
            <td rowspan="2">#</td>
            <td rowspan="2">Question</td>
            <td>Strongly</td>
            <td>Somewhat</td>
            <td></td>
            <td>Somewhat</td>
            <td>Strongly</td>
        </tr>
        <tr align="center">
                <td colspan="2">Disagree</td>
                <td>Neutral</td>
                <td colspan="2">Agree</td>
        </tr>
        <tr>
            <td>1</td>
            <td>This course helped me develop problem-solving skills.</td>
            <td align="center"><input type="radio" name="q1" value="1"></td>
            <td align="center"><input type="radio" name="q1" value="2"></td>
            <td align="center"><input type="radio" name="q1" value="3" checked></td>
            <td align="center"><input type="radio" name="q1" value="4"></td>
            <td align="center"><input type="radio" name="q1" value="5"></td>
        </tr>
        <tr>
            <td>2</td>
            <td>This course was very challenging.</td>
            <td align="center"><input type="radio" name="q2" value="1"></td>
            <td align="center"><input type="radio" name="q2" value="2"></td>
            <td align="center"><input type="radio" name="q2" value="3" checked></td>
            <td align="center"><input type="radio" name="q2" value="4"></td>
            <td align="center"><input type="radio" name="q2" value="5"></td>
        </tr>

        <tr>
            <td>3</td>
            <td>My programming ability increased after aking this course.</td>
            <td align="center"><input type="radio" name="q3" value="1"></td>
            <td align="center"><input type="radio" name="q3" value="2"></td>
            <td align="center"><input type="radio" name="q3" value="3" checked></td>
            <td align="center"><input type="radio" name="q3" value="4"></td>
            <td align="center"><input type="radio" name="q3" value="5"></td>
        </tr>
        <tr>
            <td>4</td>
            <td>I will list 'Javascript' on my resume.</td>
            <td align="center"><input type="radio" name="q4" value="1"></td>
            <td align="center"><input type="radio" name="q4" value="2"></td>
            <td align="center"><input type="radio" name="q4" value="3" checked></td>
            <td align="center"><input type="radio" name="q4" value="4"></td>
            <td align="center"><input type="radio" name="q4" value="5"></td>
        </tr>
        <tr align="center">
            <td colspan="2"></td>
            <td>1</td>
            <td>2</td>
            <td>3</td>
            <td>4</td>
            <td>5</td>
        </tr>
        <tr>
            <td colspan="7" rowspan="2" align="center"><input type="submit" value="Submit"> <input type="reset" value="Reset"></td>
        </tr>
    </table>
</form>

PHP

if(isset($_POST['q1'] && isset($_POST['q2'] && isset($_POST['q3'] && isset($_POST['q4'])
 {
  $q1 = $_POST['q1'];
  $q2 = $_POST['q2'];
  $q3 = $_POST['q3'];
  $q4 = $_POST['q4'];

  $text = $q1 . "," . $q2 . "," . $q3 . "," . $q4 . "\n";
  $ret = file_put_contents('/results.txt', $text, FILE_APPEND | LOCK_EX);

  if($ret === false) {
        die('There was an error writing this file');
    }
    else {
        echo "$ret bytes written to file";
    }
}
else {
   die('no post data to process');
}

There are some small errors in your result.php, here is the fixed php code

 <?php
 if(isset($_POST['q1']) && isset($_POST['q2']) && isset($_POST['q3']) && isset($_POST['q4']))
  {
   $q1 = $_POST['q1'];
   $q2 = $_POST['q2'];
   $q3 = $_POST['q3'];
   $q4 = $_POST['q4'];

   $text = $q1 . "," . $q2 . "," . $q3 . "," . $q4 . "\n";
   $ret = file_put_contents('results.txt', $text, FILE_APPEND | LOCK_EX);

   if($ret === false) {
    die('There was an error writing this file');
     }
     else {
    echo "$ret bytes written to file";
     }
 }
 else {
    die('no post data to process');
 }
 ?>

The problem is with this line, where you have a few missing ) closing parentheses.

if(isset($_POST['q1'] && isset($_POST['q2'] && isset($_POST['q3'] && isset($_POST['q4'])

                 ----)                 ----)                 ----)                  ----)

Replace it with:

if(isset($_POST['q1']) && isset($_POST['q2']) && isset($_POST['q3']) && isset($_POST['q4']))

Plus, there may be other issues. I ( successfully ) tested this using:

$ret = file_put_contents('results.txt', $text, FILE_APPEND | LOCK_EX);

without the / in front of results.txt

  • Note : Use a relative path instead of an absolute path.

Setting file permissions to 0644 is a safer method than 0777 should you need to set your file.


Rewrite : (run your code from within the same folder as your text file)

If your file is located one level down from the executed folder, use '../results.txt' depending on the location of the executing code and text file.

<?php

if(isset($_POST['q1']) && isset($_POST['q2']) && isset($_POST['q3']) && isset($_POST['q4']))
 {
  $q1 = $_POST['q1'];
  $q2 = $_POST['q2'];
  $q3 = $_POST['q3'];
  $q4 = $_POST['q4'];

  $text = $q1 . "," . $q2 . "," . $q3 . "," . $q4 . "\n";
  $ret = file_put_contents('results.txt', $text, FILE_APPEND | LOCK_EX);

  if($ret === false) {
        die('There was an error writing this file');
    }
    else {
        echo "$ret bytes written to file";
    }
}
else {
   die('no post data to process');
}

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM