【问题标题】:PHP form with redirect prints form info on page带有重定向的 PHP 表单在页面上打印表单信息
【发布时间】:2012-07-25 22:35:51
【问题描述】:

我已经设法在提交时使名称/电子邮件表单具有重定向功能。现在唯一的问题是,当重定向页面加载时,我会在其顶部获得表单输入(姓名和电子邮件)。

有人知道我哪里不及格了吗?

<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="style.css" rel="stylesheet" type="text/css" />
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
 function submitForm() {
      var form = document.forms[0];
      if (form.name.value == '') { alert("Name?"); return; }
      if (form.email.value == '') { alert("Email?"); return; }

      apos = form.email.value.indexOf("@");
      dotpos = form.email.value.lastIndexOf(".");
      if (apos < 1 || dotpos - apos < 2) { alert("Correct email?"); return; }

      form.submit();
  }
</script>
<script src="fancybox/jquery.fancybox-1.3.1.pack.js" type="text/javascript"></script>
<link rel="stylesheet" href="fancybox/jquery.fancybox-1.3.1.css" type="text/css" media="screen"/>

<script type="text/javascript">
function PopIt() { 
    $("a#trigger").trigger('click');
    window.onbeforeunload = UnPopIt;
    return "Dont go."; 
}

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">&nbsp;</a>
<div id="popup">
<div style="background-color:#ff3300;">
<form method="post" action="page2.php" name="popups" id="popups">
        <fieldset>
        <label for=name><span class="required">*</span> Name</label>
        <br />
        <input name="name" type="text" id="name" size="30" value="" /> 
        <br />
        <label for=email><span class="required">*</span> Email</label>
        <br />
        <input name="email" type="text" id="email" size="30" value="" />
        <br />
        <input type="button" value="Submit" class="btn" onclick="submitForm();">            
        </fieldset>  
        <br />
</form>
</div>        
</div>
</div>

<div id="content">Fa la la la la</div>
</div>
</body>
</html>

当访问者输入数据时(在弹出窗口中,退出时触发),点击提交按钮后,表单重定向到这里;

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

$headers    = "From: Acme";
$message    = 
strtoupper($_POST['name'])."
".strtoupper($_POST['email'])."
";
echo str_replace("\n","<br />", $message);
$headers2    = "From: Acme <ceo@acme.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("ceo@acme.com", "Freebies", $message, $headers);        
mail("$_POST[email]", "Gesundheit!", $message2, $headers2);

$myFile = "free.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);

}
?>
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="style.css" rel="stylesheet" type="text/css" />
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function(){
setTimeout(function(){
$("div#md").fadeIn("slow", function () {
});

}, 1000);
});
</script>
</head>
<body>
<div id="top"><a href="index.html"><img src="top.png" /></a></div>
<div id="wrapper" style="height:900px;">
<div id="content">Whoo-eee</div>
</div>
</body>
</html>

但我看不到此页面顶部显示的表单信息(姓名和电子邮件)如何或为什么显示2.php...

【问题讨论】:

  • 难道不是因为"echo str_replace("\n","
    ", $message);" ?
  • 如果它现在有效,您应该接受下面的答案

标签: php forms redirect


【解决方案1】:

你有这条线

  echo str_replace("\n","<br />", $message);

在第 2 页顶部附近,因此您正在回显由姓名和电子邮件组成的消息

【讨论】:

    【解决方案2】:
    echo str_replace("\n","<br />", $message);
    

    在第 2 页注释掉这一行

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-29
      • 1970-01-01
      • 2014-03-05
      • 2013-09-10
      • 2021-03-01
      • 2013-07-08
      • 2021-04-15
      • 1970-01-01
      相关资源
      最近更新 更多