Jump to content

Recommended Posts

Hi, I have a school project and I would like to create a simple website (very simple) to ask some survey questions and record the answers into a text file. Could someone show me sample code that will present the question

"Please discuss your best experience during the course:" presents a text box to provide the person a chance to respond and then appends the response to a file "experiences.txt" when the person selects a submit button.

 

I have to do about 25 questions and will write the code and post an example of what I think it has to do. I just need some help getting started. I will be researching this for the next couple of weeks, anyone who can start pointing me in the right direction your help is greatly appreciated. If you feel you would be doing too much of my work for me, please point me toward a tutorial or guide document where I might be able to figure this out on my own.

 

Thank you for the help!

Mike

Link to comment
https://forums.phpfreaks.com/topic/211650-totally-new-to-php/
Share on other sites

Hi HopelessMike,

 

Actually creating your survey page (the page which presents all of the questions to the user) isn't a matter of PHP coding.  If you aren't familiar with HTML, look into this explanation of creating forms.

 

Direct the 'action' of your form to somepage.php, which will contain the actual PHP code for the script.  Using the $_POST superglobal array in somepage.php, you can get all of the information they submitted through the form.  Run this array through a simple foreach loop (which will take each field one by one so you can extract the info) and write that to your text file.

 

Something like this should get you started:

 

<?php

$writeline = "";
$handle = fopen("user_data.txt", "a+");

foreach ($_POST as $field => $value) {
   $writeline .= $field . ": " . $value . " / ";
}

$writeline .= "\n";

fwrite($handle, $writeline);
fclose($handle);

?>

 

This function will loop through all of the fields from the form that was submitted and create a string of text in the format of:

 

Name: Peter / Age: 25 / Occupation: Designer

 

It will then add this string of text to 'user_data.txt' (located in the same directory as the script).  This file will contain ALL submissions of the form, each individual submission on a new line.

 

You might also like to check the PHP manual for documentation on file functions.

 

Filesystem functions at PHP.net

 

Hope this helps,

Theo

Link to comment
https://forums.phpfreaks.com/topic/211650-totally-new-to-php/#findComment-1103344
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.