Alright, I've been searching the internet for hours, so now I come to you guys for help. My code is below, but to summarize what's going on, when I use

fwrite($file, "Hello!");

it writes "Hello!" just fine. But yet if I do this:

$text = "Hello!";
fwrite($file, $text);

it won't write anything to the file at all. I've tried enclosing $text in quotes and not, and it doesn't make a difference. Here is the exact php code I am using (and yes the variables are posted to it, and if I simply print them on the page, it works).

<head>
<?php

	$date = $_POST['date'];
	$title = $_POST['title'];
	$announce = $_POST['announce'];
	$firstName = $_POST['firstName'];
	$output = "\n".$date." | ".$title." | ".$announce." | ".$firstName;
function writeIt(){
$your_data = "\n".$date."|".$title."|".$announce."|".$firstName;// Open the file and erase the contents if any
$fp = fopen("announcements.txt", "a+");// Write the data to the file
fwrite($fp, $your_data);// Close the file
fclose($fp);
}//writeIt

?>
</head>
<body onUnload="<?php writeIt(); ?>">

    grgisme wrote:

    $your_data = "\n".$date."|".$title."|".$announce."|".$firstName;

    Your mistake is right there. In a function, in order to use variable that were initialized outside the function (as in your code), you need to put global $varname1, $varname2, etc etc, at the beginning of the function. Change it to this and it should work:

    <head>
    <?php
    $date = $_POST['date'];
    $title = $_POST['title'];
    $announce = $_POST['announce'];
    $firstName = $_POST['firstName'];
    $output = "\n".$date." | ".$title." | ".$announce." | ".$firstName;
    function writeIt()
    {
    global $date, $title, $announce, $firstName;
    $your_data = "\n".$date."|".$title."|".$announce."|".$firstName;// Open the file and erase the contents if any
    $fp = fopen("announcements.txt", "a+");// Write the data to the file
    fwrite($fp, $your_data);// Close the file
    fclose($fp);
    }
    ?>
    </head>
    <body onUnload="<?php writeIt(); ?>">

    Also, what is up with that last line? You'll get this on the page:

    <body onUnload="">

    Remove that and you'll be fine.

      grgisme wrote:

      Alright, I've been searching the internet for hours, so now I come to you guys for help. My code is below, but to summarize what's going on, when I use

      fwrite($file, "Hello!");

      it writes "Hello!" just fine. But yet if I do this:

      $text = "Hello!";
      fwrite($file, $text);

      it won't write anything to the file at all. I've tried enclosing $text in quotes and not, and it doesn't make a difference. Here is the exact php code I am using (and yes the variables are posted to it, and if I simply print them on the page, it works).

      <head>
      <?php

      	$date = $_POST['date'];
      	$title = $_POST['title'];
      	$announce = $_POST['announce'];
      	$firstName = $_POST['firstName'];
      	$output = "\n".$date." | ".$title." | ".$announce." | ".$firstName;
      function writeIt(){
      $your_data = "\n".$date."|".$title."|".$announce."|".$firstName;// Open the file and erase the contents if any
      $fp = fopen("announcements.txt", "a+");// Write the data to the file
      fwrite($fp, $your_data);// Close the file
      fclose($fp);
      }//writeIt

      ?>
      </head>
      <body onUnload="<?php writeIt(); ?>">

      The problem your running into here is with your function. Variables used inside functions are considered "local" to that function unless you specify otherwise. Notice how you set all your variables before you define the function writeit(). Since the variables $date, $title, $announce are all being set outside the function when the function writeit() executes it doesn't look at the value of the variables you set outside the function. Instead it's initializing a new $date, $title, etc.. each time the function is run. There are a couple of ways to fix this. You can either pass all of these values to your function or you could declare them as global variables inside your function. Given the code you have at the moment, I think the easiest fix would be something like this:

      <?php
      
      // First we get your variables from the form
      
                  $date = $_POST['date'];
      $title = $_POST['title'];
      $announce = $_POST['announce'];
      $firstName = $_POST['firstName'];
      $output = "\n".$date." | ".$title." | ".$announce." | ".$firstName;
      
      //since we already have a variable called output, lets pass that to the function.  
      //We'll add a variable called $input that we'll use to pass data to our writeit //function function writeIt($input) { $fp = fopen("announcements.txt", "a+"); fwrite($fp, $input); fclose($fp); } //now when we call our function, and pass the $output variable we've already //created to the writeit function ?>

      That should do the trick. Hope that helps

        <body onUnload="<?php writeIt(); ?>">

        This shows a serious misconception in your approach to PHP. The onunload attribute is used with a clientside scripting language. PHP is a serverside scripting language.

        Generally, what Stepdad suggested is the right way to go.

        If you use PHP5, I would suggest:

        <?php
        if (isset($_POST['date'], $_POST['title'], $_POST['announce'], $_POST['firstName']))
        {
        	file_put_contents("announcements.txt",
        		$_POST['date'] . '|' . $_POST['title'] . '|' . $_POST['announce']
        			. '|' . $_POST['firstName'] . "\n",
        		FILE_APPEND);
        }
        else
        {
        	// one of the variables required does not exist
        	// could set some error message variable
        }
        
        // only now comes the html
        ?>

          Also, the "a" switch for "fopen()" doesn't erase any data, as your code comment says. It opens the file for appending.

            Wow Guys thanks for all the help. To clarify a couple issues there, I realize that onUnload doesn't write anything, I don't want it to. I see an issue with it now that I just realized, but it was supposed to not be writing the file until the person left the page. I'll explain my new question in another thread, so I'm not spamming. I love how easy PHP is in comparison to C and Java! Heh. Thanks for helping me out guys.

              grgisme wrote:

              ...but it was supposed to not be writing the file until the person left the page.

              The only way you would be able to do that is AJAX (or just plain JavaScript, but I don't recommend it). If you don't know JavaScript, learn that first, and then find a tutorial on AJAX. Google is your friend here.

                Write a Reply...