How to show the warning after we redirect the user to the same page?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • FritzGamaliel
    New Member
    • Aug 2010
    • 3

    #1

    How to show the warning after we redirect the user to the same page?

    I have searched in google about my problem.
    But I don't get the best solution for my need.

    My Problem is:
    I create a form with TextBox Name, TextBox Message, Submit button, and it use POST method. I want form'process done in the same page.

    After the user input data to TextBox Name and TextBox Message and pressed submit button,it send data well.

    If the user input data to TextBox Message with word' length more than 100 words, it will display warning.

    But if the user click refresh or F5 or Back button, in Firefox,it will display resend confirmation. So, I add line:
    header("locatio n: thispage.php");

    to prevent the browser show resend confirmation.
    But, as the result,
    if the user input data to TextBox Message with word' length more than 100 words, it will not display warning.



    Could you help me to solve my problem for it can show warning again with your script..Thank you for your kind attention..
    Best regards,

    Fritz Gamaliel


    Here is my code:
    Code:
    <?
    if(isset($_POST['BtnShout']))
    {
    	//SEND COMMENT AND SHOW IT TO SCREEN PROCESS
    	//STEP1.GET DATA
    	$vName=$_POST["TxtName"];
    	$vMessage=$_POST["TxtMessage"];
    
    	//STPE2.COMMENT MUST < 100 WORDS
    	if(strlen($vMessage)>100)
    	{
    		$warning = "Comment must < 100 words.";
    	}
    	header("location: thispagename.php");
    }
    ?>
    
    <HTML>
    <TITLE> MyWEB </TITLE>
    <head></head>
    
    <body>
    <form action="" method="post">
    Name:
    <input type="text" name="TxtName" maxlength=6 size=20/><br>
    Message
    <textarea name="TxtMessage" cols="50" ></textarea><br>
    <input type="submit" name="BtnShout" value="shout!"/>
    </form>
    
    <?
    echo $warning."<BR>";
    ?>
    
    </BODY>
    </HTML>
    Last edited by Dormilich; Aug 25 '10, 09:10 AM. Reason: Please use [code] tags when posting code
  • zorgi
    Recognized Expert Contributor
    • Mar 2008
    • 431

    #2
    If you want to count words than use str_word_count. strlen returns olnly string length.

    Your logic is also flawed. This is what you are saying in your code:
    If length of $vMessage is less than 100 than set $warning = "Comment must < 100 words."

    And you probably want to say:
    If length of $vMessage is greater than 100 than set $warning = "Comment must < 100 words."

    Also you are executing redirection whenever your form gets submitted. You should condition that line of code somehow. Have a look at simple elseif/else if

    Comment

    • FritzGamaliel
      New Member
      • Aug 2010
      • 3

      #3
      Pardon me if I don't ask clearly. But my problem not like that.

      My Problem is:
      I create a form with PHP.
      My Form has TextBox Name, TextBox Message, Submit button.
      My Form use POST method.

      The User input data into TextBox Message then click Submit button. It will process the Form.

      Now, the user input data into TextBox Message with length > 100 words. then, click submit. In the screen, will appear "Comment must < 100 words". After that, the user click Refresh button or click Back button in the browser or click F5. In Firefox, it will rise resend confirmation. So, I add line:

      header("Locatio n:thispagename. php");

      It will prevent the browser show resend confirmation.
      But as the result, when the user input data into TextBox Message with length > 100 words, it will not show "Comment must > 100 words".

      How to fix my code? Thank you..
      Note: I want to process my form only in one page.

      Comment

      • zorgi
        Recognized Expert Contributor
        • Mar 2008
        • 431

        #4
        I am sorry but I have given you the answer. I only forgot to mention that with forms, its good practice to redirect after successful submission to avoid multiple submitting of the same data.

        The logic:

        Code:
            if(//ERRORS ON THE FORM)
            {
                /*Handle errors*/
            }else{//NO ERRORS ON THE FORM
                /*use the data and redirect*/
            }

        Comment

        • FritzGamaliel
          New Member
          • Aug 2010
          • 3

          #5
          but how to impelement your logic to my code above?

          What you mean with Form error?
          Because There is no error in my code.

          I just want the web browser not show resend confirmation if the user click F5 on keyboard or click refresh button after submit a form.
          I have solve this problem by add line:
          header("Locatio n:thispagename. php");
          and it successfull.
          But as the result, the warning not appear because of that line. Now, I just want to show it warning without replace that line...

          But, how?thank you.

          Comment

          Working...