PHP, writing to text file query

Status
Not open for further replies.

NucleusKore

TheSaint
Hi
I am having some difficulty with a php script that I am attempting to write. I am a complete novice to php.

I have to make a form, and submit it. The script should add the contents to a text file in append mode. I am enclosing the code for the same, below. Can't seem to figure out what's wrong.

php file (qp.php)
---------------------------------------------------
<?php
//header('Location: index.htm');
$File = "qp.txt";
$Handle = fopen($File, 'a+');
q = $_POST['q']
$Data = "<q>q\n";
fwrite($Handle, $Data);
a1 = $_POST['a1']
$Data = "<c>a1\n";
fwrite($Handle, $Data);
a2 = $_POST['a2']
$Data = "<c>a2\n";
fwrite($Handle, $Data);
a3 = $_POST['a3']
$Data = "<c>a3\n";
fwrite($Handle, $Data);
a4 = $_POST['a4']
$Data = "<c>a4\n";
fwrite($Handle, $Data);
$Data = "\n";
fwrite($Handle, $Data);
print "Data Added";
fclose($Handle);
?>
-----------------------------------------------------------------

html file
-----------------------------------------------------------------
<html><head></head><body>
<form method="POST" action="qp.php">
Question: <input type="text" name="q">
<br/>
Answer 1: <input type="text" name="a1">
<br/>
Answer 2: <input type="text" name="a2">
<br/>
Answer 3: <input type="text" name="a3">
<br/>
Answer 4: <input type="text" name="a4">
<br/>
<input type="submit" value="Submit">
</form></body></html>
----------------------------------------------------------------------

:(
Any help will be much appreciated

I need the final output of the notepad to be like this:

<q>blah blah blah blah
<c>answer
<c>answer
<c>answer
<c>answer
 
OP
NucleusKore

NucleusKore

TheSaint
Well had to sort it out myself

*s269.photobucket.com/albums/jj44/visio159/Unismilies/38large.png

Here is the revised code I worked on:

Code:
<?php

header('Location: index.html'); 

$o0 = $_POST["o0"];
$q = $_POST["q"]; 
$o1 = $_POST["o1"];

$a1 = $_POST["a1"]; 

$o2 = $_POST["o2"];
$a2 = $_POST["a2"]; 

$o3 = $_POST["o3"];
$a3 = $_POST["a3"];

$o4 = $_POST["o4"];
$a4 = $_POST["a4"];

$a5 = "\n";

$o0 = "<q>";
if ( $o1 == y )

 echo ($c1 = "<c+>");
else
 echo ($c1 = "<c>");

if ( $o2 == y )

 echo ($c2 = "<c+>");
else
 echo ($c2 = "<c>");

if ( $o3 == y )

 echo ($c3 = "<c+>");
else
 echo ($c3 = "<c>");

if ( $o4 == y )

 echo ($c4 = "<c+>");
else
 echo ($c4 = "<c>");

$file = "qp.txt";

 

$values = "$a5 $o0$q\n $c1$a1\n $c2$a2\n $c3$a3\n $c4$a4\n"; 

 

$fp = fopen($file, "a+") or die("Couldn't open $file for writing!"); 

$numBytes = fwrite($fp, $values) or die("Couldn't write values to file!"); 

 

fclose($fp); 

echo "Wrote $numBytes bytes to $file successfully!"; 

 

?>

Code:
<html><head></head><body>
<form method="POST" action="qp.php">
Question: <br><textarea name="q" rows="10" cols="60"></textarea>
<br/>
Answer 1: <input type="checkbox" name="o1" value="y"><input type="text" name="a1">
<br/>
Answer 2: <input type="checkbox" name="o2" value="y"><input type="text" name="a2">
<br/>
Answer 3: <input type="checkbox" name="o3" value="y"><input type="text" name="a3">
<br/>
Answer 4: <input type="checkbox" name="o4" value="y"><input type="text" name="a4">
<br/>
<input type="submit" value="Submit">
</form></body></html>
 
Status
Not open for further replies.
Top Bottom