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 3 123 LastLast
Results 1 to 15 of 40
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Posts
    553
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Distinguishing submit from refresh?

    I have a piece of code where text is being entered on a form and password entered and then a submit is done, at which point I write the data to a file, as long as the password is ok, and then I write all items in the file back out to the screen. To get the added item to show on screen, the user must do a refresh. Unfortuantely, if the user immediately does a second refresh, then the added itm gets written to the file a second time. And will keep doing so if he keeps refreshing. I would like to put a flag in the logic to say, already written to file, don't write again, and then go clear the flag at some other point. However, I really can't see where else to clear the flag - there is no way in the submit routine that I can see to detect whether this is initial submit, or a refresh. Anyone know how to handle this?

    G

  • #2
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,273
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Why is it that you need to hit refresh to get the item to show? That shouldn't be needed. As for determing whether or not an item has been submitted all that needs to be done is to give the submit button a name and check to see if that variable was set or not.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Posts
    553
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks, Spookster. Yes, the submit variable is set, but I don't think I can distinguish whether when I test it it is immediately after the submit or after the refresh after the submit. In both cases it will show the variable as set... As for why the refresh is needed, I'm not sure where I go wrong. Below is the code, with some extraneous checks left out for brevity...

    .
    .
    .
    if($HTTP_POST_VARS['submit']) {
    if($HTTP_POST_VARS['password'] == 'xxx') {
    if(!$HTTP_POST_VARS['name']) {
    echo "You must enter a name";
    exit;
    }
    $fp = fopen('items.txt','a');
    if(!$fp) {
    echo "Error opening file!";
    exit;
    }
    .
    .
    .
    $outline = etc... . $strip;
    .
    .
    .
    fwrite($filp, $outline);
    if(!fclose($filp)) {
    echo "Error closing file";
    exit;
    }
    echo "<b>Item added</b>\n";

    } else {
    echo "Invlaid password";
    }
    }

    ?>
    <FORM ACTION="<?=$PHP_SELF?>" METHOD="POST" NAME="itementry" class="textg">
    Your name:<BR>
    <INPUT TYPE="text" SIZE="30" NAME="name"><BR>
    The Item:<BR>
    <TEXTAREA NAME="items" COLS="70" ROWS="8"></TEXTAREA><BR><BR>
    Password:<BR>
    <INPUT TYPE="password" SIZE="30" NAME="password"><BR>
    <INPUT TYPE="submit" NAME="submit" VALUE="Post to File"><BR>
    </FORM>

  • #4
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,273
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Hmmm. I don't see a need for a refresh. Can you explain what this is suppose to do. You are having a person type in their name and password along with some other input and then apending that input to items.txt?
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #5
    Regular Coder
    Join Date
    Jun 2002
    Posts
    553
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Spookster,

    Yes, exactly. The contents of the file are informational items submitted by various folks.... I suppose the problem lies with the following... The display I show has a section driven by one form (the general admin part of the page), which has edit, delete, activate, etc., buttons next to each 'item' in the file, with all items in the file displayed. Then below that is the 'add item' form that I have referred to in this thread. So when the viewer does the add item, the item does not appear in the general admin display above until a refresh is done. I guess I was not thinking so clearly and failed to explain that.

    G

  • #6
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,273
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Ok I think I understand. In the general admin part are you closing the file once you have opened it? Maybe you could post the relevant code for that as well?
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #7
    Regular Coder
    Join Date
    Jun 2002
    Posts
    553
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The gist of it (the admin part) is this: For each item in the file, I write the file contents for that item and a line following like so to the browser:

    <a href=\"$PHP_SELF?action=delitem&id=$key\">Delete</a>

    (except that in addition to delete there are several other editing action options).

    Then, using the delete case above as an example, if $action == "delitem"
    I remove that item from my array and rewrite the entire file. So, yes, I do close the file after performing the chosen action.

    G
    Last edited by gorilla1; 07-20-2002 at 08:19 PM.

  • #8
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,273
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Can we see the code though?
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #9
    Regular Coder
    Join Date
    Jun 2002
    Posts
    553
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Spookster,
    I'm sorry, but I'm modifying code for someone (I didn't write the original code), so I don't feel comfortable posting alot of it... I had another idea, which was to store off the submitted text, and then at the top of the submit code, to check whether the submitted text equals the stored off, and if so I know it is a refresh and I should not write the file again... But nothing seems to be working at this point, so I think I will leave this for tonight.. Thanks for all the ideas.

    G

  • #10
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,273
    Thanks
    4
    Thanked 83 Times in 82 Posts
    If I were to see how everything else is working I could post a solution that doesn't require a refresh.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #11
    Regular Coder
    Join Date
    Jun 2002
    Posts
    553
    Thanks
    0
    Thanked 0 Times in 0 Posts
    OK, thanks, Spookster. Below I have boiled the code logic down to its essence (yet still a working version) so that it exhibits the precise problem - namely, that 1) a refresh is needed to view the updated item, and 2) consecutive refreshes will result in consecutive duplicate entries in the file, even though only one entry was intended for submission. Thanks again.

    G.

    <?

    $recin = file('items.txt');

    foreach($recin as $linein) {
    $linein = trim($linein);
    echo $linein . "</b>\n";
    echo "<BR><BR>\n";
    }


    if($HTTP_POST_VARS['submit']) {
    $filepnt = fopen('items.txt','a');
    if(!$filepnt) {
    echo "Open error";
    exit;
    }
    $lineout = $HTTP_POST_VARS['Item'];
    $lineout = str_replace("\r\n","<BR>",$lineout);
    $lineout .= "\r\n";
    fwrite($filepnt, $lineout);
    if(!fclose($filepnt)) {
    echo "Close error";
    exit;
    }
    echo "<b>added this iem</b>\n";
    }

    ?>
    <FORM ACTION="<?=$PHP_SELF?>" METHOD="POST" NAME="Itemform">
    Item:<BR>
    <TEXTAREA NAME="Item" COLS="65" ROWS="5"></TEXTAREA><BR><BR>
    <INPUT TYPE="submit" NAME="submit" VALUE="Post"><BR>
    </FORM>
    Last edited by gorilla1; 07-21-2002 at 06:00 PM.

  • #12
    Regular Coder
    Join Date
    Jun 2002
    Posts
    553
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Spookster,

    I had no success trying to avoid the refresh. Did you have a thought on it?

    G

  • #13
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,273
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Put everything as you have it now in a .txt file and post a link to it and ill take a look at it.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #14
    Regular Coder
    Join Date
    Jun 2002
    Posts
    553
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It's right there in the 07-21-2002 12:57 PM post - 23 lines of code.

    G

  • #15
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,273
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Well I put together a script using various different methods but all end up with the same problem as you originally had. This is very strange.

    It has me perlexed at the moment. I'll have to investigate it some more.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster


  •  
    Page 1 of 3 123 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
    •