Ok this will really blow your mind now :o
I just looked at the code you posted originally and that is javascript not PHP!
Do you know how to write a form and process it?
Here is a simple (and i do mean simple!) script that will display a form for you to ‘login’ then send the data to itself for processing. (IE: put the data in a file in your case)
[php]
<?php
// check if form has been submitted
if(isset($_POST['submit']))
{
// initialise an error array
$errors = array();
// check the username is not empty
if(!empty($_POST['username']))
{
$user = $_POST['username'];
}
else
{
$user = false;
$errors[] = 'Please enter a username';
}
// check the password is not empty
if(!empty($_POST['password']))
{
$pass = $_POST['password'];
}
else
{
$pass = false;
$errors[] = 'Please enter a password';
}
// check everything is ok
if($user && $pass)
{
// path to and name of file
$file = 'xys.txt';
// check if we can open the file
if( $fp = fopen("$file", 'wb') )
{
flock($fp, LOCK_EX);
fwrite($fp, "$user\r\n");
fwrite($fp, "$pass\r\n");
flock($fp, LOCK_UN);
}
fclose($fp);
}
else // something wasn't right?
{
// show the errors (if any)
foreach($errors AS $error)
{
echo '
' . $error . '
';
}
}
}
else
{
// display the form in HTML ?>
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
Username: <input type="text" name="username" />
Password: <input type="password" name="password" />
<input type="submit" name="submit" value="Submit" />
</form>
<?php
} // close if/else statement
?>
[/php]
WOW :o I hear you say!
No fear, let’s start from the top…
this checks if the form has been submitted…
[php]
// check if form has been submitted
if(isset($_POST[‘submit’]))
[/php]
initialise an array called errors for use later (if any errors)
[php]
// initialise an error array
$errors = array();
[/php]
Check if the username box is NOT empty (notice the ! this means NOT)
if its not empty then set the variable $name with the value $_POST[‘username’].
however if it is empty the it will set $name to false and add an element to the $errors array for use later.
[php]
// check the username is not empty
if(!empty($_POST[‘username’]))
{
$user = $_POST[‘username’];
}
else
{
$user = false;
$errors[] = ‘Please enter a username’;
}
[/php]
the next section of code (not included here) does exactly the same only this time it does the password…
this part checks $user and $pass are set.( IE: not false)
[php]
// check everything is ok
if($user && $pass)
[/php]
if everything is ok, we’re good to process the data (write to a file in your case)
[php]
// path to and name of file
$file = ‘xys.txt’;
// check if we can open the file
if( $fp = fopen("$file", ‘wa’) )
{
flock($fp, LOCK_EX); // lock the file so no one else can write to it
fwrite($fp, “$user\r\n”); // write to the file
fwrite($fp, “$pass\r\n”);// write to the file
flock($fp, LOCK_UN); // unlock the file
}
fclose($fp); // close the file
[/php]
or if something went wrong and an error was set then this part will loop through the $errors array and print out each one.
[php]
else // something wasn’t right?
{
// show the errors (if any)
foreach($errors AS $error)
{
echo ‘
’ . $error . ‘
’;
}
}
[/php]
and finally…
if the form has NOT been submitted then display it:
[php]
else
{
// display the form in HTML ?>
Username:
Password:
[/php]
Copy the full code to your editor an have a play around with it, you’ll soon get the hang of whats happening