...

View Full Version : Writing IF to text file



tb582
09-02-2009, 02:29 PM
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?


<!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_handle, filesize($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>

funnymoney
09-02-2009, 02:33 PM
<!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_handle, filesize($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>

tb582
09-02-2009, 02:46 PM
Now I'm getting Parse error: syntax error, unexpected $end in which is the last 3 lines of code? how can that be?




<!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_handle, filesize($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>

_Aerospace_Eng_
09-02-2009, 03:04 PM
That isn't the code that was given to you. You took out a closing } so thats why you are getting that error.

tb582
09-02-2009, 04:49 PM
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


$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;

_Aerospace_Eng_
09-02-2009, 05:01 PM
So why the need for the text file? Why not just email the user directly?

tb582
09-02-2009, 05:02 PM
I just want to have the text file on the server as backup :)

_Aerospace_Eng_
09-02-2009, 08:07 PM
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.

$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.

tb582
09-02-2009, 08:20 PM
I was looking to just specify to PHP a single email to email too not anything fancy no database etc.

_Aerospace_Eng_
09-02-2009, 08:23 PM
Then just do something like this

$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);

tb582
09-02-2009, 08:28 PM
actually that's emailing the entire text file, I want one email per update to the text file

_Aerospace_Eng_
09-02-2009, 08:37 PM
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.

tb582
09-02-2009, 08:38 PM
Yeah I only want the body of the email to be what was just written to the text file.

_Aerospace_Eng_
09-02-2009, 09:01 PM
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.

tb582
09-02-2009, 09:05 PM
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.

Scriptet
09-03-2009, 01:29 AM
Ah, so you've finally got to recieve the post data from your old thread I see.

So you just want to e-mail the userID and user Email?



$message = "Userid: $userID User Email: $userEmail"


Then use $message as the e-mail message in your mail command.

tb582
09-03-2009, 02:46 AM
Ah, so you've finally got to recieve the post data from your old thread I see.

So you just want to e-mail the userID and user Email?



$message = "Userid: $userID User Email: $userEmail"


Then use $message as the e-mail message in your mail command.


No nooo - I got the email part working but its emailing the entire text file so old updates as well as new... So actually what I want is to only mail the UPDATE ie each time the file is written to, only that data to be sent, NOT all the data in the entire file


This is what I have so far:


<!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'];


$filename = "myfile.txt";
//first, obtain the data initially present in the text file
$ini_handle = fopen($filename, "r");
$ini_contents = fread($ini_handle, filesize($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 = "Account: $account\nAction: $action\nAction Date: $actionDate\nMediaID's: $IDs\nItem Type: $itemType\nAsset Title: $titles\nUserid: $userID\nUser Email: $userEmail \n" . $ini_contents;
if (fwrite($handle, $writestring) === false) {
echo "Cannot write to text file. <br />";
}
else {
echo "Writen to file: $writestring";
}
}

$headers = 'From: webmaster@site.com' . "\r\n" .
'Reply-To: webmaster@site.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail('person@site.com','New Content has been added to abc',$writestring,$headers);

?>

</body>
</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum