Page 1 of 1

form problem

Posted: Tue Apr 15, 2008 12:39 pm
by hsub
Hello everyone,
I have this simple form which has three names available to choose from. Depending on which option is chosen, what is submitted gets written to a specific text file. There are 3 text file.
here is what i have. maybe someone could shed some light onto something i am missing.
just to let you know, the permission on the text files are adjusted to 777 and i have tested them separately with a simple script which writes a string of text to each. that's not the problem, i think the problem lies in the structure of the code.

I basically get the die error return 'can't open file'

Code: Select all

<?php
if ($_POST['message'] != "") {
    switch ($_POST['name']) {
        case 'Jamil':
            $file = 'message.txt';
            break;
        case 'Razy':
            $file = 'message1.txt';
            break;
        case 'Stephanie':
            $file = 'message2.txt';
            break;
    }
}
?>
<?php
if (!function_exists('file_put_contents')) {
    function file_put_contents($file, $contents)  {
        $fh = fopen($file, 'w') or die("can't open file");
 
        if(!$fh) {
            trigger_error('file_put_contents cannot write in file.', E_USER_ERROR);
            return;
        }
 
        fwrite($fh, $contents);
        fclose($fh);
    }
}
 
if (file_put_contents($file, htmlspecialchars(stripslashes($_POST['message'])))) {      
    echo "Your message was recorded";
} else {
    echo "An error occurred";
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method='post'>
<p class="style2">
    <select name="name">
        <option>Jamil</option>
        <option>Razy</option>
        <option>Stephanie</option>
    </select>
    &nbsp;
    Message Updater:
</p>
<p class="style2">
    <input type="text" name="message" />
</p>
<p class="style2">
    <input type="submit" Value="Build"/>
</p>
</form>

Re: form problem

Posted: Tue Apr 15, 2008 12:59 pm
by andym01480
It is kind of complicated structure wise!
Note if someone enters a name other than the ones you are looking for your script and mine would attempt to open the file the user named which would be dangerous!!!

Code: Select all

<?php
if ($_POST['message'] != "") {
    switch ($_POST['name']) {
        case 'Jamil':
            $file = 'message.txt';
            break;
        case 'Razy':
            $file = 'message1.txt';
            break;
        case 'Stephanie':
            $file = 'message2.txt';
            break;
    }
 
$fh = fopen($file, 'w') or die("can't open file");
$contents=htmlspecialchars(stripslashes($_POST['message']));
if(fwrite($fh, $contents))
 {
echo "Message recorded";
}else echo "An error occurred";
fclose($fh);
exit();
}
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method='post'>
<p class="style2">
    <select name="name">
        <option>Jamil</option>
        <option>Razy</option>
        <option>Stephanie</option>
    </select>
    &nbsp;
    Message Updater:
</p>
<p class="style2">
    <input type="text" name="message" />
</p>
<p class="style2">
    <input type="submit" Value="Build"/>
</p>
</form>

Re: form problem

Posted: Tue Apr 15, 2008 1:27 pm
by hsub
andy...
this works quite good.
thanks a great deal

if i limit the selections to the three the 3 options, there shouldn't be any problems?