Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Closed Thread
 
LinkBack Thread Tools Display Modes
Old 03-05-2009, 06:30 PM   #1 (permalink)
TheSaint
 
NucleusKore's Avatar
 
Join Date: Jun 2004
Location: Antigua
Posts: 3,447
Default PHP, writing to text file query


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
__________________
http://www.neville.in
http://www.linuxrocks.in
"The Future Is Open"
NucleusKore is offline  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 03-05-2009, 11:46 PM   #2 (permalink)
TheSaint
 
NucleusKore's Avatar
 
Join Date: Jun 2004
Location: Antigua
Posts: 3,447
Default Re: PHP, writing to text file query

Well had to sort it out myself



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>
__________________
http://www.neville.in
http://www.linuxrocks.in
"The Future Is Open"
NucleusKore is offline  
Old 03-05-2009, 11:55 PM   #3 (permalink)
Wise Old Owl
 
j1n M@tt's Avatar
 
Join Date: Jun 2007
Location: Kochi
Posts: 1,119
Default Re: PHP, writing to text file query

nice try...this is hw u learn, trial and error
__________________
j1n M@tt || "a guy who gets off bed only b'coz of technology..."
j1n M@tt is offline  
Old 04-05-2009, 06:52 AM   #4 (permalink)
TheSaint
 
NucleusKore's Avatar
 
Join Date: Jun 2004
Location: Antigua
Posts: 3,447
Default Re: PHP, writing to text file query

Yes, took me six hours but it was worth it
__________________
http://www.neville.in
http://www.linuxrocks.in
"The Future Is Open"
NucleusKore is offline  
Old 07-05-2009, 10:45 PM   #5 (permalink)
Wise Old Owl
 
The Unknown's Avatar
 
Join Date: Nov 2006
Location: Pune, Maharashtra, India
Posts: 1,728
Default Re: PHP, writing to text file query

Quote:
Originally Posted by NucleusKore View Post
Yes, took me six hours but it was worth it
I've also had many such idiotic errors
__________________
KDE on ArchLinux
PHP, MySQL, PostgreSQL, Linux, Apache; Message me to hire (freelancing only)
Explore Technology @ http://www.itech7.com
Cheap and Reliable VPS Hosting @ http://j.mp/arHk5e
The Unknown is offline  
Closed Thread

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


 
Latest Threads
- by Sujeet
- by gforz
- by soumya

Advertisement




All times are GMT +5.5. The time now is 03:12 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2