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); } ?> Link to comment https://forums.phpfreaks.com/topic/176166-function-problem/ 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" Link to comment https://forums.phpfreaks.com/topic/176166-function-problem/#findComment-928373 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 Link to comment https://forums.phpfreaks.com/topic/176166-function-problem/#findComment-928397 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... Link to comment https://forums.phpfreaks.com/topic/176166-function-problem/#findComment-928406 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.