nisroc Posted October 1, 2009 Share Posted October 1, 2009 I have a forum which i wish to write text to a textfile. So far all has gone great until I added a function to what I want to do. Im not getting any errors but i am also not getting a textfile. Any help ... please The form; <form id="addlinks" name="addlinks" method="get" action="functions.php?action=addlink"> enter new link<input name="newlinkname" type="text" /> enter directorty<input name="newlinkdir" type="text" /> <input name="Save" type="submit" value="Save Changes" /> </form> The Function <?php function addLink() { $myFile = "testFile.txt"; $fh = fopen($myFile, 'w') or die("can't open file"); $stringData = $_GET['newlinkname']; fwrite($fh, $stringData); $stringData = $_GET['newlinkdir']; fwrite($fh, $stringData); fclose($fh); } ?> Quote Link to comment Share on other sites More sharing options...
mayfair Posted October 1, 2009 Share Posted October 1, 2009 I haven't come across this way of calling a function before, but im assuming it's possible? Anyway it may be because it's case sensitive. Try changing: action="functions.php?action=addlink" to action="functions.php?action=addLink" Quote Link to comment Share on other sites More sharing options...
SoN9ne Posted October 1, 2009 Share Posted October 1, 2009 Are you trying to append to the file every time or just overwrite the file? If you want to append to the file you should use a(opens file for writing, sets pointer at the end of file) for the fopen method instead of w (opens file for writing, sets pointer at beginning, truncates file to zero length). You can accomplish this a few different ways. 1. Change the form method to post 2. See below <?php $action = isset($_POST['action']) ? $_POST['action'] : ''; // Use any of theese two methods should work, use the if statement or the switch, not both... // Simple method if ($action == 'addlink') { addLink(); } // Allows scalability switch ($action) { case 'addlink': addLink(); break; } // End of two methods function addLink() { $myFile = "testFile.txt"; $fh = fopen($myFile, 'w') or die("can't open file"); $stringData = $_GET['newlinkname']; fwrite($fh, $stringData); $stringData = $_GET['newlinkdir']; fwrite($fh, $stringData); fclose($fh); } ?> It may help to take a peek at an old log class I wrote. It should help make things a little easier. http://jeremysimkins.com/downloads/logfile.txt Quote Link to comment Share on other sites More sharing options...
zq29 Posted October 1, 2009 Share Posted October 1, 2009 As far as I'm aware, you can't call functions from the query string. To do what you're trying to do, you'll need to do something like this in your functions.php file: switch(@$action) { case "addlink": addLink(); break; case "foo": foo(); break; case "bar": bar(); break; default: echo "No function called."; break; } I must say, it's an odd approach... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.