用于从html表单收集数据的PHP脚本

时间:2012-04-19 12:36:45

标签: php html-form

我遇到了一个php脚本的问题,我写的是从html表单中收集表单数据,下面是代码。我觉得我很遗憾,我的PHP体验有限。

任何帮助/建议表示赞赏。如果您编辑代码,请尝试具体,以便我可以看到错误的位置。

"\nComments:\n".$comments. ;
<?php
$error = "\n Please review the information you provided, there seems to be a problem" ;
$email1 = 'root@centos6.labform.localdomain' ;
$to = "$email1";
$from = $_POST['email'];
$name = "$firstname $lastname";
$headers = "From: $email \n";
$subject = 'Lab Systems Request Form';

// we now create the email

$message = array();
$firstname = $_POST['firstname'] ;
$lastname = $_POST['lastname'] ;
$extnumber = $_POST['extnumber'] ;
$department = $_POST['department'] ;
$email = $_POST['email'] ;
$type = $_POST['type'] ;
$systems = $_POST['systems'] ;
$comments = $_POST['comments'] ;


$body = "We have received the following information from $name:\n\nHere are the details:\n"
"\nFirstName:\n".$firstname.
"\nLastName:\n".$lastname.
"\nExtNumber:\n".$extnumber.
"\nDepartment:\n".$department.
"\nEmail:\n".$email.
"\nFormFactor:\n".$type.
"\nSystemType:\n".$systems.
//$body ="We have received the following information from $name:\n\n";
//"Here are the details:\n " ;
//"FirstName: $firstname \n" ;
//"LastName: $lastname \n" ;
//"ExtNumber: $extnumber \n" ;
//"Department: $department \n" ;
//"Email: $email \n" ;
//"FormFactor: $type \n" ;
//"SystemType: $systems \n" ;
//"Comments: $comments \n" ;

// now we send the email and redirect to thankyou.html if all ok
$send = mail( $to,$from,$headers,$subject,$body );
//print "$to,$subject,$body,$headers";

if($send)
 {header( "Location: thankyou.html" );}
 else
 {print "We encountered an error sending your mail, please notify directly
$email1 or $email2"; }

?>

此外,当我因某种原因设法打印所有数据时,它会在每行前面放置一个“1”: - S并以相反的顺序收集数据 非常感谢! STONITH

嗨,谢谢你的回复。我编辑了代码。我添加了你的建议。代码运行正常,它重定向到thankyou.html但电子邮件正文是空的,如果我在每个开头添加打印:“\ nFirstName:\ n”。$ firstname。 .......然后我可以看到收集的示例数据,但我得到的是:日期:星期四,2012年4月19日15:54:22 +0100(IST)来自:Apache To:root@centos6.labform。 localdomain主题:root@centos6.labform.localdomain实验室系统申请表来自:并且没有收集其他数据

这是html表单:     http://dpaste.com/734277/ 这是collection.php:     http://dpaste.com/734276/

2 个答案:

答案 0 :(得分:1)

mail()函数中的参数顺序错误。 from-address应该在标题中。请查看the manual for this function以使其正确。

答案 1 :(得分:1)

安东尼奥

你需要对所有代码出错以及所有代码(我们需要查看表单)进行更具描述性,但这听起来就像脚本正在运行一样。

除了Lex说你的$ body变量有一个。最后不应该在那里:

$body = "We have received the following information from $name:\n\nHere are the details:\n"
"\nFirstName:\n".$firstname.
"\nLastName:\n".$lastname.
"\nExtNumber:\n".$extnumber.
"\nDepartment:\n".$department.
"\nEmail:\n".$email.
"\nFormFactor:\n".$type.
"\nSystemType:\n".$systems.
"\nComments:\n".$comments. ;

应该是:

$body = "We have received the following information from $name:\n\nHere are the      details:\n"
"\nFirstName:\n".$firstname.
"\nLastName:\n".$lastname.
"\nExtNumber:\n".$extnumber.
"\nDepartment:\n".$department.
"\nEmail:\n".$email.
"\nFormFactor:\n".$type.
"\nSystemType:\n".$systems.
"\nComments:\n".$comments ;