我是PHP新手,最近一直在學習構建表單。我能夠讓他們繼續並運行,並按照教程網站上的指示進行操作。但是,這個形式我試圖通過輸入代碼來建立自己的代碼,我記得我在w3c網站學到了。我無法運行它。請幫我..PHP此錯誤代碼的錯誤
這是形式的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 = "[email protected]";
$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";
也請告訴我在哪裏在我的電子郵件中,我沒有收到發件人。在我之前的所有腳本中,它用來顯示發件人爲「我」。
爲什麼你的函數綁定在if語句中? – skrilled
我不明白你在說什麼。對不起我的新手PHP技能。你可以更具體地說一下你在說什麼功能嗎?如果可能,你能告訴我需要糾正什麼嗎? – user3050570
我該如何糾正? – user3050570