将PHP代码放在HTML文件中的位置(对于联系表单)?

I have an html file (i.e. index.html). I have html code for my contact form (i.e. name, email, etc.. fields). Where do I put my PHP code without turning the index.html into a index.php file, if it is possible?

If you want the php code to be interpreted instead of being shown, then somehow the http server answering the request to that file has to know that is it meant to start interpretation of the code. Typically that is done using the file name extension .php.

You certainly could configure your http server such that the same will be performed when .html files are requested. But that would be a very bad approach. Don't do it.

So rename your file to index.php and go with that. It is easiest, transparent and robust.

If you are concerned with how your URL looks, then take a look at "rewriting" and "pretty URLs" which allow to use URLs like http://my.server/index or simply http://my.server/ for the same. Rewriting inside the http server takes care to internally map such requests to your index.php file without changing the URL visible in the users browser.

However there is one thing I would like to mention here, since I often see beginners to fall into a that issue: if that php code of yours only contains the processing of a posted form, so maybe validation and storage, then there is absolutely no reason to put it into the same file as the html markup! In contrary, don't! Use a separate file for processing, after having processed forward the browser to another view. Only if you need dynamic values getting inserted into your html markup, then it makes sense to embed php into your markup. That is exactly what php was originally created for and you should do that by means of turning your static html files into php files (.php name extension).

It's actually not that difficult.

1) In your opening form tag <form action="contact-proc.php" method="post" name="contact" id="contact"> make sure you have an action (the processing page that collects the data and sends the email).

html page sample

<form action="contact-proc.php" method="post" name="contact" id="contact">
<table border="0" align="center" cellpadding="5" cellspacing="0">
  <tr>
    <td colspan="2" height="5px"></td>
  </tr>
  <tr>
    <td><div align="right"><strong><font color="#FF0000">*</font>Name:</strong></div></td>
    <td><input name="name" type="text" size="35" /></td>
  </tr>
  <tr>
    <td><div align="right"><strong><font color="#FF0000">*</font>E-Mail Address:</strong></div></td>
    <td><input name="email" type="text" size="35" /></td>
  </tr>
  <tr>
    <td><div align="right"><strong><font color="#FF0000">*</font>Message:</strong></div></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td colspan="2"><textarea name="comments" cols="50" rows="10" wrap="virtual"></textarea></td>
  </tr>
  <tr>
    <td align="center"><div align="center"><input name="Submit" type="submit" value="Send" /></div></td>
  </tr>
</table>
</form>

2) Create a page to process the data and send the email. In this case contact-proc.php

3) On the processing page include something like this...

<?php
$email = 'your-email-address@domain.ext';
$domain = 'your-domain-name'; // ie: google.com
$strVisitorInfo = ''.
"Customer Service Request from Your WebSite

" .
"Name    : ".$_POST['name']."
" .
"E-Mail   : ".$_POST['email']."
" .
"Comments : 
" .
$_POST['comments'];

$to = $Email;
$subject = "Customer Service Request from Your WebSite";
$headers = 'From: noreply@'.$domain . "
" .
'Reply-To: noreply@'.$domain . "
" .
'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $strVisitorInfo, $headers);
?>

Where $_POST['name'] $_POST['email'] & $_POST['comments'] are the name of the form elements on your html page. You can change those to match your form.

On your processing page you can include some information letting the visitor know that there email has been sent. Like...

<div align="center">Thank you <?php echo $_POST['name'] ?>.<br>
Your email has been sent. We will be in touch with you soon.
</div>

Of course you will want to do some validation to make sure empty forms and other nonsense are submitted. But that's a different question than what is asked here.