Clearing Form after submit

what about this part?

I don’t know. I typically avoid header redirects unless absolutely necessary. In any case, wouldn’t it be only a matter of having the correct code flow logic in place, using conditional checks and variable assignments as needed?

1 Like

Take a minute off from posting and learn PRG and then try it. Heck, You dont even have to learn it. I gave you the code. I gave you all you need to know to get going writing a single page Php form.

To be honest I am happy with the way i have implemented it, i was mostly questioning your assertion that using a post variable will fail in certain circumstances as you provide no detail as to why or what those circumstances are. If you are going to state something is the wrong way to do something you should really say why so we can decide if we want to take that advice or not.

I agree with @Mittineague and don’t need to use any header redirects as my code flows in a certain way that i can turn on and off bits as needed depending on the validation.

Well I was able to resolve this by just adding conditional below each input field which on submission inserts value="" for each input

In some IE browers, the if(isset($_POST[...])) form submission checking will fail because these are older IE versions. You have to support all browsers if you are going to make a good website. And it’s sad to say, companies still use IE if you’re going to ask why.

if(isset($_POST[...])) is also an amateur hack. The original way to check form submission was always if($_SERVER['REQUEST_METHOD'] == 'POST'). if(isset($_POST[...])) was a quick a dirty way of doing it and many newbies found this easier to remember and do rather than the legitimate way.

Like I always say, “Simplicity isn’t always good.”

A person cannot submit a form twice in the same second. And you can achieve what you are asking for using $_SESSION.

1 Like

I still use IE, I’m used to where everything is. And every time I start Chrome, it takes about two minutes to actually open up, for some reason, and longer if I haven’t disabled developer add-ons.

Back on the $_SERVER['REQUEST_METHOD'], presumably when the same piece of code handles input from multiple forms, is the way to decide which form was submitted to use a hidden input field? I have some code where I’ve done it that way, and while I could split all the handling out into separate physical files it does clutter the directory up a bit. (Two apologies: first for dragging this a little off-topic, and second because I have a feeling I’ve asked this before and received an answer).

Well. Preferably, you’d want to set the action to a different file so when the form submits, you won’t get directed to the same file that may rely on a hidden field to determine what action the form takes.

You can have multiple forms on the page (which I presume you already know), however, to keep them from colliding and for organization purposes, separateing them into different files would be a wise choice.

1 Like

cool thanks for clarifying. I don’t mind learning and changing my ways if there is a reason but it is frustrating if someone says its wrong and won’t say why.

intregued as to why if(isset($_POST[…])) would fail in IE but i guess that is a question for another day.

thanks

1 Like

IIRC the reason has to do with click vs. enter. Both will submit the form.

The problem (that may no longer be a problem, not tested recently) is that older versions of IE at least, did not (always?) send type “submit” as part of the POST array, but only the form’s inputs.

So while other browsers did send type “submit” as part of the POST array, testing for isset($_POST[‘submit’]) could fail for those IE users while testing for $_SERVER[‘REQUEST_METHOD’] == ‘POST’ would not fail regardless of what browser.

3 Likes

ah ok that makes sense. For some reason i always gave my button a name attribute and would check that. E.g

<input type="submit" name="add" id="button" value="Continue">

if(isset($_POST['add']))

I don’t know why i got into the habit of this but i never had a report of a problem or found one myself so i wonder if this by chance avoided the problem by forcing the variable to be sent/set for the submit button as it was given a name?

thanks

1 Like

I found that my code is so messy. I keeps going from html to php code and it is really hard to read. I have lines of php code under each input field which is really messy. For example I have something like this…

$nickname   = $_POST['nickname'];  /* This is at the top of the file along with other variables for each input. */


<input type="submit" name="nickname" id="someid"  
       <?php
                $r = '/^[a-zA-Z]{1,20}$/';  
                $test = preg_match($r, $nickname);
                       if (!$test && $_POST['nickname']) {  
                           $ok = false;
                              if(!$ok) {
                                echo ' value="'.$_POST['nickname'].'"';  /* print to input field. */
                                echo ' /> </div>';
                                echo '<div id="nickname_error ">';
                                echo '<label style="color:#FF0000;font-size: 0.8em">Error Found</label>';
                                echo '<div style="color:#FF0000;font-size: 0.8em">This field is required. Only letters. .</div>'; 
                                echo '</div>';
                              }
                        }
                        else  {
                           echo ' value="" /> '; 
                           
                        }
        ?>
      

/>

although this code is messy it does work but what I want to do is clean this all up and put it at the top of the file. Only php code within HTML should be something like this…

<input type="submit" name="nickname" id="someid"  value="php code here" />

How do I move this around. Each field has its own error message btw…

Move all the calculations to the start of the code, and just output the values inside your html. Where you have echo statements in that code, you could assign it to a single variable and just echo that.

$nicknamecustom = "";
$r = '/^[a-zA-Z]{1,20}$/';  
$test = preg_match($r, $nickname);
if (!$test && $_POST['nickname']) {  
  $ok = false;
  if(!$ok) {
    $nicknamecustom .= ' value="'.$_POST['nickname'].'"';  /* print to input field. */
    $nicknamecustom .=  ' /> </div>';
    $nicknamecustom .= '<div id="nickname_error ">';
    $nicknamecustom .= '<label style="color:#FF0000;font-size: 0.8em">Error Found</label>';
    $nicknamecustom .= '<div style="color:#FF0000;font-size: 0.8em">This field is required. Only letters. .</div>'; 
    $nicknamecustom .= '</div>';
    }
  }
  else  {
    $nicknamecustom .= ' value="" /> '; 
    }

... and then ...

<input type="submit" name="nickname" id="someid"  
<?php
echo $nicknamecustom;
?>

then repeat for each different field that requires it.

ETA: not sure of the reason for this bit:

 $ok = false;
 if(!$ok) {

There’s probably many “better” ways to do this, perhaps changing your error message divs between visible and invisible.

2 Likes

How would you handle echo inside input field to print empty in case form is submitted and how do you show different error for each field?

Use a different variable for each field, and display the appropriate one.

Sorry, not sure what you mean by that.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.