hi. currently, i feed variables into my php file from another file.
index.php --> feeds --> guestbook.php with the neccessary entries.

so when i reach guestbook.php, the variables are printed out onto the page. when i reload guestbook.php, the same set of variables is processed even though i did not specify it to and it appends to the bottom of the page, with the original existing variables.

is there a way to stop the re-processing of the variables?

for more info, head on to hosted.barrysworld.net/donoe/index.php, enter some stuffs, and reload the guestbook.php. thanks in advanced!

    Can I see a copy of the PHP code for both the index.php and guestbook.php? I will need to see the script if I am to help you.

      What your talking about is the dreaded "repost" problem with forms. Search this forum for "repost". This problem comes up every few days.

      -- Rich

        code goes below:

        $filename = "guestbook.txt";
        $guestbook = fopen($filename,"a+");
        fwrite($guestbook,"\n");
        fwrite($guestbook,$name);
        fwrite($guestbook,"&%");
        fwrite($guestbook,$email);
        fwrite($guestbook,"&%");
        fwrite($guestbook,$entry);
        fclose($guestbook);

        $guestbook = fopen($filename,"r");
        $buffer = fgets($guestbook,4096);
        while(!feof($guestbook)) {
        $buffer = fgets($guestbook,4096);
        list($name,$email,$entry) = split('&%',$buffer,3);
        echo "<br>name: $name<br>";
        echo "email: <a href='mailto:$email'>$email</a><br>";
        echo "comment: $entry<br>";
        }
        fclose($guestbook);

        for php experts, this script is done from scratch by a newbie, and i find more satisfaction churning out my own scripts, though others may have already done it. anyhow, please advice. thanks. 🙂

          Hmm... I've never encountered this reposting problem... though there is a couple easy ways to get around it. Here's an example:

          First add some kind of validation to make sure the user doesn't submit an empty form like this (or some variation):

          if ($user_name=='' or $user_email=='' or $user_info=='') {
          echo 'Please fill in all the fields.
          <a href="javascript:history.go(-1)">Click here to go back</a>';
          }
          else {
          // code to insert informtion into database or write to file...
          }

          Now, after your done using the variables, at the end of your page, vear the variables like so:

          $user_name = '';
          $user_email = '';
          $user_info = '';

          That way, if you refreash, you'll get the error instead of it writing out the information for a second time... At the same time you stop people from submitting empty forms.

          If you don't want error message to appear when you refresh, you could set the variables to something else you can check, and then leave no error message. For Example, put this instead at the end of the script :

          $user_name = 'foo';
          $user_email = 'foo';
          $user_info = 'foo';

          And then for the validation section at the top, check for 'foo' in the variables, and if it is there, don't execute the script for placing the information on the page (in a database). Hopefully, this doesn't sound too confusing ;/

          • Jonathan Summers

            You try the validate first idea in my other post or you could always seperate the sections of code so that you have the file writing in one file and the file reading in the other. Then use a JavaScipt (or other means) to bind the two so that after writing to the file it automatically loads the second file with the file reading section.

            This JavaScript would do just that:

            <Script Language="JavaScript">
            <!-- hide from old browsers
            window.location='secondpage.php'
            -->
            </Script>
            <?
            // if really, really, old browser without javascript...
            echo "<a href='secondpage.php'>Click here to view Guest Book";

            There are many other ways too.... I don't know of how to stop the problem of it keeping the variables in memory, but these are work-arounds you can use for now.

            Hope this help,
            Jonathan Summers

              There seems to be a problem. Though i have set the validation script to check for the 'foo', each time i refresh the page, the variables that are stored seemed to have been lost. therefore it doesnt seem to go pass the if ($user_name == 'foo') part, because $user_name have not been set if the script is refreshed... i hope im on the right track. please advice jonathan. 🙂

                Sorry, I misled you a bit. Validation, though you should have that too, won't solve the problem. Plan B, cheating with JavaScript page redirection, will stop the problem by keeping the writing portion and the reading portion and seperate pages. Though it would be nice to fix the problem without resorting to JS, it can work for now. If you search around the 'net, you might be able to find a better solution. Here's what I did (you can find this working example at summers.dhs.org/scripts/php/test/guestbook.php):
                -------- guestbook.php -----------

                <head><title>The Guestbook!</title>
                <body>
                <?php

                if ($page!=2) {

                // login form

                echo '
                <form method="post" action="guestbook.php?page=2">
                name <input size="25" name="name"><br>
                email <input size="25" name="email"><br>
                entry <input size="25" name="entry"><br>
                <input type="submit" name="Submit" value="done">
                </form>';
                }
                else {
                // validate information

                if (username!='') {

                // write data to file

                $filename = "guestbook.txt";
                $guestbook = fopen($filename,"a+");
                fwrite($guestbook,"\n");
                fwrite($guestbook,$name);
                fwrite($guestbook,"&%");
                fwrite($guestbook,$email);
                fwrite($guestbook,"&%");
                fwrite($guestbook,$entry);
                fclose($guestbook);
                }
                else {
                echo ('You must enter a name!');
                }
                // JavaScript page change
                echo "
                <script language='JavaScript'>
                <!--
                window.location='guestbook2.php?name=$name&email=$email&entry=$entry'
                --></script>";
                }
                ?>
                </body>


                --------- guestbook2.php ---------

                <body>
                <?php
                // read data from file and display

                $filename = "guestbook.txt";
                $guestbook = fopen($filename,"r");
                $buffer = fgets($guestbook,4096);
                while(!feof($guestbook)) {
                $buffer = fgets($guestbook,4096);
                list($name,$email,$entry) = split('&%',$buffer,3);
                echo "<br>name: $name<br>";
                echo "email: <a href='mailto:$email'>$email</a><br>";
                echo "comment: $entry<br>";
                }
                fclose($guestbook);
                ?>
                </body>

                  hmm, from your code i understand that it seems like the only way to get out of this, is to get to another page, so that the previous variables wont be fed into again...

                  jon, do you think using session_register would help? i read up on it, but cant seem to be able to implement it...

                    Opps, there were a couple errors in the guestbook.php file... here is the revised source:

                    <head><title>The Guestbook!</title>
                    <body>
                    <?php
                    // login form

                    echo '
                    <form method="post" action="guestbook.php?page=2">
                    name <input size="25" name="name"><br>
                    email <input size="25" name="email"><br>
                    entry <input size="25" name="entry"><br>
                    <input type="submit" name="Submit" value="done">
                    </form>';

                    // validate information

                    if ($name!='') {

                    // write data to file

                    $filename = "guestbook.txt";
                    $guestbook = fopen($filename,"a+");
                    fwrite($guestbook,"\n");
                    fwrite($guestbook,$name);
                    fwrite($guestbook,"&%");
                    fwrite($guestbook,$email);
                    fwrite($guestbook,"&%");
                    fwrite($guestbook,$entry);
                    fclose($guestbook);

                    // JavaScript page change
                    echo "
                    <script language='JavaScript'>
                    <!--
                    window.location='guestbook2.php?name=$name&email=$email&entry=$entry'
                    --></script>";
                    }

                    ?>
                    </body>

                      I dont know much about the session functions... I've always used cookies to track people. It might help... I'll have to read up on it. If I find anything better that will work, I'll post it on this forum.

                        right now i'll modify the code so that there are two pages. thanks for your help jonathan 🙂

                          Write a Reply...