【发布时间】:2014-02-11 15:19:12
【问题描述】:
感谢 enijar 发现了第一个错误,但现在我看到它没有向我的文件写入任何内容。再说一遍,不知道发生了什么。
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$fp = fopen("demoFormData.txt", "a");
$savestring = clean_string($name).", ".clean_string($company).", ".clean_string($email).", ".$model.", ".$os.", ".$comments."\n"."-----------------------------------\n";
fwrite($fp, $savestring);
fclose($fp);
echo 'form saved';
这是我所有的相关代码,包括错误检查:
if(isset($_POST["submit"])){
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
if(!isset($_POST['name']) ||
!isset($_POST['company']) ||
!isset($_POST['email']) ||
!isset($_POST['model']) ||
!isset($_POST['OS']) ||
!isset($_POST['comments'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$name = $_POST["name"];
$company = $_POST["company"];
$email = $_POST["email"];
$model = $_POST["model"];
$os = $_POST["OS"];
$comments = $_POST["comments"];
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp, $email)){
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp, $name)){
$error_message .= 'The Name you entered does not appear to be valid.<br/>';
}
if(!preg_match($string_exp, $company)){
$error_message .= 'The Company you entered does not appear to be valid.<br/>';
}
if(strlen($comments) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$fp = fopen("demoFormData.txt", "a");
$savestring = clean_string($name).", ".clean_string($company).", ".clean_string($email).", ".$model.", ".$os.", ".$comments."\n"."-----------------------------------\n";
fwrite($fp, $savestring);
fclose($fp);
echo 'form saved';
}
对 PHP 很陌生,但对文件读写并不陌生,因此非常感谢任何帮助。
【问题讨论】:
-
错误代码是什么?
-
@Yani 我不知道如何获得它...我的表单位于一个单独的 PHP 文件中,该文件调用了这个文件。我发现它的方式是那条线是旧的评论和祈祷方法。如何查看错误代码?
-
我认为 clean_string 函数之前的这个点导致错误 $savestring = .clean_string($name)。一世';;不过需要查看您的错误。
-
@Enijar 很好地摆脱了错误,但它没有向文件写入任何内容......
-
可能是因为您需要在
echo 'form saved';之后为您的if(isset($_POST["submit"])){加上一个右大括号以读取为echo 'form saved';}@metsales
标签: php