简体   繁体   English

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

[英]HTML form not getting submitted. PHP code not working properly

I have created this HTML form and coded PHP trying to get the user information getting sent to my e-mail after the user fills out the form and clicks on submit button. 我创建了此HTML表单并编写了PHP代码,试图在用户填写表单并单击“提交”按钮后将用户信息发送到我的电子邮件中。 However as a result I get just my PHP code outputted after clicking on submit button. 但是,结果是我只单击了提交按钮,就得到了我的PHP代码输出。 Please take a look at my code and let me know what I am doing wrong. 请看一下我的代码,让我知道我做错了什么。 Thanks in advance. 提前致谢。

HTML Code 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";

?>

I get just my PHP code outputted after clicking on submit bottom. 单击提交底部后,我只得到我的PHP代码输出。

PHP is (in this context) a server side language, if the browser is seeing the PHP source then the PHP is not being executed. 在这种情况下,PHP是一种服务器端语言,如果浏览器看到的是PHP源代码,则说明该PHP没有被执行。 Either: 要么:

  • You have no webserver 您没有网络服务器
  • You have not installed PHP support on the webserver 您尚未在网络服务器上安装PHP支持
  • You have not configured the server to process the file for PHP directives (eg by giving the file a .php extension) 您尚未配置服务器以处理PHP指令的文件(例如,给文件扩展名.php
  • You are running a webserver but are telling the browser to load the files with a file:// URI instead of an http:// URI so it isn't being used 您正在运行网络服务器,但告诉浏览器使用file:// URI而不是http:// URI加载文件,因此该file://未被使用

Are you covering the PHP code with <?php and ?> ? 您是否用<?php?>覆盖了PHP代码? Dou you set the file extension to .php ? 您是否将文件扩展名设置为.php

Also, you should consider to use the same variable names to get also the actual data. 另外,您应该考虑使用相同的变量名称来获取实际数据。 It is not helpful if you read the POST variable into $emailField , but then continuing to use a completely other variable ( $meail ). 如果将POST变量读入$emailField ,然后继续使用完全其他的变量( $meail ),则没有帮助。

I think this will solve your problem 我认为这可以解决您的问题

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM