Get rid of form values after submitting

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
clonemaster
Forum Newbie
Posts: 12
Joined: Sun Aug 02, 2009 1:10 pm

Get rid of form values after submitting

Post by clonemaster »

Hi all,
I have a form but when the message is submitted, I'd like to clear the fields (this is working) but also reload the page without the values form the fields, which is now behind the ? of the URL
This is what I have:

Code: Select all

 <?php
  $amount=15;

  if ($_GET['action']=='write') {
      header('location: gasten.php?action=read&start=0');
    $file=fopen('gastenboek.txt','a');

    $message=str_replace("\r",'',$message);
    $message=str_replace("\n",'{{',$_GET['message']);
    fwrite($file,$_GET['name'].'|||'.$_GET['email'].'|||'.date('d-m-Y').'|||'.$_GET['url'].'|||'.$message."\n");
    fclose($file);
    
  }

  $start=(isset($_GET['start'])?$_GET['start']:0);
  $gastenboek=Array();
  $gastenboek=file('gastenboek.txt');

?>

  <SCRIPT language="JavaScript">
    function validate(form) 
  {
      if (form.name.value=="") 
    {
      alert("Vul je naam in");
      return false;
    } 
    else if (form.message.value=="") 
    {
      alert("Vul je bericht in");
      return false;
    } 
    else if (form.url.value=="") 
    {
      return true;
    } 
    else 
    {
     return false;
    }
  }
                </SCRIPT>
              </p>
<TABLE width="100%" cellspacing="0" cellpadding="0">
  <TR>
      <TD class="side"> Berichten <? echo $start+1; ?> tot en met <? echo min($start+$amount,sizeof($gastenboek)); ?>.
        <HR> <TABLE width="100%" >
          <?
          $gastenboek=array_reverse($gastenboek);
          for ($i=$start;$i<$start+$amount && $i<sizeof($gastenboek);$i++) {
            list($name,$email,$date,$url,$message)=explode('|||',$gastenboek[$i]);
            $message=str_replace('{{',"\n",$message);
            echo '<TR><TD><B>'.($email!=""?'<A href="mailto:'.$email.'">'.$name.'</A>':$name).'</B></TD><TD align="right"><B>'.$date.'</B></TD></TR>'."\n";
            echo ($url!=""?'<TR><TD colspan="2"><A href="'.$url.'" target="_blank">'.$url.'</A></TD></TR>':'')."\n";
            echo '<TR><TD colspan="2"><SPAN>'.str_replace("\n",'<BR>',htmlspecialchars($message)).'</SPAN></TD></TR>'."\n";
            echo '<TR><TD colspan="2"><HR></TD></TR>'."\n";
          }
        ?>
        </TABLE>
        <CENTER>
          <?
          if ($start>0) echo '<A href="gasten.php?start='.max(0,$start-$amount).'"><<<</A> ';
          if ($start+$amount<sizeof($gastenboek)) echo ' <A href="gasten.php?start='.($start+$amount).'">>>></A>';
        ?>   </CENTER></TD>
    </TR>
</TABLE>
  <TABLE width="100%" cellspacing="0" cellpadding="0" >
    <TR>
      <TD class="side">Nieuw bericht</TD>
    </TR>
  </TABLE>
  <TABLE width="100%" cellspacing="0" cellpadding="0" >
    <TR>
      <TD class="side"> <FORM action="gasten.php" method="GET" onSubmit="return validate(this);">
          <INPUT type="hidden" name="action" value="write">
          <TABLE class="side">
            <TR>
              <TD>Naam:</TD>
              <TD><INPUT type="text" name="name" size="30"></TD>
            </TR>
            <TR>
              <TD>E-Mail:</TD>
              <TD><INPUT type="text" name="email" size="30">
                (optioneel)</TD>
            </TR>
            <TR>
              <TD><p class="antispam">Leave this empty:
            <br /><input name="url"/></p></TD>
            </TR>
            <TR>
              <TD>Bericht:</TD>
              <TD><TEXTAREA name="message" cols="50" rows="5"></TEXTAREA></TD>
            </TR>
          </TABLE>
          <INPUT type="submit" value="Verstuur" <form onsubmit="document.form.name.value=''; document.form.message.value='';"> 
        </FORM></TD>
    </TR>
  </TABLE>
User avatar
mecha_godzilla
Forum Contributor
Posts: 375
Joined: Wed Apr 14, 2010 4:45 pm
Location: UK

Re: Get rid of form values after submitting

Post by mecha_godzilla »

Sorry if this is stating the obvious, but couldn't you just redirect back to the page again using the 'gasten.php' URL without any values appended to it?

HTH,

Mecha Godzilla
clonemaster
Forum Newbie
Posts: 12
Joined: Sun Aug 02, 2009 1:10 pm

Re: Get rid of form values after submitting

Post by clonemaster »

Thanks for replying, byt the matter is solved already
Post Reply