2013-03-04 56 views
0

使用本網站很多作爲參考點,但真的與一些代碼掙扎。我建立了一個回調形式,我最初建立客戶端驗證(JavaScript),但很快意識到我需要服務器端PHP驗證,所以試圖將其添加到我的腳本。它在其功能上起作用,除了收到電子郵件時沒有信息顯示。我真的不明白爲什麼當我沒有使用驗證時,$ name不像以前那樣拉動信息。代碼如下:電子郵件到PHP服務器端驗證在腳本

PHP:

<?php 

if (isset($_POST['Form'])) { 
    import_request_variables("p", "z"); 
    $missingfields = array(); 
    $required = array("Name"=>"Name", "Company"=>"Company"); 

    while (list($var, $val) = each($required)) { 
     if (isset($zForm[$var]) && $zForm[$var] != '') { 
      // check this value further here 
     } else { 
      $missingfields[$var] = $val; 
     } 
    } 

    if (count($missingfields)) { 
     print "You missed out one or more fields:<br />"; 

     while(list($var, $val) = each($missingfields)) { 
      print $val . "<br />"; 
     print "Please click back on your browser and enter the required fields.<br />"; 
     } 
    } else { 
     /* Subject and E-mail Variables */ 

$emailSubject = 'Call-back Request'; 
$webMaster = '[email protected]'; 

/* Gathering Data Variables */ 

$name = $_POST['Form[Name]']; 
$email = $_POST['email']; 
$telephone = $_POST['telephone']; 
$company = $_POST['company']; 
$message = $_POST['message']; 

$body = <<<EOD 
<br><hr><br> 
Name: $name <br> 
Email: $email <br> 
Telephone: $telephone <br> 
Company: $company <br> 
Message: $message <br> 
EOD; 

$headers = "From: $email\r\n"; 
$headers .= "Content-type: text/html\r\n"; 
$success = mail($webMaster, $emailSubject, $body, $headers); 

/* Results rendered as HTML */ 

$theResults = <<<EOD 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
<head> 
    <title>Thankyou</title> 
    <link href="style.css" rel="stylesheet" type="text/css" media="screen" /> 
</head> 
<body> 
    <div id="content"> 
     <div id="contactthankyouheader"> 
      <h2>Thankyou for Contacting Us</h2></br> 
     </div> 
     <div id="contactthankyou"> 
      <p>Someone will be in contact with you shortly.</p> 
     </div> 
     <div id="contactthankyou2"> 
      <p>Please click the link below to return to the homepage.  
</p> 
     </div> 
     <div id="contactthankyoulink"> 
      <a href="index.htm"><h4>Homepage</h4></a> 
     </div> 
    </div> 
</body> 
</html> 
EOD; 
echo "$theResults"; 
    } 
} 

?> 

HTML: 

<html> 
<form method="post" action="callback-function.php"> 
Name: <input type="text" name="Form[Name]" /> (required)<br /> 
Company: <input type="text" name="Form[Company]" /> (required) <br /> 
Age: <input type="text" name="Form[Age]" /><br /><br /> 
Languages known:<br /> 
<input type="checkbox" name="Form[Languages][]" value="PHP" checked="checked">  
PHP</input> 
para><input type="checkbox" name="Form[Languages][]" value="CPP"> C++</input> 
<input type="checkbox" name="Form[Languages][]" value="Delphi"> Delphi</input> 
<input type="checkbox" name="Form[Languages][]" value="Java"> Java</input> 
<input type="submit" /> 
</form> 
</html> 

回答

0

Form從您的形式發送數組。所以,你應該把它當作一個數組:

$name = $_POST['Form']['Name']; 
$company = $_POST['Form']['Company']; 

,你缺乏EmailMessage ..等你的表格上。

Languages也是一個數組,你可以使用內爆的列表:

$Languages = implode(',', $_POST['Languages']); 
+0

輝煌,我覺得這是一件簡單的這樣。謝謝你的幫助 – user2132963 2013-03-04 19:12:55