PDA

View Full Version : help in php coding


*GandaBerunda*
24-09-2007, 05:09 PM
i am having a textbox and a button on my website..i want whatever the user enters in da box to be saved to a textfile on the server...how can i do so?
after writing it should automatically redirect to another address....can someone please guide me?
i think i can do this by opening a php file using "OnClick" and then redirecting thorugh the php file after the data has been written.....can someone please give me the complete code?
this is the code tat i get when i click on the button in ms expression web
<input name="Button1" type="button" value="Sign in">
and the name of the textbox is text1

pleaze give me the entire code if i want to write the text to a file in the dir
http://xxxx.xxxx.com/text.txt

Sukhdeep Singh
24-09-2007, 05:27 PM
No need to open nother account. You could have posted in your old topic regarding problems you faced :)
http://www.thinkdigit.com/forum/showthread.php?t=68950

[xubz]
24-09-2007, 05:36 PM
Heh :p

Coded this in 15mins, So Post back if you get any Errors

3 Files
1) test.html
2) test.php
3) text.txt

test.html
<form action="test.php" method="post">
<input type="text" name="tbName" /><br />
<input type="submit" name="btnSubmit" value="Submit" />
</form>

test.php
<?php
//Check if the Form is Posted
if (isset($_POST))
{
$success = false;

//Validate the Fields
if ((isset($_POST['tbName']) && $_POST['tbName'] != '') &&
(isset($_POST['btnSubmit']) && $_POST['btnSubmit'] == 'Submit'))
{
$fcontents = file_get_contents('./text.txt'); //Get the Contents of the File
if (is_writable('./text.txt')) //Check if the File is Writable
{
//Open the File and fput the contents. (Don't forget to add CR+LF)
$fp = @fopen('./text.txt', 'w');
@fputs($fp, $fcontents);
@fputs($fp, $_POST['tbName']);
@fputs($fp, "\r\n");
@fclose($fp);

$success = true; //Set Success Boolean to true
}
else
{
echo 'File not Writable';
}

//If Success is true, then Redirect to a Different Page
if ($success)
{
$redir_page = './test.html'; //Can be set to http://gmail.com/, etc.
header('Location: ' . $redir_page);
}
}
else
{
echo 'Please Fill all the Fields';
}
}
?>

text.txt
Data
-----

(Don't forget to add a blank like at EOF)


The script maybe a little inefficient, but it can be modified :)

(Yeh! I had some time to waste :( )