Jump to content

Recommended Posts

I have the following form the just has one field. 

 

<html>
    <head>
        <title>Post</title>
    </head>
    <body>
        <form action="worldpay_callback.php" method="post">
            <input type="text" name="username" id="username" />
            <input type="submit" name="submit" id="submit" value="send" />
        </form>
    </body>
</html>

 

I am then posting the form to the following page that is meant to loop through all of the post values and save them to a text file.  For some reason my foreach loop doesn't seem to be working as done of the post values are getting written to the text file.  Can anyone see anything I'm doing wrong?

 

$myFile ="notifications.txt";
$fh = fopen($myFile, 'w+') or die("can't open file");

foreach($_POST as $k=>$v)
{
   $data = 'POST: Key:'.$k.' - Value:'.$v ." \n";
   fwrite($fh, $data);
}
   
fclose($fh);

return "[OK]";

 

Thanks for any help

Link to comment
https://forums.phpfreaks.com/topic/257858-foreach-loop-not-working/
Share on other sites

The highest permissions a file should ever have are 0755.

Why do you need a loop if only one value is being sent to the server?

You do realize that opening a file in "w+" mode will truncate the file every time correct?

You should have error_reporting set to E_ALL or -1, and Display_errors set to "On", if it is a file permissions error, an error will be triggered.

A couple debugging precautions should be put in place here:

 

if(isset($_POST['submit']))
{
    $myFile ="notifications.txt";
    $username = trim($_POST['username']);
    $fh = fopen($myFile, 'w+') or die("can't open file");
    $data = 'POST: Key: ' . $k . ' - Value: ' . $v . " \n";
    $fw = fwrite($fh, $data);
    if($fw === false)
    {
        echo "failed to write to file.";
    }
    fclose($fh);
}

Something tells me that is not all of your php code. Have you even checked if the code is being executed by echoing something in it? What's all the actual php code in your file, including the opening php tags you are using?

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.