samedi 20 juin 2015

How to store php form data to a web server in a plain text file?

I have an assignment that calls for data entries from a php form to be stored in a plain text file on the same web server. I created the php form page and the plain text file but I am unsure how to connect the two. I've searched the internet and tried multiple ways for about two hours now and no dice. The data entries have to accumulate in the plain text file too (1st person submits, and a 2nd person submitting can see 1st person's submission and so on).

I didn't add any of the code I've tried to the plain text file because none of them were working and I wanted to (in theory) simplify the process of not having to try and fix the code. I know that the plain text file needs some sort of code to retrieve the code from the php form but unsure what to try at this point. And yes I wrote permissions for the plain text file to be writable via FileZilla.

Here's my php form code:

<!DOCTYPE HTML> 
<html>
<head>
<title>Roy Feedback Form Assignment 7</title>
<style>
.error {color: #FF0000;}
</style>
</head>
<body> 

<?php
// define variables and set to empty values
$nameErr = $emailErr = $commentErr = $likesErr = $howErr = $rateErr = "";
$name = $email = $comment = $likes = $how = $rate = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
 $nameErr = "Name is required";
} else {
 $name = test_input($_POST["name"]);
}


$email = test_input($_POST["email"]);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format"; 
}

if (empty($_POST["comment"])) {
 $commentErr = "Comments are required";
} else {
 $comment = test_input($_POST["comment"]);
}

if (empty($_POST["likes"])) {
 $likesErr = "Things you liked is required";
} else {
 $likes = test_input($_POST["likes"]);
}

if (empty($_POST["how"])) {
 $howErr = "How you got to our site is required";
} else {
 $how = test_input($_POST["how"]);
}

 if (empty($_POST["rate"])) {
 $rateErr = "Rating our site is required";
} else {
 $rate = test_input($_POST["rate"]);
}
}

function resetForm($form) {
$form.find('input:text, input:password, input:file, select, textarea').val('');
$form.find('input:radio, input:checkbox')
     .removeAttr('checked').removeAttr('selected');
}

function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?> 

<h2>Roy Feedback Form Assignment 7</h2>
<p><span class="error">* required field.</span></p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 

Name: <input type="text" name="name">
<span class="error">* <?php echo $nameErr;?></span>
<br><br>

E-mail: <input type="text" name="email">
<span class="error">* <?php echo $emailErr;?></span>
<br><br>

Comment: <textarea name="comment" rows="5" cols="40"></textarea>
<span class="error">* <?php echo $commentErr;?></span>
<br><br>

Things you liked:
<input type="radio" name="likes" value="Site design">Site design
<input type="radio" name="likes" value="Links">Links
<input type="radio" name="likes" value="Ease of use">Ease of use
<input type="radio" name="likes" value="Images">Images
<input type="radio" name="likes" value="Source code">Source code
<span class="error">* <?php echo $likesErr;?></span>
<br><br>

How you got to our site:
<input type="radio" name="how" value="Search engine">Search engine
<input type="radio" name="how" value="Links from another site">Links from another site
<input type="radio" name="how" value="Deitel.com website">Deitel.com website
<input type="radio" name="how" value="Reference from a book">Reference from a book
<input type="radio" name="how" value="Other">Other
<span class="error">* <?php echo $howErr;?></span>
<br><br>

Rate our site:
<select name="rate">
<option value="">- Please Select -</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
<span class="error">* <?php echo $rateErr;?></span>
<br/><br/>

<input type="submit" name="submit" value="Submit"> 
<input type="reset" value="Reset">

</form>

</body>
</html>

And here's the plain text file code:

<!DOCTYPE html>
<html>
<head>
    <title>Roy Feedback Results Assignment 7</title>
</head>
<body>



</body>
</html>

Aucun commentaire:

Enregistrer un commentaire