2011-12-22 169 views
2

我是非常新的表單驗證和處理,事實上,這是我的第一次嘗試,並且我創建了一個包含所需文本字段,註釋文本區域的簡單聯繫表單和兩個單選按鈕。我已經經歷了許多基本的工作,並且使用PHP將腳本放在一起。我遇到的問題是,當我單擊提交如果窗體是空的,我被導向到一個新的頁面,有一個錯誤消息說我需要填寫我的名字。但是,如果填寫我的姓名並點擊提交,我會得到相同的消息,而不是爲下一個字段指定的錯誤消息。我甚至在收到所有字段時都會收到此消息。以下是我的代碼:表單錯誤信息無法正常工作,表單不會提交

<?php 
    $myemail = "[email protected]"; 

    $name = check_input($_POST['name'], "Enter your name"); 
    $company = check_input($_POST['company'], "Enter your company name"); 
    $function = check_input($_POST['function'], "Provide an occassion"); 
    $address = check_input($_POST['address'], "Enter your address"); 
    $place = check_input($_POST['place'], "Enter your area code"); 
    $number = check_input($_POST['number'], "Enter your number"); 
    $mobile = check_input($_POST['mobile'], "Enter your mobile number"); 
    $email = check_input($_POST['email'], "Enter a valid E-Mail address"); 
    $client = check_input($_POST['client'], "Are you a client already? Please let us know"); 
    $method = check_input($_POST['method'], "Choose your preferred method of contact"); 
    $message = check_input($_POST['message'], "Enter your comments or description"); 
    $date = check_input($_POST['date'], "Tell us when your event takes place"); 
    $size = check_input($_POST['size'], "Tell us how big your presentation is going to be"); 


    $subject = "Client from website"; 
    $subject = striplashes($subject); 

    if(isset($_POST['button-submit'])) { 

    if (!isset($_REQUEST['email'])) { 
     header("Location: index.html"); 
     } 
     else { 
     mail("[email protected]", "Feedback Form Results", 
      $message, "From: $email"); 
     header("Location: thanks.html"); 
     } 

    } 

    if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email)) 
    { 
     show_error("E-mail address not valid"); 
    } 


    $enquiry = "Hello! 

    Your contact form has been submitted by: 

    Name: $name 
    Company: $company 
    Event: $function 
    Address: $address 
    Place: $place 
    E-mail: $email 
    Contact number: $number 
    Mobile: $mobile 
    Current client: $Client 

    Comments: $message 

    My exhibition is on $date 
    Size: $size 

    Please contact me $method 



    End of message"; 


    mail($myemail, $subject, $enquiry); 

    header('Location: thanks.html'); 
    exit(); 


    function check_input($data, $problem='') 
    { 
     $data = trim($data); 
     $data = stripslashes($data); 
     $data = htmlspecialchars($data); 
     if ($problem && strlen($data) == 0) 
     { 
      show_error($problem); 
     } 
     return $data; 
    } 

    function show_error($myError) 
    { 
    ?> 
     <html> 
     <body> 

     <b>Please correct the following error:</b><br /> 
     <?php echo $myError; ?> 

     </body> 
     </html> 
    <?php 
    exit(); 
    } 
    ?> 

請幫我解決。我不知道我做錯了什麼。感謝提前:)

下面是HTML表單:

<form id="contact" method="post" action="sendmail.php" enctype="multipart/form-data"> 
             <ul> 
             <li style="margin-bottom:15px; text-align:left"><label for="name">Vorname/Name*</label> 
                           <input id="name" name="Name" type="text" value="" class="name-field" /></li> 

             <li style="margin-bottom:15px; text-align:left"><label for="company">Firma*</label> 
                           <input id="company" name="Company" type="text" value="" class="company-field" /></li> 

             <li style="margin-bottom:15px; text-align:left"><label for="function">Funktion*</label> 
                         <input id="function" name="Function" type="text" value="" class="function-field" /></li> 

             <li style="margin-bottom:15px; text-align:left"><label for="address">Adresse*</label> 
                         <input id="address" name="Address" type="text" value="" class="address-field" /></li> 

             <li style="margin-bottom:15px; text-align:left"><label for="place">PLZ/Ort*</label> 
                         <input id="place" name="Place" type="text" value="" class="place-field" /></li> 

             <li style="margin-bottom:15px; text-align:left"><label for="number">Telefon*</label> 
                         <input id="number" name="Number" type="text" value="" class="number-field" /></li> 

             <li style="margin-bottom:15px; text-align:left"><label for="mobile">Mobiltelefon*</label> 
                         <input id="mobile" name="Mobile" type="text" value="" class="mobile-field" /></li> 

             <li style="margin-bottom:15px; text-align:left"><label for="email">E-Mail*</label> 
                         <input id="email" name="Email" type="text" value="" class="email-field" /></li> 




             <li style="margin-bottom:15px; text-align:left">Wir sind bereits Kunde* 
                               <input type="radio" name="Client" id="r1" value="Ja" /> 
                                <label for="r1">Ja</label> 
                               <input type="radio" name="Client" id="r2" value="Nein" /> 
                                <label for="r2">Nein</label> 
             </li> 



             <li style="margin-bottom:15px; text-align:left">Kontaktieren Sie uns* 
                              <input type="radio" name="method" id="r3" value="Per telefon" /> 
                                <label for="r3">Per telefon</label> 
                               <input type="radio" name="method" id="r4" value="Per e-mail" /> 
                                <label for="r4">Per e-mail</label> 
             </li> 

             <li style="margin-bottom:15px; text-align:left"><label for="message">Bemerkungen*</label> 
                         <textarea id="message" name="Message" type="text" value="" class="message-field" ></textarea></li> 

             <li style="margin-bottom:15px; text-align:left"><label for="date">Nächste Messe datum*</label> 
                         <input id="date" name="Date" type="text" value="" class="date-field" /></li> 

             <li style="margin-bottom:15px; text-align:left"><label for="size">Standgrösse m sq*</label> 
                         <input id="size" name="Size" type="text" value="" class="size-field" /></li> 

             <li style="margin-top:25px; margin-right:10px; text-align:left; color:#F00">*Diese Felder werden benötigt.</li> 

             <input type="submit" class="button-submit" id="button-submit" value="Senden" name="button-submit" /> 

             </ul> 
            </form> 
+0

如何調用check_input函數,我認爲我們在這裏丟失了一些代碼。 – RageZ 2011-12-22 09:33:03

回答

0

的問題是,這是否if ($problem && strlen($data) == 0)總是正確的 - 這讓我覺得你不$_POST['name']得到任何價值 - 把一些消息,並看看你有什麼信息。

0

我試過這段代碼,它在我身邊工作!嘗試utf8編碼您的文本,表單輸入名稱等...

<input type='text' name='name'> 
<?php 
$myemail = "[email protected]"; 

$name = check_input($_POST['name'], "Enter your name"); 
$company = check_input($_POST['company'], "Enter your company name"); 
$function = check_input($_POST['function'], "Provide an occassion"); 
$address = check_input($_POST['address'], "Enter your address"); 
$place = check_input($_POST['place'], "Enter your area code"); 
$number = check_input($_POST['number'], "Enter your number"); 
$mobile = check_input($_POST['mobile'], "Enter your mobile number"); 
$email = check_input($_POST['email'], "Enter a valid E-Mail address"); 
$client = check_input($_POST['client'], "Are you a client already? Please let us know"); 
$method = check_input($_POST['method'], "Choose your preferred method of contact"); 
$message = check_input($_POST['message'], "Enter your comments or description"); 
$date = check_input($_POST['date'], "Tell us when your event takes place"); 
$size = check_input($_POST['size'], "Tell us how big your presentation is going to be"); 


$subject = "Client from website"; 
$subject = striplashes($subject); 

if(isset($_POST['button-submit'])) { 

if (!isset($_REQUEST['email'])) { 
    header("Location: index.html"); 
    } 
    else { 
    mail("[email protected]", "Feedback Form Results", 
     $message, "From: $email"); 
    header("Location: thanks.html"); 
    } 

} 

if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email)) 
{ 
    show_error("E-mail address not valid"); 
} 


$enquiry = "Hello! 

Your contact form has been submitted by: 

Name: $name 
Company: $company 
Event: $function 
Address: $address 
Place: $place 
E-mail: $email 
Contact number: $number 
Mobile: $mobile 
Current client: $Client 

Comments: $message 

My exhibition is on $date 
Size: $size 

Please contact me $method 



End of message"; 


mail($myemail, $subject, $enquiry); 

header('Location: thanks.html'); 
exit(); 


function check_input($data, $problem='') 
{ 
    $data = trim($data); 
    if (strlen($data) == 0) 
    { 
     show_error($problem); 
    } 
    $data = stripslashes($data); 
    $data = htmlspecialchars($data); 
    return $data; 
} 

function show_error($myError) 
{ 
?> 
    <html> 
    <body> 

    <b>Please correct the following error:</b><br /> 
    <?php echo $myError; ?> 

    </body> 
    </html> 
<?php 
exit(); 
} 
?> 
+0

感謝您的幫助,但我仍然有問題...也許問題出在我的代碼? – user1081524 2011-12-23 11:50:17

+0

我在原來的問題中添加了表單 – user1081524 2011-12-23 11:53:12