codecamp

PHP教程 - 表单提交后的PHP重定向

PHP教程 - 表单提交后的PHP重定向

重定向是通过使用header()函数使用PHP输出位置来完成的。

这里的如何重定向到一个名为thanks.html的页面:

header(“Location:thanks.html");

不要通过echo()或print()将任何内容输出到浏览器,或通过在外部包含HTML标记<?php ...?> 标签之前调用header()。

例子

这里是一个表单处理程序脚本的重定向到感谢页面的快速示例:


<?php //from   ww  w  .j  a va2  s .c om
    if ( isset( $_POST["submitButton"] ) ) { 
      // (deal with the submitted fields here) 
      header( "Location: thanks.html" ); 
      exit; 
    } else { 
      displayForm(); 
    } 
    function displayForm() { 
    ?> 
    <!DOCTYPE html5> 
    <html> 
      <body> 
        <form action="index.php" method="post"> 
            <label for="firstName">First name</label> 
            <input type="text" name="firstName" id="firstName" value="" /> 
            <label for="lastName">Last name</label> 
            <input type="text" name="lastName" id="lastName" value="" /> 
            <input type="submit" name="submitButton" id="submitButton" value= "Send Details" /> 
        </form> 
      </body> 
    </html> 
    <?php 
    } 
    ?> 


PHP表单元素
PHP表单TextField
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

Operator

Introduction

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }