2014-03-05 132 views
0

我是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">*&nbsp;<?php echo $overviewErr;?></span></label><br> 

<textarea name="overview" maxlength="1000" cols="25" rows="5"><?php echo $overview;?></textarea><br> 

<br>&nbsp;<input type="submit" value="Submit" name="submit" class="art-button" style="zoom: 1;">&nbsp; 
</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"; 

也請告訴我在哪裏在我的電子郵件中,我沒有收到發件人。在我之前的所有腳本中,它用來顯示發件人爲「我」。

+0

爲什麼你的函數綁定在if語句中? – skrilled

+0

我不明白你在說什麼。對不起我的新手PHP技能。你可以更具體地說一下你在說什麼功能嗎?如果可能,你能告訴我需要糾正什麼嗎? – user3050570

+0

我該如何糾正? – user3050570

回答

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 = "[email protected]"; 
$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"])) 

等......像以前一樣......

爲了處理複選框,你需要這樣的事情。這將定義一個數組名爲$服務:

if (isset($_POST['service'])) { 
    $service = $_POST['service']; 
} 

而且它可以在此行前右側放置:

//Email & SEND INFO 

然後,當你的代碼打$ email_message =「服務:行,它將有一個名爲$ service的數組來內爆。

+1

[其實它沒有](https://ideone.com/9MNOwk)。 – Daedalus

+0

@達達盧斯我知道,這是一個可愛的小演示,但只是爲了一個新手而裝作。把事情簡單化。第一天你無法學習所有東西。我將編輯我的帖子來解決真正的問題,這是if塊。 – larsAnders

+0

一個人不會爲了新手而假裝,第一次給出正確的例子。如果你知道它錯了,不要說它是正確的。 – Daedalus