duanfaxin7014 2012-07-24 09:16
浏览 109

联系表单+重定向不起作用

I made a simple "name and email" contact form and I would want it to redirect to "page2.html" after the information has been submitted.

I cant get it to work.

This is the html part I made for the submission form. Im still not sure what does "name" part stand for, the id part (with the dash) refers to CSS styling. But anyhow, this is how I made the contact "name and email" form.

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

and php part is recycled, i tried entering header with redirect in the last line but it doesnt work, the best it does is opens up a blank page in the browser that prints out the info I entered in the form.

I would like to have the form redirect after the info has been submitted, so...thanks for help, in advance :D

<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']!="" ){


    $headers    = "From: Webmaster";
    $message    = 
  strtoupper($_POST['name'])."
".strtoupper($_POST['email'])."
";
    echo str_replace("
","<br />", $message);
    $headers2   = "From: Sender Name <email@gmail.com>
Content-Type:     text/plain; charset=UTF-8; format=flowed
MIME-Version: 1.0
Content-Transfer-Encoding:     8bit
X-Mailer: PHP
";
    $message2   = "Message
";

    mail("webmaster@gmail.com", "Subject", $message, $headers);
    mail("$_POST[email]", "Subject", $message2, $headers2);

    $myFile = "submissions.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 "You didnt enter anything";
    ?>
    <script language="javascript">
    alert("You didnt enter anything");
    </script>
    <?
}
?>
  • 写回答

3条回答 默认 最新

  • dongpixi2648 2012-07-24 09:27
    关注

    The name attribute in the form will give the $_POST array is name. E.g., using

    <input type="text" name="awesometext"/>
    

    in a form with method="post" will make the value of that text field accessible through $_POST['awesometext'] in the php script you define in the form's action.

    If after sending the email you want to redirect the user to another page using header(), no output must be generated on the page before that. Get rid of all echoes and html output, so remove the html you have at the beginning. You may only have php code without output if you want to use header().

    Then you can use header('Location:somepage.php'); to direct to somepage.php.

    Your code would then be:

    <?php 
    if($_POST['name']!="" ){
    
    
    $headers    = "From: Webmaster";
    $message    = 
    strtoupper($_POST['name'])."
    ".strtoupper($_POST['email'])."
    ";
    echo str_replace("
    ","<br />", $message);
    $headers2   = "From: Sender Name <email@gmail.com>
    Content-Type:     text/plain; charset=UTF-8; format=flowed
    MIME-Version: 1.0
    Content-Transfer-Encoding:     8bit
    X-Mailer: PHP
    ";
    $message2   = "Message
    ";
    
    mail("webmaster@gmail.com", "Subject", $message, $headers);
    mail("$_POST[email]", "Subject", $message2, $headers2);
    
    $myFile = "submissions.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 here
    header('Location:somepage.php');
    
    } else {
    echo "You didnt enter anything";
    ?>
    

    The header() needs to be called before any output is generated. The code above should work.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体