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 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错