php脚本正在显示它自己

时间:2013-03-22 08:39:18

标签: php html

我是php的新手。我正在使用WAMP。我的问题是,如果编写PHP代码并使用localhost / abcd.php测试它工作正常,但当我使用相同的代码链接到HTML时,它显示的是PHP代码本身。为什么这发生了我保存所有文件和ansi并开始所有服务

这是html代码

enter code here


 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
   <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
   <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
   <title>Aliens Abducted Me - Report an Abduction</title>
    <link rel="stylesheet" type="text/css" href="style.css" />
    </head>
  <body>
  <h2>Aliens Abducted Me - Report an Abduction</h2>

   <p>Share your story of alien abduction:</p>
  <form action="report.php" method="post" >
  <label for="firstname">First name:</label>
  <input type="text" name="firstname" /><br />
  <label for="lastname">Last name:</label>
  <input type="text" name="lastname" /><br />
  <label for="email">What is your email address?</label>
  <input type="text" name="email" /><br />
   <label for="whenithappened">When did it happen?</label>
   <input type="text" name="whenithappened" /><br />
   <label for="howlong">How long were you gone?</label>
   <input type="text" name="howlong" /><br />
   <label for="howmany">How many did you see?</label>
   <input type="text" name="howmany" /><br />
   <label for="aliendescription">Describe them:</label>
   <input type="text" name="aliendescription" size="32" /><br />
   <label for="whattheydid">What did they do to you?</label>
   <input type="text" name="whattheydid" size="32" /><br />
   <label for="fangspotted">Have you seen my dog Fang?</label>
    Yes <input name="fangspotted" type="radio" value="yes" />
   No <input name="fangspotted" type="radio" value="no" /><br />
 <img src="fang.jpg" width="100" height="175"
  alt="My abducted dog Fang." /><br />
  < label for="other">Anything else you want to add?</label>
  <textarea name="other"></textarea><br />
  <input type="submit" value="Report Abduction" name="submit" />
   </form>
    </body>
   </html>

这里是PHP代码

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
   <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
   <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Aliens Abducted Me - Report an Abduction</title>
   </head>
   <body>
  <h2>Aliens Abducted Me - Report an Abduction</h2>

   <?php
 $first_name = $_POST['firstname'];
  $last_name = $_POST['lastname'];
 $when_it_happened = $_POST['whenithappened'];
 $how_long = $_POST['howlong'];
$how_many = $_POST['howmany'];
$alien_description = $_POST['aliendescription'];
$what_they_did = $_POST['whattheydid'];
$fang_spotted = $_POST['fangspotted'];
$email = $_POST['email'];
$other = $_POST['other'];

$dbc = mysqli_connect('data.aliensabductedme.com', 'owen', 'aliensrool',      'aliendatabase')
or die('Error connecting to MySQL server.');

 $query = "INSERT INTO aliens_abduction (first_name, last_name, when_it_happened,       how_long, " .
"how_many, alien_description, what_they_did, fang_spotted, other, email) " .
"VALUES ('$first_name', '$last_name', '$when_it_happened', '$how_long', '$how_many', " .
"'$alien_description', '$what_they_did', '$fang_spotted', '$other', '$email')";

 $result = mysqli_query($dbc, $query)
 or die('Error querying database.');

mysqli_close($dbc);

echo 'Thanks for submitting the form.<br />';
echo 'You were abducted ' . $when_it_happened;
echo ' and were gone for ' . $how_long . '<br />';
echo 'Number of aliens: ' . $how_many . '<br />';
echo 'Describe them: ' . $alien_description . '<br />';
echo 'The aliens did this: ' . $what_they_did . '<br />';
echo 'Was Fang there? ' . $fang_spotted . '<br />';
echo 'Other comments: ' . $other . '<br />';
echo 'Your email address is ' . $email;
 ?>

    

1 个答案:

答案 0 :(得分:1)

网络服务器根据文件后缀决定做什么。在网络服务器的设置中,您可以例如设置* .html文件由PHP解析或不解析。通常* .php文件被处理为PHP文件和* .html文件不是。如果你想在html文件中使用php代码,你必须做两件事:

  1. 设置您的网络服务器以使用php for * .html文件
  2. 嵌入您的代码,如下所示
  3. 示例index.html

    <html>
      <head>
      </head>
      <body>
      <?php
        echo("Hello World");
      ?>
      </body>
    </html>