dousi4148 2015-11-12 10:48
浏览 58
已采纳

需要使用PHP保存数据的HTML表单

I'm trying to make a form whose fields will be automatically populated from the url. Also, if user clicks on "Save me a seat" it should redirect to one location, send email and save cookies. If user clicks on "Not interested, thank you", it should redirect him/her to another location, send email and save cookies.

Now, I haven't worked with PHP for 2 years now and there is much that I forgot.

Here is the code:

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
if(isset($_POST['submit'])){
// Fetching variables of the form which travels in URL
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$title = $_POST['positiontitle'];
$company = $_POST['companyname'];
$all = array($firstname, $lastname, $title, $company);
if($firstname !=''&& $lastname !=''&& $title !=''&& $company !='')
{
//  To redirect form on a particular page
header("Location:http://www.example.com");
}
else{
?><span><?php echo "Please fill in all fields!";?></span> <?php
}
}
?>
<!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" />
<meta content="noindex, nofollow" />
</head>
<body>
 <form class="conf-form" method="POST">
    <legend>Please input your information:</legend><br />
    First name:<br>
    <input class="fst-name" type="text" name="firstname" value="First Name">
    <br><br />
    Last name:<br>
    <input class="lst-name" type="text" name="lastname" value="Last Name">
    <br /><br />
    Position title:<br />
    <input class"pos-title" type="text" name="positiontitle" value="Position Title"/>
    <br /> <br />
    Company name:<br />
    <input class="cmp-name" type="text" name="companyname" value="Company Name"/>
    <br /><br />
    <input id="save-me" type="submit" value="Save me a seat" name="submit">&nbsp;&nbsp;&nbsp;&nbsp;<input id="not-int" type="submit" value="Not interested, thank you" name="notsubmit">
    <br /><br />
    If you have any comments/questions please let us know here:<br />
    <textarea rows="4" cols="50"></textarea>
</form> 
</body>
</html>

It's not redirecting where I want it. For start, how do I fix this?

  • 写回答

2条回答 默认 最新

  • dreljie602951 2015-11-12 10:55
    关注

    Redirecting cannot be made after you have already sent content. In your case, you have already have HTML content before running header():

    Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.

    This should trigger an error but you probably have error reporting turned off. To turn on error reporting, you'll need to include this at the top of your main PHP file that runs every time:

    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(E_ALL);
    

    This will save you time when developing.

    To solve your exact problem, you need to move the PHP include file above your HTML content.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算