我是PHP新手,最近一直在学习构建表单。我可以按照教程网站上的说明密切启动并运行它们。但是,这个,我尝试通过输入代码自己构建,我记得我在w3c网站上学到了。我无法让它运行。请帮帮我......
这是表单的HTML代码:
<form name="contactform" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" id="contactform">
<label for="first_name">First Name<span class="error">*<?php echo $first_nameErr;?></span></label><br>
<input type="text" name="first_name" maxlength="50" size="20" value="<?php echo $first_name;?>"><br>
<label for="last_name">Last Name<span class="error">*<?php echo $last_nameErr;?></span></label><br>
<input type="text" name="last_name" maxlength="50" size="20" value="<?php echo $last_name;?>"><br>
<label for="email">Email Address<span class="error"><?php echo $emailErr;?>*</span></label><br>
<input type="text" name="email" maxlength="80" size="35" value="<?php echo $email;?>"><br>
<label for="overview">Overview of project <span class="error">* <?php echo $overviewErr;?></span></label><br>
<textarea name="overview" maxlength="1000" cols="25" rows="5"><?php echo $overview;?></textarea><br>
<br> <input type="submit" value="Submit" name="submit" class="art-button" style="zoom: 1;">
</form>
这是表单的PHP代码,它与HTML代码在同一个文件中:
<?php
function test_input($data){
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
// define variables and set to empty values
$first_nameErr = $last_nameErr = $emailErr = $overviewErr = "";
$first_name = $last_name = $email = $overview = "";
if(isset($_POST['email'])) {
$email_to = "myself@mydomain.com";
$email_subject = "Contact us - My company's name";
{
if (empty($_POST["first_name"]))
{$first_nameErr = "(First Name is required)";}
else
{$first_name = test_input($_POST["first_name"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$first_name))
{
$first_name = "(Only letters and white space allowed)";
}
}
if (empty($_POST["last_name"]))
{$last_nameErr = "(Last Name is required)";}
else
{$last_name = test_input($_POST["last_name"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$last_name))
{
$last_name = "(Only letters and white space allowed)";
}
}
if (empty($_POST["email"]))
{$emailErr = "(Email ID is required)";}
else
{$email = test_input($_POST["email"]);
// check if e-mail address syntax is valid
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))
{
$emailErr = "(Invalid email format)";
}
}
if (empty($_POST["overview"]))
{$overviewErr = "(Overview is required)";}
else
{$overview = test_input($_POST["overview"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$overview))
{
$overview = "(Only letters and white space allowed)";
}
}
}
//Email & SEND INFO
$email_message = "Form details below.\n\n";
$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email)."\n";
$email_message .= "Services: ".clean_string(implode(', ', $service))."\n";
$email_message .= "Overview: ".clean_string($overview)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- Success HTML -->
Thank you for contacting us. We will be in touch with you very soon.
<?php
}
?>
现在,每当我执行代码时,我都会收到此错误: 在第48行调用未定义的函数test_input(),这是整个验证过程的名字字段中的test_input()。
如何解决此错误?
提前致谢。
我使用larsAnders的建议解决了上述错误!感谢那!
我根据我正在使用的新代码更新了主要代码。
但是我有两个新的错误,因为新的错误,我在电子邮件中没有收到任何内容,我在复选框中检查了,而且当我收到电子邮件时,我也没有得到我从中得到它的人。这是我正在使用的HTML代码:
<label for="service">Services required<br></label>
<input type="checkbox" name="service[]" value="web_design_services">Web Design Services</label><br>
<input type="checkbox" name="service[]" value="web_development_services">Web Development Services</label><br>
<input type="checkbox" name="service[]" value="web_identity_management_services">Web Identity Management Services</label><br>
<input type="checkbox" name="service[]" value="digital_design_services">Digital Design Services</label><br>
<input type="checkbox" name="service[]" value="web_video_services">Web Video Services</label><br>
<input type="checkbox" name="service[]" value="web_audio_services">Web Audio Services</label><br>
<input type="checkbox" name="service[]" value="content_management_services">Content Management Services</label><br>
并且,这是我使用的关于整个脚本中的新错误的唯一一行PHP代码:
$email_message .= "Services: ".clean_string(implode(', ', $service))."\n";
请在我的电子邮件中告诉我在未收到发件人方面出错的地方。在我之前的所有脚本中,它用于将发件人显示为“我”。
答案 0 :(得分:1)
在PHP中,与许多语言一样,事物的顺序很重要。因此,当您的脚本正在执行并且它到达函数调用时,该函数必须已经在某处定义。首先,定义一个函数。然后,调用该函数。你不能只在任何地方放置一个函数(比如在if块中)并期望代码搜索并使用它。确保您的函数都在任何if块之外定义,因为如果条件的计算结果为false,则永远不会定义函数。在学习的过程中,只需将所有功能放在脚本的顶部,在任何逻辑块之外。像这样开始你的脚本:
function test_input($data){
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
// define variables and set to empty values
$first_nameErr = $last_nameErr = $emailErr = $overviewErr = "";
$first_name = $last_name = $email = $overview = "";
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "sriprabhav@ymail.com";
$email_from = $_POST['email'];
$email_subject = "Contact us - Origin web designers";
//{ why are there stray brackets everywhere? if(condition){code} else{code}
if (empty($_POST["first_name"]))
{$first_nameErr = "(First Name is required)";}
else
{$first_name = test_input($_POST["first_name"]);}
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$first_name))
{
$first_name = "(Only letters and white space allowed)";
}
if (empty($_POST["last_name"]))
等......和以前一样......
要处理复选框,您需要这样的东西。这将定义一个名为$ service的数组:
if (isset($_POST['service'])) {
$service = $_POST['service'];
}
它可以放在这一行之前:
//Email & SEND INFO
然后,当您的代码点击$ email_message。=“Services:line时,它将有一个名为$ service的数组以进行内爆。