Page 1 of 1

file handling functions

Posted: Tue Aug 18, 2009 3:54 am
by bluekhille
how am i going to save and view records from a textbox to text file (notepad)?

here's my code for saving record but it only creates text file but it doesn't save the record.

in this code im only testing the name record

<html>
<body>
<form>

<form method = "POST" action = "mp05.php" action2 = "mp05.php">
&nbsp;&nbsp;&nbsp;Name
<input type = "text" name = "name"><br><br>
&nbsp;Address
<input type = "text" name = "address"><br><br>
&nbsp;&nbsp;Course
<input type = "text" name = "course"><br><br>
&nbsp;&nbsp;Contact
<input type = "text" name = "contact"><br><br>
&nbsp;&nbsp;Email&nbsp;&nbsp;&nbsp;
<input type = "text" name = "email"><br><br>

&nbsp;&nbsp;&nbsp;<input type = "submit" value = "View" name = "action">
&nbsp;&nbsp;&nbsp;<input type = "submit" value = "Save" name = "action2">



<?PHP


if(isset($_POST['action2']));
$name = $_POST['name'];
$address = $_POST['address'];
$course = $_POST['course'];
$contact = $_POST['contact'];
$email = $_POST['email'];

$file = fopen ("studinfo.txt","a");
fwrite($file,$name);
fclose($file);

?>
</form>
</body>
</hmtl>

Re: file handling functions

Posted: Tue Aug 18, 2009 6:14 am
by jackpf
Does it not write what ever you put into the name input box to the file?

Re: file handling functions

Posted: Tue Aug 18, 2009 6:30 am
by bluekhille
yeah. nothing appears on my text file

Re: file handling functions

Posted: Tue Aug 18, 2009 6:38 am
by jackpf
What?? Yes it does write the name to the text file, but nothing is written to the text file??!?? That doesn't make sense...

8O


Ok...well what does

Code: Select all

var_dump($name);
display?

Also, have you got errors turned on?

Re: file handling functions

Posted: Tue Aug 18, 2009 6:45 am
by bluekhille
jackpf wrote:What?? Yes it does write the name to the text file, but nothing is written to the text file??!?? That doesn't make sense...

8O


Ok...well what does

Code: Select all

var_dump($name);
display?

Also, have you got errors turned on?

what i mean is anything i put on the textbox doesn't appear in the textfile..
no error appears..

Re: file handling functions

Posted: Tue Aug 18, 2009 6:46 am
by jackpf
Oh right...

And what does the code I posted display?

Btw, your logic isn't completely correct, try this instead:

Code: Select all

<?PHP
 
 
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
$name = $_POST['name'];
$address = $_POST['address'];
$course = $_POST['course'];
$contact = $_POST['contact'];
$email = $_POST['email'];
 
$file = fopen ("studinfo.txt","a");
fwrite($file,$name);
fclose($file);
}
?>

Re: file handling functions

Posted: Tue Aug 18, 2009 7:04 am
by bluekhille
jackpf wrote:Oh right...

And what does the code I posted display?

Btw, your logic isn't completely correct, try this instead:

Code: Select all

<?PHP
 
 
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
$name = $_POST['name'];
$address = $_POST['address'];
$course = $_POST['course'];
$contact = $_POST['contact'];
$email = $_POST['email'];
 
$file = fopen ("studinfo.txt","a");
fwrite($file,$name);
fclose($file);
}
?>
the other code displays word NULL

then this one .. nothing happens

Re: file handling functions

Posted: Tue Aug 18, 2009 7:46 am
by jackpf
Oh I see. You've got a nested form. Remove the form tag at the top.

Re: file handling functions

Posted: Tue Aug 18, 2009 7:58 am
by bluekhille
jackpf wrote:Oh I see. You've got a nested form. Remove the form tag at the top.
i remove and it display this

string(6) "dasdwa"

Re: file handling functions

Posted: Tue Aug 18, 2009 8:32 am
by jackpf
Ok, so it should be writing that to the file "studinfo.txt".