file handling functions

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
bluekhille
Forum Commoner
Posts: 25
Joined: Thu Jul 16, 2009 6:49 am

file handling functions

Post 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>
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: file handling functions

Post by jackpf »

Does it not write what ever you put into the name input box to the file?
bluekhille
Forum Commoner
Posts: 25
Joined: Thu Jul 16, 2009 6:49 am

Re: file handling functions

Post by bluekhille »

yeah. nothing appears on my text file
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: file handling functions

Post 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?
bluekhille
Forum Commoner
Posts: 25
Joined: Thu Jul 16, 2009 6:49 am

Re: file handling functions

Post 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..
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: file handling functions

Post 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);
}
?>
bluekhille
Forum Commoner
Posts: 25
Joined: Thu Jul 16, 2009 6:49 am

Re: file handling functions

Post 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
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: file handling functions

Post by jackpf »

Oh I see. You've got a nested form. Remove the form tag at the top.
bluekhille
Forum Commoner
Posts: 25
Joined: Thu Jul 16, 2009 6:49 am

Re: file handling functions

Post 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"
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: file handling functions

Post by jackpf »

Ok, so it should be writing that to the file "studinfo.txt".
Post Reply