Writing input fields to a .txt file not working

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • LugNut29
    New Member
    • Jul 2013
    • 16

    #1

    Writing input fields to a .txt file not working

    Hi All,

    I am trying to write the name="fname" & name="age" to a .txt file, but it's not writing those two things to the file.


    Code:
    <?php echo '<?xml version="1.0" encoding="IUTF-8"?>'; ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta name="generator" content="HTML Tidy for Linux (vers 25 March 2009), see www.w3.org" />
    <link rel="stylesheet" type="text/css" href="css/styles.css" />
    <script type="text/javascript" src="js/catalog.js"></script>
    <title>Catalog</title>
    </head>
    <body>
    <form id="frm1" name="myForm" onsubmit="return (validateForm() && checkRadios())" action="thankyou.php" 
    
    method="post">First name: <input type="text" name="fname" />  Age: <input type="text" name="age" />  <input 
    
    type="submit" name="submit" value="Submit" /> <input type="button" onclick="formReset()" value="Reset form" />
    <br />
    </body>
    </html>
    
    <?php
    $iName = $_POST['fname'];
    $pAge = $_POST['age'];
    $data = "$iName | $pAge\n";
    
    $fh = fopen("db\orders.txt", "a");
    fwrite($fh, $data);
    
    fclose($fh);
    ?>
    Code:
    <?php
    $iName = $_POST['fname'];
    $iAge = $_POST['age'];
    $data = "$iName | $iAge\n";
    
    $fh = fopen("db\orders.txt", "a");
    fwrite($fh, $data);
    
    fclose($fh);
    Thanks,
    T
  • Luuk
    Recognized Expert Top Contributor
    • Mar 2012
    • 1043

    #2
    rewrite this:
    Code:
    $fh = fopen("db\orders.txt", "a");
    fwrite($fh, $data);
    to:
    Code:
    $fh = fopen("db\orders.txt", "a");
    if ($fh) { 
      fwrite($fh, $data);
    } else {
     echo "could not open file...";
    }
    just to make sure there are sufficient rights for opening this file.....
    Last edited by Luuk; Aug 16 '13, 03:32 PM. Reason: code edit...

    Comment

    Working...