Leos
July 26, 2012, 10:46am
1
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
<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("\
","<br />", $message);
$headers2 = "From: Sender <info@gmail.com>\
Content-Type: text/plain; charset=UTF-8; format=flowed\
MIME-Version: 1.0\
Content-Transfer-Encoding: 8bit\
X-Mailer: PHP\
";
$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 in html
<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>
Based on how you have it coded, it would definitely do that. If you don’t want it to, you will need to change your PHP a bit.
First you need to move all of the below code to the top of your page (before ANY output)
<?
if($_POST['name']!="" and $_POST['email']!=""){ // where is the closing brace to this?
$headers = "From: Sender";
$message =
strtoupper($_POST['name'])."
".strtoupper($_POST['email'])."
";
echo str_replace("\
","<br />", $message);
$headers2 = "From: Sender <info@gmail.com>\
Content-Type: text/plain; charset=UTF-8; format=flowed\
MIME-Version: 1.0\
Content-Transfer-Encoding: 8bit\
X-Mailer: PHP\
";
$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);
?>
Then instead of using “echo ‘<script>document.location=“page2.html”; </script>’;” (of which I have no idea how this works since it isn’t in PHP tags, you will want to put this after your email is sent successfully
header("Location: page2.html");
The above line will tell the server to redirect to page2.html and it will do it seamlessly so your user won’t see the delay you are getting now.
Leos
July 26, 2012, 11:52am
3
I tried it and it still outputs the header error? Where did I screw it up?
<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("\
","<br />", $message);
$headers2 = "From: Sender <info@gmail.com>\
Content-Type: text/plain; charset=UTF-8; format=flowed\
MIME-Version: 1.0\
Content-Transfer-Encoding: 8bit\
X-Mailer: PHP\
";
$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);
?>
<?
header("Location: page2.html");
?>
<?
} else {
echo "All fields are mandatory";
?>
<script language="javascript">
alert("All fields are mandatory");
</script>
<?
}
?>
You can’t have any output prior to calling header().
This will get you closer, but you don’t have your opening HTML tags in your code sample so you may still get the output error.
<?
if($_POST['name']!="" and $_POST['email']!=""){
$headers = "From: Sender";
$message =
strtoupper($_POST['name'])."
".strtoupper($_POST['email'])."
";
//echo str_replace("\
","<br />", $message); // commented out as this is output
$headers2 = "From: Sender <info@gmail.com>\
Content-Type: text/plain; charset=UTF-8; format=flowed\
MIME-Version: 1.0\
Content-Transfer-Encoding: 8bit\
X-Mailer: PHP\
";
$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);
header("Location: page2.html");
}
?>
<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']==""){
echo "All fields are mandatory";
?>
<script language="javascript">
alert("All fields are mandatory");
</script>
<?
}
?>
Leos
July 26, 2012, 12:08pm
5
Oh man this one seems to work…Im so glad, and I think I sort of understand how this php script works. I used to reuse it just by copy pasting, until I needed to make it redirect…and hit the wall
It still stops at “inforedirect.php” if I left one or both fields blank…maybe I could make the warning say “go back one page”.
But then I dont see how could I incorporate it 100% in my design - originally the form pops out when user clicks tries to close the page (you know, that old “but wait theres more” trick). So if the name and email form is displayed in the div, hitting “back” wont show it.
Is there a way to check for name and email input before executing the redirect?
Can you post your updated code? As the header redirect won’t execute until both the name and email are entered, so it is redirecting from something else still.
Leos
July 26, 2012, 12:44pm
7
Sure man, no problem. This is what I’ve had in mind with this form;
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript">
function PopIt() {
$("a#trigger").trigger('click');
window.onbeforeunload = UnPopIt;
return "Please don't go because...but wait, there's more!";
}
function UnPopIt() { /* nothing to return */ }
$(document).ready(function() {
window.onbeforeunload = PopIt;
$("a#trigger").fancybox({
'hideOnContentClick': false,
'showCloseButton': false
});
$("a[id!=trigger]").click(function(){ window.onbeforeunload = UnPopIt; });
});
</script>
</head>
<body>
<div style="display: none;">
<a id="trigger" href="#popup"> </a>
<div id="popup">
<div style="background-color:#fff;">
<p class="pop">Stop! Before you leave, please check out these freebies!</p>
<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>
</div>
</div>
</div>
<div id="content">
Content
</div>
</body>
</html>
and the PHP you made for me
<?
if($_POST['name']!="" and $_POST['email']!=""){
$headers = "From: Sender";
$message =
strtoupper($_POST['name'])."
".strtoupper($_POST['email'])."
";
//echo str_replace("\
","<br />", $message); // commented out as this is output
$headers2 = "From: Sender <info@gmail.com>\
Content-Type: text/plain; charset=UTF-8; format=flowed\
MIME-Version: 1.0\
Content-Transfer-Encoding: 8bit\
X-Mailer: PHP\
";
$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);
header("Location: page2.html");
}
?>
<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']==""){
echo "All fields are mandatory";
?>
<script language="javascript">
alert("All fields are mandatory");
</script>
<?
}
?>
Leos
July 26, 2012, 12:58pm
8
Basically it would be a popup triggered on exit. Like this;
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript">
function PopIt() {
$("a#trigger").trigger('click');
window.onbeforeunload = UnPopIt;
return "Stop! Dont go we have cookies for free 99.";
}
function UnPopIt() { /* nothing to return */ }
$(document).ready(function() {
window.onbeforeunload = PopIt;
$("a#trigger").fancybox({
'hideOnContentClick': false,
'showCloseButton': false
});
$("a[id!=trigger]").click(function(){ window.onbeforeunload = UnPopIt; });
});
</script>
</head>
<body>
<div style="display: none;">
<a id="trigger" href="#popup"> </a>
<div id="popup">
<div background-color:#fff;">
<form method="post" action="inforedirect.php" name="popups" id="popups">
<fieldset>
<label for=name accesskey=U ><span class="required">*</span> Name</label>
<input name="name" type="text" id="name" size="30" value="" />
<label for=email accesskey=E ><span class="required">*</span> Email</label>
<input name="email" type="text" id="email" size="30" value="" />
<input type="submit" value="Submit" id="submit" name="submit" class="button" />
</fieldset>
</form>
</div>
</div>
</div>
<div id="content">Content 123</div>
</body>
</html>
…and the PHP remade…something like this
<?
if($_POST['name']!="" and $_POST['email']!=""){
$headers = "From: Sender";
$message =
strtoupper($_POST['name'])."
".strtoupper($_POST['email'])."
";
//echo str_replace("\
","<br />", $message); // commented out as this is output
$headers2 = "From: Sender <info@gmail.com>\
Content-Type: text/plain; charset=UTF-8; format=flowed\
MIME-Version: 1.0\
Content-Transfer-Encoding: 8bit\
X-Mailer: PHP\
";
$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);
header("Location: page2.html");
}
?>
<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']==""){
echo "All fields are mandatory";
?>
<script language="javascript">
alert("All fields are mandatory");
</script>
<?
}
?>