Jump to content

Function problem


nisroc

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.