HTML表单未提交。 PHP代码无法正常工作

时间:2014-02-12 10:12:01

标签: php html forms validation

我创建了这个HTML表单并编写了PHP,试图在用户填写表单并单击“提交”按钮后将用户信息发送到我的电子邮件。但是结果我在点击提交按钮后输出了我的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 = 'Vladimirg808@gmail.com';

 /* 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";

?>

3 个答案:

答案 0 :(得分:2)

  

点击提交底部后,我只输出了我的PHP代码。

PHP(在此上下文中)是服务器端语言,如果浏览器看到PHP源,则PHP不会被执行。之一:

  • 您没有网络服务器
  • 您尚未在网络服务器上安装PHP支持
  • 您尚未将服务器配置为处理PHP指令的文件(例如,通过为文件提供.php扩展名)
  • 您正在运行网络服务器但是告诉浏览器使用file:// URI加载文件而不是http:// URI,因此未使用

答案 1 :(得分:0)

您是否使用<?php?>覆盖了PHP代码?你把文件扩展名设置为.php

此外,您应该考虑使用相同的变量名来获取实际数据。如果您将POST变量读入$emailField,然后继续使用完全其他变量($meail),则无效。

答案 2 :(得分:-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 = 'Vladimirg808@gmail.com';

 /* 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";
}
?>