2010-08-31 164 views
2

我是PHP的新手..我正在更新一些朋友的网站。PHP代码错误

我以为我正确添加了代码,但是当我点击从HTML表单提交时,我基本上都会显示所有的PHP代码。 (当正常工作时,它被重定向到另一页)。

这里是我的代码:

<?php 
// Website Contact Form Generator 
// http://www.tele-pro.co.uk/scripts/contact_form/ 
// This script is free to use as long as you 
// retain the credit link 

// get posted data into local variables 
$EmailFrom = Trim(stripslashes($_POST['EmailFrom'])); 
$EmailTo = "EMAILREMOVED"; 
$Subject = "Volunteer Form"; 
$Name = Trim(stripslashes($_POST['Name'])); 
$Telephone = Trim(stripslashes($_POST['Telephone'])); 
$Shift = Trim(stripslashes($_POST['Shift'])); 

// validation 
$validationOK=true; 
if (Trim($EmailFrom)=="") $validationOK=false; 
if (Trim($Name)=="") $validationOK=false; 
if (Trim($Telephone)=="") $validationOK=false; 
if (Trim($Shift1)=="" && Trim($Shift2)=="" && Trim($Shift3)=="" && Trim($Shift4)=="" && Trim($Shift5)=="" && Trim($Shift6)=="" && Trim($Shift7)=="" && Trim($Shift8)=="") $validationOK=false; 
if (!$validationOK) { 
    print "<meta http-equiv=\"refresh\" content=\"0;URL=volunteer-error.html\">"; 
    exit; 
} 

// set Shift Time 
if ($Shift1!="") $time1="8:30am-9:45am, "; 
if ($Shift2!="") $time2="10:00am-11:15am, "; 
if ($Shift3!="") $time3="11:30am-12:45pm, "; 
if ($Shift4!="") $time4="1:00pm-2:15pm, "; 
if ($Shift5!="") $time5="2:30pm-3:45pm, "; 
if ($Shift6!="") $time6="4:00pm-5:15pm, "; 
if ($Shift7!="") $time7="5:30pm-6:45pm, "; 
if ($Shift8!="") $time8="7:00pm-8:00pm"; 

// prepare email body text 
$Body = ""; 
$Body .= "Name: "; 
$Body .= $Name; 
$Body .= "\n"; 
$Body .= "Email: "; 
$Body .= $EmailFrom; 
$Body .= "\n"; 
$Body .= "Telephone: "; 
$Body .= $Telephone; 
$Body .= "\n"; 
$Body .= "Shift: "; 
if (Trim($Shift1)!="") $Body .= $time1; 
if (Trim($Shift2)!="") $Body .= $time2; 
if (Trim($Shift3)!="") $Body .= $time3; 
if (Trim($Shift4)!="") $Body .= $time4; 
if (Trim($Shift5)!="") $Body .= $time5; 
if (Trim($Shift6)!="") $Body .= $time6; 
if (Trim($Shift7)!="") $Body .= $time7; 
if (Trim($Shift8)!="") $Body .= $time8; 
$Body .= "\n"; 

// send email 
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>"); 

// redirect to success page 
if ($success){ 
    print "<meta http-equiv=\"refresh\" content=\"0;URL=volunteer-success.html\">"; 
} 
else{ 
    print "<meta http-equiv=\"refresh\" content=\"0;URL=volunteer-error.html\">"; 
} 
?> 

编辑:这里是html代码:

<!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" /> 
<link href="images/favicon.ico" rel="shortcut icon" type="image/x-icon"> 
<link href="CSS/borders.css" rel="stylesheet" type="text/css" /> 
<title>Volunteer Signup Form</title> 

<style type="text/css"> 
body 
{ 
font-family: Verdana, Arial, Helvetica, sans-serif; 
font-size: 11px; 
} 
</style> 

</head> 

<body> 

<table width="100%" border="0" cellpadding="0" cellspacing="0" class="signup"> 
    <tr> 
    <td>&nbsp;</td> 
    <td>&nbsp;</td> 
    </tr> 
    <tr> 
    <td>&nbsp;</td> 
    <td height="18" valign="top"><strong>Volunteer Signup Form</strong></td> 
    </tr> 
    <tr> 
    <td width="35">&nbsp;</td> 
    <td height="307" valign="top"> 
    <form method="POST" action="contact.php"> 
    Name: <br /> 
<input type="text" name="Name"><br /><br /> 
Email: <br /> 
<input type="text" name="EmailFrom"><br /><br /> 
Telephone Number:<br /> 
<input type="text" name="Telephone"><br /><br /> 
Shift(s):<br /> 
<label><input name="Shift1" type="checkbox" id="shiftid" value="1" /> 8:30am - 9:45am</label><br /> 
<label><input name="Shift2" type="checkbox" id="shiftid" value="1" /> 10:00am - 11:15am</label><br /> 
<label><input name="Shift3" type="checkbox" id="shiftid" value="1" /> 11:30am - 12:45pm</label><br /> 
<label><input name="Shift4" type="checkbox" id="shiftid" value="1" /> 1:00pm - 2:15pm</label><br /> 
<label><input name="Shift5" type="checkbox" id="shiftid" value="1" /> 2:30pm - 3:45pm</label><br /> 

<label><input name="Shift6" type="checkbox" id="shiftid" value="1" /> 4:00pm - 5:15pm</label><br /> 
<label><input name="Shift7" type="checkbox" id="shiftid" value="1" /> 5:30pm - 6:45pm</label><br /> 
<label><input name="Shift8" type="checkbox" id="shiftid" value="1" /> 7:00pm - 8:00pm</label><br /> 

<br /> 

<table width="100%" border="0" cellspacing="0" cellpadding="0"> 
    <tr> 
    <td width="140" align="center"><input type="submit" name="submit" value="Submit" /></td> 
    <td>&nbsp;</td> 
    </tr> 
</table> 

</form> </td> 
    </tr> 
</table> 

</body> 
</html> 

改变我做: 我补充说,涉及到Shift6,Shift7所有代码, Shift8。

我觉得这将是完全相同的代码换班1-5 ..所以我只是复制和粘贴的代码。

这里有什么错误?

注意:电子邮件地址已从代码中删除,原因很明显;)如果需要测试,请使用有效地址填写。

谢谢!

+0

您确定该文件是以'.php'扩展名而不是'.html'保存的。 – shamittomar 2010-08-31 04:54:08

+0

如果它没有你的修改,你可以运行一个'diff',看看有什么不同。 – 2010-08-31 05:14:52

回答

1

PHP文件是否位于启用PHP的Web服务器上?通常当你看到代码时,这意味着服务器没有解释文件。

+0

如果我运行非编辑的代码,它工作正常..一旦我做了更改..它不。这就是为什么我认为我添加了一个错误。 – developer 2010-08-31 05:03:32

+0

编辑您的原始文章并包含html代码,我会尝试一下。 – JMC 2010-08-31 05:07:41

+0

@acidjazz:谢谢:) – developer 2010-08-31 05:13:32

0

如果您的代码最终在浏览器中而不是被解释 - 这意味着通常是服务器配置错误。正确配置的服务器永远不会发送php代码。

一个可能的原因可能是文件名不以.php结尾(所以服务器不知道它必须这样对待它)。

如果文件名是好的,你应该确保服务器支持PHP。

+0

可能只是一个黑暗中的镜头:你确定关于文件扩展名 - 我的意思是你有没有选中“在W​​inExplorer选项中隐藏已知文件类型的扩展名”?如果你在记事本中编辑代码,它可以静静地将文件保存为file.php.txt并隐藏最终的.txt文件。这样的文件不会被服务器解释,因此请确保您将file.php保存为“所有文件”,而不是“文本文档”。 – djn 2010-08-31 13:56:23