Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    New Coder
    Join Date
    Aug 2006
    Posts
    37
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Writing IF to text file

    If I take out the if statement the write to file works and it writes that string in the code, but if add the if statement back in nothing get written. How do I specify that the output from the IF statement should be written to the text file?

    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>

    <body>
    <?php
    if($_POST){
        echo 
    "The Contents of the $_POST array are shown below:";
        
    //Loop through get array
        
    foreach($_POST as $key => $value){
            
    $num++;
            echo 
    $num." Key: ".$key." Value: ".$value."<br />";
        }
        
        
    $account $_POST['account'];
        
    $action $_POST['action'];
        
    $actionDate $_POST['actionDate'];
        
    $IDs $_POST['IDs'];
        
    $itemType $_POST['itemType'];
        
    $titles $_POST['titles'];
        
    $userEmail $_POST['userEmail'];
        
    $userID $_POST['userID'];

        echo 
    "Account Name: ".$account."<br />";
        echo 
    "Action: ".$action."<br />";
        echo 
    "Action Date: ".$actiondate."<br />";
        echo 
    "IDs: ".$IDs."<br />";
        echo 
    "Item Type: ".$itemtype."<br />";
        echo 
    "Title: ".$titles."<br />";
        echo 
    "User Email: ".$userEmail."<br />";
        echo 
    "User ID: ".$userID."<br />";
    }
    else{
        echo 
    "<h1>No Values were found in the $_POST Array!</h1>";
    }

    $filename "myfile.txt";
        
    //first, obtain the data initially present in the text file
        
    $ini_handle fopen($filename"r");
        
    $ini_contents fread($ini_handlefilesize($filename));
        
    fclose($ini_handle);
        
    //done obtaining initially present data
      
        //write new data to the file, along with the old data
        
    $handle fopen($filename"w+");
            
    $writestring "text to write to file\n" $ini_contents;
            if (
    fwrite($handle$writestring) === false) {
                echo 
    "Cannot write to text file. <br />";          
            }
        
    fclose($handle);
    ?>

    </body>
    </html>

  • #2
    Regular Coder funnymoney's Avatar
    Join Date
    Aug 2007
    Posts
    364
    Thanks
    17
    Thanked 24 Times in 24 Posts
    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>

    <body>
    <?php
    if(!empty($_POST)){
        echo 
    "The Contents of the $_POST array are shown below:";
        
    //Loop through get array
        
    foreach($_POST as $key => $value){
            
    $num++;
            echo 
    $num." Key: ".$key." Value: ".$value."<br />";
        }
        
        
    $account $_POST['account'];
        
    $action $_POST['action'];
        
    $actionDate $_POST['actionDate'];
        
    $IDs $_POST['IDs'];
        
    $itemType $_POST['itemType'];
        
    $titles $_POST['titles'];
        
    $userEmail $_POST['userEmail'];
        
    $userID $_POST['userID'];

    #file writing on isset($_POST)
    $filename "myfile.txt";
        
    //first, obtain the data initially present in the text file
        
    $ini_handle fopen($filename"r");
        
    $ini_contents fread($ini_handlefilesize($filename));
        
    fclose($ini_handle);
        
    //done obtaining initially present data
      
        //write new data to the file, along with the old data
        
    $handle fopen($filename"w+");
            
    $writestring "Userid: $userID User Email: $userEmail \n" $ini_contents;
            if (
    fwrite($handle$writestring) === false) {
                echo 
    "Cannot write to text file. <br />";          
            }
    else {
    echo 
    "Writen to file: $writestring";
    }
        
    fclose($handle);


        echo 
    "Account Name: ".$account."<br />";
        echo 
    "Action: ".$action."<br />";
        echo 
    "Action Date: ".$actiondate."<br />";
        echo 
    "IDs: ".$IDs."<br />";
        echo 
    "Item Type: ".$itemtype."<br />";
        echo 
    "Title: ".$titles."<br />";
        echo 
    "User Email: ".$userEmail."<br />";
        echo 
    "User ID: ".$userID."<br />";
    }
    else{
        echo 
    "<h1>No Values were found in the $_POST Array!</h1>";
    }


    ?>

    </body>
    </html>

  • #3
    New Coder
    Join Date
    Aug 2006
    Posts
    37
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Now I'm getting Parse error: syntax error, unexpected $end in which is the last 3 lines of code? how can that be?



    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>

    <body>
    <?php
    if(!empty($_POST)){
        echo 
    "The Contents of the $_POST array are shown below:";
        
    //Loop through get array
        
    foreach($_POST as $key => $value){
            
    $num++;
            echo 
    $num." Key: ".$key." Value: ".$value."<br />";
        }
        
        
    $account $_POST['account'];
        
    $action $_POST['action'];
        
    $actionDate $_POST['actionDate'];
        
    $IDs $_POST['IDs'];
        
    $itemType $_POST['itemType'];
        
    $titles $_POST['titles'];
        
    $userEmail $_POST['userEmail'];
        
    $userID $_POST['userID'];

    #file writing on isset($_POST)
    $filename "myfile.txt";
        
    //first, obtain the data initially present in the text file
        
    $ini_handle fopen($filename"r");
        
    $ini_contents fread($ini_handlefilesize($filename));
        
    fclose($ini_handle);
        
    //done obtaining initially present data
      
        //write new data to the file, along with the old data
        
    $handle fopen($filename"w+");
            
    $writestring "Userid: $userID User Email: $userEmail \n" $ini_contents;
            if (
    fwrite($handle$writestring) === false) {
                echo 
    "Cannot write to text file. <br />";          
            }
    else {
    echo 
    "Writen to file: $writestring";
    }
      
    ?>

    </body>
    </html>
    Last edited by tb582; 09-02-2009 at 02:57 PM.

  • #4
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    That isn't the code that was given to you. You took out a closing } so thats why you are getting that error.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #5
    New Coder
    Join Date
    Aug 2006
    Posts
    37
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ahh great success - two things now, how do I add in emailing the output to a specified email, and how do I separate each item by line breaks? the br code that I tried doesn't seem to work

    PHP Code:
    $writestring "Account: $account "<br />" Action: $action "<br />" Action Date: $actionDate "<br />" MediaID's: $IDs Item Type: $itemType Asset Title: $titles Userid: $userID User Email: $userEmail \n" $ini_contents
    Last edited by tb582; 09-02-2009 at 04:58 PM.

  • #6
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    So why the need for the text file? Why not just email the user directly?
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #7
    New Coder
    Join Date
    Aug 2006
    Posts
    37
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I just want to have the text file on the server as backup

  • #8
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Your <br /> tags are strings so they need to be treated as such however if you want newlines in your text file then you should use newline characters.
    PHP Code:
    $writestring "Account: $account\nAction: $action\nAction Date: $actionDate\nMediaID's: $IDs Item Type: $itemType Asset Title: $titles Userid: $userID User Email: $userEmail \n" $ini_contents
    Where is $userEmail defined? How are you getting this value? Do you have them stored in a database? Loop through all of the records and set User Email to the value from the database and use the php mail function.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #9
    New Coder
    Join Date
    Aug 2006
    Posts
    37
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I was looking to just specify to PHP a single email to email too not anything fancy no database etc.

  • #10
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Then just do something like this
    PHP Code:
    $headers 'From: webmaster@example.com' "\r\n" .
        
    'Reply-To: webmaster@example.com' "\r\n" .
        
    'X-Mailer: PHP/' phpversion();
    mail('someaddress@blah.com','subject',$writestring,$headers); 
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #11
    New Coder
    Join Date
    Aug 2006
    Posts
    37
    Thanks
    0
    Thanked 0 Times in 0 Posts
    actually that's emailing the entire text file, I want one email per update to the text file

  • #12
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Not sure what you are asking. What do you consider an update to the text file? You mean when the file is changed or each time you write to the file? If thats the case then use the same code just after or before you write to the file, and just change the third parameter to whatever message you like.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #13
    New Coder
    Join Date
    Aug 2006
    Posts
    37
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yeah I only want the body of the email to be what was just written to the text file.

  • #14
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    I'm not seeing the issue here. Use the same mail code I gave you. Put the $message in a variable, and write just that $message to the text file.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #15
    New Coder
    Join Date
    Aug 2006
    Posts
    37
    Thanks
    0
    Thanked 0 Times in 0 Posts
    write string is the variable that you already defined, but its emailing the entire text file so old updates as well as new... and if I add the code right beneath the second echo command I do not get any emails - the only place I've had sucess is above the closing PHP tag.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •