Form Help I Dont Get It

Just For Starters Ive Never Seen PHP Before, And It Seems Very Complicated, Here Is My Problem

I Have Two Files, Index.htm And Write.php.htm And In One Of The Form Sections Of Index It Has Username And Password, And I Want To Be Able To Write The Data Put In There, Into A File Of The Same Directory. Here Is The Form Section Of Index.htm

[php]

Login:
Password:
Check this box to remember login
Login Now!
[/php]

I Also Found This At The Top If It Helps?
[php] function SetFocus(){
/* If the username field is empty, it gets focus. If the password field is empty but username is filled,
it gets focus. Returns false to prevent submit if a field is empty. */
var Username = document.getElementById(‘username’);
var Password = document.getElementById(‘password’);
if ( Username.value == ‘’){
Username.focus();
return false;
}
if ( Password.value == ‘’){
Password.focus();
return false;
}
return true;
}[/php]

I’m sorry but I don’t really understand your question

I Want To Be Able To Write The Data Put In There, Into A File Of The Same Directory.

:confused: Probally Because im A Proper Begginer, But Basicly Anything Wrote In The Form I Want A Way To Extract It, For Example Anything Wrote In The Form Goes Into A .txt Document. Sorry…

Can Anyone Help Me?

when the form has been submitted, fetch the data in the post arrays then write them to a file, here’s a little code that will write the username and password in a file.
It should be placed in write.php (not write.php.htm) as you mentioned.

Here is the code:
[php]
// set user & password
$user = $_POST[‘username’];
$pass = $_POST[‘password’];

// path to and name of file
$file = ‘path/to/file/here/nameFile.here’;
// 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);
[/php]

Hope this helps you on your way (don’t forget to edit the path and name of file BEFORE using the code or it will throw an error)
:wink:

:confused: Im Sorry To Annoy All You Experts Again, Just I Have No Clue What To Do…

// set user & password
$user = $_POST[‘username’];
$pass = $_POST[‘password’];

// path to and name of file
$file = ‘xys.txt’; //file i created in the same directory as this file
// 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);

Were Do I Put That? Whats An Array? EHH

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
:wink:

Sponsor our Newsletter | Privacy Policy | Terms of Service