hi i am a beginner of php i just want to know the code or procedure to write a data in file automatically when submit button is clicked.. for example. if i am getting a data from the user like name,age etc....after finishing the form ,if user clicks the submit button ,it should be automatically store in word pad or note pad... pls help me in this problem..
you can try this code: <? if(!$_POST['submit']) { ?> <form method="post" action=""> <input type="text" name="name" value=""> <input type="submit" name="submit"> </form> <? } else { $name = $_POST['name']; $fp = fopen("new.txt",w); if($fp != null) { fwrite($fp,$name); } } ?>
Here is the Correct Version of Code. It will create text file if file doesn't exist before. <?php if(!$_POST['submit']) { ?> <form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>"> <input type="text" name="name"> <input type="submit" name="submit"> </form> <?php } else { $name = $_POST['name']; $fp = fopen("myfile.txt","a+") or exit("unable to open the file"); if($fp != null) { fwrite($fp,$name); } fclose($fp); } ?>