html - How to insert web form data to csv file using php -
i trying achieve data storage in csv file through web form, tried below codes saved in "form.html", "form1.php", "formtest.csv" , 3 in same directory.
<form id="form1" name="form1" method="post" action="form1.php"> <table class="formattblclass"> <tr> </tr> <tr> <td width="68"><span>first name</span></td> <td width="215"><input class="<?=$aclass;?>" type="text" name="fn" id="fn" /></td> <td width="62"><span>last name</span></td> <td colspan="3"><input class="<?=$aclass;?>" name="ln" type="text" id="ln" size="50" /></td> </tr> <div align="center"> <input type="submit" name="submit" id="submit" value="submit" /> <input type="reset" name="reset" id="button" value="reset" /> </div> </table> </form> now form1.php script
<?php $fn = $_post['fn']; $ln = $_post['ln']; if(empty($fn) || empty($ln)) {//show form $message = 'fill in areas in red!'; $aclass = 'errorclass'; } $cvsdata = $fn . "," . $ln . "\n"; $fp = fopen("formtest.csv","a"); if($fp){ fwrite($fp,$cvsdata); // write information file fclose($fp); // close file } ?> now formtest.csv file
if run in browser , again refreshed csv file getting output below picture
how proper user inputted output??
in advance
<?php $fn = $_post['fn']; $ln = $_post['ln']; if(empty($fn) || empty($ln)) { //show form $message = 'fill in areas in red!'; $aclass = 'errorclass'; } else { $fp = fopen('formtest.csv', 'w'); $cvsdata = $fn . "," . $ln . "\n"; fputcsv($fp, $cvsdata);//use fputscv() fclose($fp); } ?>
Comments
Post a Comment