【发布时间】:2012-07-12 19:44:38
【问题描述】:
我找了又找,没有找到任何东西,虽然这可能是因为我什至不知道是什么导致了这个错误,更不用说如何修复它了。
首先,我是个新手。我知道 PHP 的基础知识,但有很多我不知道,所以如果这个问题的答案很简单(或者如果你看不懂我的代码,因为它太乱了!),请原谅。
我想作为我的第一个应用程序之一,我会制作一个简单的电子邮件脚本,用户可以在其中输入他们的姓名、主题、消息和电子邮件地址。
这是表单页面的相关位:http://pastebin.com/UhQukUuB(抱歉,不太清楚如何嵌入代码...)。
<form action="send.php" method="post">
Name: <input type="text" name="name" size="20" /><br />
Subject: <input type="text" name="subject" size="20" /><br />
Message:<br /><textarea name="message" rows="12" cols="55"></textarea><br />
Your email address: <input type="text" name="emailAddress" size="20" /><br />
<input type="submit" value="Send" />
</form>
这在 send.php 中:http://pastebin.com/nky0L1dT。
<?php
$name=$_POST['name'];
$subject=$_POST['subject'];
$message=$_POST['message'];
$emailAddress=$_POST['emailAddress'];
//It's receiving the variables correctly, I've checked by printing the variables.
$errors=array(); //Creates empty array with 0 indexes. This will now be filled with error messages (if there are any errors).
if($name=="" || $subject=="" || $message=="" || $emailAddress=""){
if($name==""){
$errors[0]="You did not supply a name.";
}
if($subject==""){
$errors[count($errors)]="You did not supply a subject."; //I'm using count($errors) so it will create a new index at the end of the array, regardless of how many indexes it currently has (if that makes sense, it's hard to explain)
}
if($message==""){
$errors[count($errors)]="You did not supply a message.";
}
if($emailAddress==""){
$errors[count($errors)]="You did not supply an email address.";
}
}
//Were there any errors?
if(!count($errors)==0){
print "The following errors were found:<br />";
for($i=0; $i<count($errors); $i++){
print $errors[$i]."<br />";
}
die ();
}
//Rest of email script, which I'll write when the stupid bug is fixed. :(
?>
会发生以下情况:当您错过名称、主题或消息时,错误检测器会正常工作并显示“您没有提供名称/主题/消息”。当您错过电子邮件地址时,什么也不会发生。我知道它被存储在数组中并且被正确接收,因为如果您错过了名称/主题/消息和电子邮件地址,它会显示“您没有提供名称/主题/消息。您没有提供电子邮件地址”。我盯着屏幕看了半个小时,只是想弄清楚它为什么会这样?
谢谢。
【问题讨论】: