Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Refresh URL

Status
Not open for further replies.

HVtech

IS-IT--Management
Jul 1, 2010
71
NL
Hi,
I have this form, when message is submitted, I'd like to empty the fields (working) and page refreshed without the values behind the ? in the URL
How can I do that?
Thanks for replying..
Code:
 <?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></TD>
    </TR>
  </TABLE>

<form onsubmit="document.form.name.value=''; document.form.message.value=''; document.location='[URL unfurl="true"]http://somesite.nl/index.html';">[/URL]
 
Use POST instead of GET.
Code:
<FORM action="gasten.php" method="[red]POST[/red]" onSubmit="return validate(this);">

You'll need to change your PHP vars to reflect that.

Code:
if ($_[red]POST[/red]['action']=='write') {



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Sory, but doesn't make a differnce.. message is not posted and given url is not loaded.
Any other ideas ?
 
I'm sorry, I though it already worked as it was.

All that was needed was to change all references in your PHP that used the GET superglobal, to use the POST.

The form should work exactly the same as it was, but there will be nothing in the URL to show your form values.
GET uses the URL to pass the form values while POST does not.

Alternatively you could simply redirect to the same form using a javascript window.open() that has the url of the form without any values attached.



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Hi,
I have a different approach now.
This is the page people see the earlier posted messages (well it's supposed to) and can post a new message.
Code:
<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"> <TD class="side"> <FORM action="action.php" method="POST" 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=''; ">
          <input type="hidden" name="return" value="<?php echo '[URL unfurl="true"]http://'.$_SERVER[/URL]['HTTP_HOST'].$_SERVER['REQUEST_URI'] ?>" /> 
        </FORM>
          </TD>
    </TR>
  </TABLE>

On POST output is directed to action.php which look likes this:
Code:
<?php
//error_reporting(E_ALL);
  $amount=15;
  if ($_POST['action']=='write') {
    $file=fopen('gastenboek.txt','a');

    $message=$_POST['message'];
    $message=str_replace("\r",'', $message);
    $message=str_replace("\n",'{{',$message);
    fwrite($file,$_POST['name'].'|||'.$_POST['email'].'|||'.date('d-m-Y').'|||'.$_POST['url'].'|||'.$message."\n");
    fclose($file);
  }
  $start=(isset($_POST['start'])?$_POST['start']:0);
  $gastenboek=Array();
  $gastenboek=file('gastenboek.txt');
  header('location: '.$_POST['return']);
?>

Problem is new message can be posted (I see it in the gastenboek.txt file) but is not visible on the page (gasten.php)
How come ??

Thanks for supporting :)
 
Are you reading from the file when you load gasten.php? and if so how?

I see no code that reads that file.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Yeah, well I guess that's the problem..
Someone else suggested to do it like this..
But now the gastenboek.txt is not read thus not showing messages.
Can you help me to get this right ?
 
Got it
Added this php to the gasten.php for reading the file
Code:
<?php
//error_reporting(E_ALL);
  $amount=15;
    $file=fopen('gastenboek.txt','a');
    $start=(isset($_GET['start'])?$_GET['start']:0);
  $gastenboek=Array();
  $gastenboek=file('gastenboek.txt');
 ?>

Thanks for supporting !
 
Glad you worked it out.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top