2014-02-12 56 views
0

我已經創建了這個HTML表單和編碼的PHP,試圖在用戶填寫表單並單擊提交按鈕後將用戶信息發送到我的電子郵件。然而,因爲我點擊「提交」按鈕後只輸出了我的PHP代碼。請看看我的代碼,讓我知道我做錯了什麼。提前致謝。HTML表單沒有被提交。 PHP代碼不能正常工作

HTML代碼

<!DOCTYPE html> 
<html> 
<head> 

<title> 
    learning jquery 
</title> 
<style> 
textarea, input{ 
position:absolute; left:130px; 
} 
body{background-color:gray;} 
h1{ 
    text-align:center; margin-bottom:20px; 
} 
label{ 
    position:absolute; left:145px; 
} 
#label1, #checkbox{position:relative; 
    top:125px; 
} 
#reset{position:absolute; top:500px;} 
#submit{position:absolute; top:500px; left:200px;} 
</style> 
</head> 

<body> 
    <h1>Please fill out the form below</h1> 
    <form action="form.php" method="post"> 
<p>E-mail Address: <input type="email" name="email"/></p> 
<p>Name: <input type="text" name="name"/></p> 
<p>Phone: <input type="tel" name="phone"/></p> 
<p><p> 
<input type="radio" name="budget" id="radio"/><label for="radio">$1,000</label><br> 
<input type="radio" name="budget" id="radio1"/><label for="radio1">$1000 - $5000</label><br> 
<span>Budget:</span><input type="radio" name="budget" id="radio2"/><label for="radio2">$5000 - $10000</label><br> 
<input type="radio" name="budget" id="radio3"/><label for="radio3">$1,0000 - $25000</label><br> 
<input type="radio" name="budget" id="radio4"/><label for="radio4">75,000 and up</label><br><br> 
<span>How many people? </span><select id="select" name="traveler"> 
    <option>Please Choose</option> 
    <option>1</option> 
    <option>2</option> 
    <option>3</option> 
    <option>4</option> 
    <option>5</option> 
    <option>6</option> 
    <option>7</option> 
    <option>8</option> 
    <option>9</option> 
    <option>10</option> 
    <option>10+</option> 
</select><br><br> 
<span id="some_span">Comments:</span> 
<textarea cols="40" rows="10" name="comments" id="textarea"></textarea><br> 
<input type="checkbox" id="checkbox" name="newsletter"><label id="label1" for="checkbox">Subscribe to FREE online newspaper?</label> 
<input type="reset" id="reset" value="Reset"/> 
<input type="submit" id="submit" value="Send Email"/> 

    </form> 
</body>  
</html> 


//PHP code 




<?php 

/*subject and email validates*/ 

$emailSubject = 'Having fun with PHP'; 
$webMaster = '[email protected]'; 

/* Gathering data variables */ 

    $emailField = $_POST['email']; 
    $nameField = $_POST['name']; 
    $phoneField = $_POST['phone']; 
    $budgetField = $_POST['budget']; 
    $travelersField = $_POST['traveler']; 
    $commentsField = $_POST['comments']; 
    $newsletterField = $_POST['newsletter']; 

    $body = <<<EOD 
<br><hr><br> 
Email: $meail <br> 
Name: $name <br> 
Phone Number: $phone <br> 
Budget: $budget <br> 
Number of Travelers: $traveler <br> 
Comments: $comments <br> 
Newsletter: $newsletter <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> 
<head> 

<title> 
PHP form 
</title> 
<style> 
body{ 
background-color:gray; 
} 
</style> 
</head> 

<body> 
<h1 style="text-align:center; margin-top:40px;">Thank you. Your Email was received. We will answer it soon.</h1>  

</body>  
</html> 
EOD; 

echo "theResults"; 

?> 
+0

爲什麼你在變量'$ theResults'上回顯'echo「theResults」;'當你有一個'HEREDOC'內容時。所以它應該是'echo $ theResults;'對嗎? –

+0

在附註中,您所有的'

+0

你有沒有在你的系統中安裝php? – krishna

回答

-2

我認爲這將解決您的問題

<!DOCTYPE html> 
<html> 
<head>...</head> 
<body> 
    <h1>Please fill out the form below</h1> 
    <form action="form.php" method="post"> 
      <p>E-mail Address: <input type="email" name="email"/></p> 
      <p>Name: <input type="text" name="name"/></p> 
      <p>Phone: <input type="tel" name="phone"/></p> 
      <p><p> 
      <input type="radio" name="budget" id="radio"/><label for="radio">$1,000</label><br> 
      <input type="radio" name="budget" id="radio1"/><label for="radio1">$1000 - $5000</label><br> 
      <span>Budget:</span><input type="radio" name="budget" id="radio2"/><label for="radio2">$5000 - $10000</label><br> 
      <input type="radio" name="budget" id="radio3"/><label for="radio3">$1,0000 - $25000</label><br> 
      <input type="radio" name="budget" id="radio4"/><label for="radio4">75,000 and up</label><br><br> 
      <span>How many people? </span><select id="select" name="traveler"> 
       <option>Please Choose</option> 
       <option>1</option> 
       <option>2</option> 
       <option>3</option> 
       <option>4</option> 
       <option>5</option> 
       <option>6</option> 
       <option>7</option> 
       <option>8</option> 
       <option>9</option> 
       <option>10</option> 
       <option>10+</option> 
      </select><br><br> 
      <span id="some_span">Comments:</span> 
      <textarea cols="40" rows="10" name="comments" id="textarea"></textarea><br> 
      <input type="checkbox" id="checkbox" name="newsletter"><label id="label1" for="checkbox">Subscribe to FREE online newspaper?</label> 
      <input type="reset" id="reset" value="Reset"/> 
      <input type="submit" id="submit" value="Send Email"/> 

    </form> 
</body>  
</html> 


//PHP code 

<?php 
if(isset($_REQUEST['submit'])) 
{ 
/*subject and email validates*/ 

$emailSubject = 'Having fun with PHP'; 
$webMaster = '[email protected]'; 

/* Gathering data variables */ 

    $emailField = $_POST['email']; 
    $nameField = $_POST['name']; 
    $phoneField = $_POST['phone']; 
    $budgetField = $_POST['budget']; 
    $travelersField = $_POST['traveler']; 
    $commentsField = $_POST['comments']; 
    $newsletterField = $_POST['newsletter']; 

    $body = <<<EOD 
<br><hr><br> 
Email: $emailField <br> 
Name: $nameField <br> 
Phone Number: $phoneField <br> 
Budget: $budgetField <br> 
Number of Travelers: $travelersField <br> 
Comments: $commentsField <br> 
Newsletter: $newsletterField <br> 
EOD; 

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

    /* Results rendered as HTML*/ 

    $theResults = <<<EOD 
<html> 
<head> 

<title> 
PHP form 
</title> 
<style> 
body{ 
background-color:gray; 
} 
</style> 
</head> 

<body> 
<h1 style="text-align:center; margin-top:40px;">Thank you. Your Email was received. We will answer it soon.</h1>  

</body>  
</html> 
EOD; 

echo "theResults"; 
} 
?> 
2

我得到點擊提交後下方輸出的只是我的PHP代碼。

如果瀏覽器看到PHP源代碼,那麼PHP沒有被執行,PHP(在這種情況下)是服務器端語言。或者:

  • 你沒有web服務器
  • 您沒有在Web服務器上
  • 安裝PHP支持您還沒有配置服務器來處理PHP指令的文件(例如,通過給文件.php擴展名)
  • 您正在運行的Web服務器,但告訴瀏覽器中的文件與file:// URI加載http:// URI代替不使用它
+0

另外,您使用的變量看起來不正確。例如,您將名稱存儲在'$ nameField'中,但之後使用'$ name'。 –

+0

@Quentin PHP是(在這種情況下)服務器端語言 - PHP始終是服務器端語言! – GuyT

+0

@GuyT - 我想象寫命令行工具嗎?或者PHP-GTK的存在? – Quentin

0

你覆蓋與<?php?> PHP代碼?您是否將文件擴展名設置爲.php

另外,您應該考慮使用相同的變量名稱來獲取實際數據。如果您將POST變量讀入$emailField,但繼續使用完全的其他變量($meail),則沒有幫助。