Help me, PHP redirect is not working right

Myler

the horror.
Apr 25, 2009
531
6
0
G16
I have a form with basic "name and email" fields and after the user submits the info, it grabs the data into txt file on my server and redirects the user to page2.html.

The form I'm using for this job works, but not exactly like I would like it. For instance, when someone enters name and email in the form and submits, it doesnt go directly to page2.html.

It stops for a second on inforedirect.php as if thats some kind of a transition page ?

And, to make it more interesting, if the user for some reason leaves the fields empty and clicks submit, "all fields are mandatory" warning displays, but not in a popup box, it shows the warning on inforedirect.php and the only way to fix it is to click the "back" button in the browser.

Can somebody point me how could I make this form bit better....

Here is the inforedirect.php
Code:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
<script src="http://www.google.com/jsapi"></script>
<script type="text/javascript">google.load("jqueryui", "1.5.2");</script>
<? 
if($_POST['name']!="" and $_POST['email']!=""){

$headers	= "From: Sender";
$message	= 
  strtoupper($_POST['name'])."
".strtoupper($_POST['email'])."
";
echo str_replace("\n","<br />", $message);
$headers2	= "From: Sender <info@gmail.com>\nContent-Type: text/plain; charset=UTF-8; format=flowed\nMIME-Version: 1.0\nContent-Transfer-Encoding: 8bit\nX-Mailer: PHP\n";
$message2	= "
Hello ".($_POST['name'])."	
		 
";
		
mail("$_POST[email]", "Thanks for entering", $message2, $headers2);
		
		
$myFile = "info-file.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = "$_POST[name]*$_POST[email]*".$_SERVER['REMOTE_ADDR']."*".date("d-m-Y H:i")."
";
fwrite($fh, $stringData);
fclose($fh);

?>
        
echo '<script>document.location="page2.html"; </script>';
        
<?
} else {
echo "All fields are mandatory";
?>
<script language="javascript">
alert("All fields are mandatory");
</script>
<?
}
?>



and this is the form code.

Code:
<form method="post" action="inforedirect.php" name="popups" id="popups">
<fieldset>
<label for=name accesskey=U ><span class="required">*</span> Name</label>
<br />
<input name="name" type="text" id="name" size="30" value="" /> 
<br />
<label for=email accesskey=E ><span class="required">*</span> Email</label>
<br />
<input name="email" type="text" id="email" size="30" value="" />
<br />
<input type="submit" value="Submit" id="submit" name="submit" class="button" />
 </fieldset>  
 <br />      
</form>
 


1.) Change the FORM ACTION tag to point to page2.php (not page2.html).

2.) Rename page2.html to page2.php

3.) Inside page2.php, place the following at the top:

Code:
<? 
if($_POST['name']!="" and $_POST['email']!=""){

$headers    = "From: Sender";
$message    = 
  strtoupper($_POST['name'])."
".strtoupper($_POST['email'])."
";
echo str_replace("\n","<br />", $message);
$headers2    = "From: Sender <info@gmail.com>\nContent-Type: text/plain; charset=UTF-8; format=flowed\nMIME-Version: 1.0\nContent-Transfer-Encoding: 8bit\nX-Mailer: PHP\n";
$message2    = "
Hello ".($_POST['name'])."    
         
";
        
mail("$_POST[email]", "Thanks for entering", $message2, $headers2);
        
$myFile = "info-file.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = "$_POST[name]*$_POST[email]*".$_SERVER['REMOTE_ADDR']."*".date("d-m-Y H:i")."
";
fwrite($fh, $stringData);
fclose($fh);

} else {
echo "All fields are mandatory";
echo '<script language="javascript">';
echo 'alert("All fields are mandatory");';
echo '</script>';
}
?>

And whoever wrote that code is the reason many developers think PHP is a shitty language. Sloppy, sloppy... :)
 
@ Klopa : your example works, no "in between" page blink like before.

But if I decide to leave the fields empty and submit the form, it loads empty "page2.php" with "all fields are mandatory" warning, and when I click OK on that warning, it loads the acutal page2.php with all the content...

Is there a way to make the warning message stay on the first page before the redirect starts, so if someone doesnt enter their name they actually get the chance to enter it?
 
Hmmm... yeah, there is. I was just being too lazy to explain it. Here, do this:

1.) Put this at the top of your HTML form:

Code:
<script type="text/javascript">
     function submitForm() {
          var form = document.forms[0];
          if (form.name.value == '') { alert("You didn't enter a name!"); return; }
          if (form.email.value == '') { alert("You didn't enter an e-mail!"); return; }

          // Verify e-mail address
          apos = form.email.value.indexOf("@");
          dotpos = form.email.value.lastIndexOf(".");
          if (apos < 1 || dotpos - apos < 2) { alert("Invalid e-mail address"); return; }

          // Submit form
          form.submit();
      }
</script>
2.) Within your HTML form, change the submit button to:

Code:
<input type="button" value="Submit" class="button" onclick="submitForm();">
Should work fine. :)
 
PS. Should mention, make sure to take the 'name="submit"' part out of the submit button attributes. Otherwise, the Javascript won't work. Nothing will happen when you click the button.