How can I make this html form use internal php instead of an external php file?

huangapple go评论102阅读模式
英文:

How can I make this html form use internal php instead of an external php file?

问题

我正在制作一个简单的HTML表单,使用下面的方法进行提交。

我想把所有的PHP代码都放在HTML文件中。(我知道你可以将contact.htm重命名为contact.php,但这并没有告诉我如何将它从外部文件改为内部代码,以及在哪里放置代码)

有什么建议吗?

表单的一部分:

<form method="post" action="forms/contact.php">
   ......
<button type="submit" tabindex="5">提交联系表单</button>
</form>
......

.php文件:

<?php
$webmaster_email = "me@mail.com";
$thankyou_page = "thanks.html";
$error_page = "errors.html";
$firstname = $_REQUEST['firstname'] ;
$lastname = $_REQUEST['lastname'] ;
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;

$msg = "\r\nFirst Name: " . $firstname . "\r\n\r\nLast Name: " . $lastname . "\r\n\r\nE-mail address: " . $email . "\r\n\r\nMessage: " . $message . "\r\n";

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    mail( "$webmaster_email", "Message on Contact Form", $msg );
    header( "Location: $thankyou_page" );
}

else {
    header( "Location: $error_page" );
}
?>
英文:

I am making a simple html form, using the method below to post.

I want all the php in the htm file. (i know you can just rename contact.htm to contact.php but that does not tell me how to change it from external file to internal code and where to put the code)

Any suggestions?

Part of the .html form: 

&lt;form method=&quot;post&quot; action=&quot;forms/contact.php&quot;&gt;
   ......
&lt;button type=&quot;submit&quot; tabindex=&quot;5&quot;&gt;Submit Contact Form&lt;/button&gt;
&lt;/form&gt;
......

.php file:

&lt;?php
$webmaster_email = &quot;me@mail.com&quot;;
$thankyou_page = &quot;thanks.html&quot;;
$error_page = &quot;errors.html&quot;;
$firstname = $_REQUEST[&#39;firstname&#39;] ;
$lastname = $_REQUEST[&#39;lastname&#39;] ;
$email = $_REQUEST[&#39;email&#39;] ;
$message = $_REQUEST[&#39;message&#39;] ;

$msg = &quot;\r\nFirst Name: &quot; . $firstname . &quot;\r\n\r\nLast Name: &quot; . $lastname . &quot;\r\n\r\nE-mail address: &quot; . $email . &quot;\r\n\r\nMessage: &quot; . $message . &quot;\r\n&quot;;

if ($_SERVER[&#39;REQUEST_METHOD&#39;] === &#39;POST&#39;) {
 	mail( &quot;$webmaster_email&quot;, &quot;Message on Contact Form&quot;, $msg );
	header( &quot;Location: $thankyou_page&quot; );
}

else {
	header( &quot;Location: $error_page&quot; );
}
?&gt;

答案1

得分: 2

要在HTML文件中直接包含PHP代码,您需要将HTML文件保存为.php扩展名,然后使用<?php ... ?>标签将PHP代码嵌入HTML中。以下是您可以这样做的方法:

  1. 将HTML文件保存为.php扩展名,例如contact.php。

  2. 修改HTML表单以包含其中的PHP代码:

<!DOCTYPE html>
<html>

<head>
	<title>Contact Form</title>
</head>

<body>
	<?php
		$webmaster_email = "me@mail.com";
		$thankyou_page = "thanks.html";
		$error_page = "errors.html";
		if ($_SERVER['REQUEST_METHOD'] === 'POST') {
			$firstname = $_POST['firstname'];
			$lastname = $_POST['lastname'];
			$email = $_POST['email'];
			$message = $_POST['message'];
			$msg = "\r\nFirst Name: " . $firstname . "\r\n\r\nLast Name: " . $lastname . "\r\n\r\nE-mail address: " . $email . "\r\n\r\nMessage: " . $message . "\r\n";
			if (mail($webmaster_email, "Message on Contact Form", $msg)) {
				header("Location: $thankyou_page");
				exit();
			} else {
				header("Location: $error_page");
				exit();
			}
		}
	?>
	<form method="post" action="">
		<label for="firstname">First Name:</label>
		<input type="text" name="firstname" id="firstname" required>
		.....
		<button type="submit" tabindex="5">Submit Contact Form</button>
	</form>
</body>

</html>

以上是将PHP代码直接嵌入HTML文件的方法。请确保您的服务器支持PHP,并且将文件保存为.php扩展名以便正确解析PHP代码。

英文:

To include the PHP code directly within your HTML file, you need to save your HTML file with a .php extension and then embed the PHP code within the HTML using the <?php ... ?> tags. Here's how you can do it:

Save your HTML file with a .php extension, for example, contact.php.

Modify your HTML form to include the PHP code within it:

<!DOCTYPE html>
<html>

     &lt;head&gt;
     	&lt;title&gt;Contact Form&lt;/title&gt;
     &lt;/head&gt;

     &lt;body&gt;
     	&lt;?php
			$webmaster_email = &quot;me@mail.com&quot;;
			$thankyou_page = &quot;thanks.html&quot;;
			$error_page = &quot;errors.html&quot;;
			if ($_SERVER[&#39;REQUEST_METHOD&#39;] === &#39;POST&#39;) {
				$firstname = $_POST[&#39;firstname&#39;];
				$lastname = $_POST[&#39;lastname&#39;];
				$email = $_POST[&#39;email&#39;];
				$message = $_POST[&#39;message&#39;];
				$msg = &quot;\r\nFirst Name: &quot; . $firstname . &quot;\r\n\r\nLast Name: &quot; . $lastname . &quot;\r\n\r\nE-mail address: &quot; . $email . &quot;\r\n\r\nMessage: &quot; . $message . &quot;\r\n&quot;;
				if (mail($webmaster_email, &quot;Message on Contact Form&quot;, $msg)) {
					header(&quot;Location: $thankyou_page&quot;);
					exit();
				} else {
					header(&quot;Location: $error_page&quot;);
					exit();
				}
			}
			?&gt;
     	&lt;form method=&quot;post&quot; action=&quot;&quot;&gt;
     		&lt;label for=&quot;firstname&quot;&gt;First Name:&lt;/label&gt;
     		&lt;input type=&quot;text&quot; name=&quot;firstname&quot; id=&quot;firstname&quot; required&gt;
     		.....
     		&lt;button type=&quot;submit&quot; tabindex=&quot;5&quot;&gt;Submit Contact Form&lt;/button&gt;
     	&lt;/form&gt;
     &lt;/body&gt;

     &lt;/html&gt;

答案2

得分: 0

如果我理解正确,你想要将所有内容放在一个文件中。

首先,你需要将文件重命名为.php文件。然后,如果表单元素的action属性为空,表单将提交到同一页(例如:重新加载带有表单数据的页面)。

然后,你可以将所有的PHP代码放在顶部,并检查是否有POST数据被提交。

<?php
if ($_POST) {
   $webmaster_email = "me@mail.com";
   $thankyou_page = "thanks.html";
   $error_page = "errors.html";
   $firstname = $_POST['firstname'];
   $lastname = $_POST['lastname'];
   $email = $_POST['email'];
   $message = $_POST['message'];

   $msg = "\r\nFirst Name: " . $firstname . "\r\n\r\nLast Name: " . $lastname . "\r\n\r\nE-mail address: " . $email . "\r\n\r\nMessage: " . $message . "\r\n";

   mail("$webmaster_email", "Message on Contact Form", $msg);
   header("Location: $thankyou_page");
}
?>
<form method="post" action="">
   ......
<button type="submit" tabindex="5">提交联系表单</button>
</form>

以上是翻译好的内容。

英文:

If I understand correctly, you want to have everything in one file.

You would first need to rename your file to a .php file. Then, if the action attribute is empty on the form element the form will submit to the same page (eg: reloads the page with the form data).

Then you can have all of the PHP code in the top and just have a check if there is POST data being submitted.

&lt;?php
if( $_POST ){
   $webmaster_email = &quot;me@mail.com&quot;;
   $thankyou_page = &quot;thanks.html&quot;;
   $error_page = &quot;errors.html&quot;;
   $firstname = $_POST[&#39;firstname&#39;] ;
   $lastname = $_POST[&#39;lastname&#39;] ;
   $email = $_POST[&#39;email&#39;] ;
   $message = $_POST[&#39;message&#39;] ;

   $msg = &quot;\r\nFirst Name: &quot; . $firstname . &quot;\r\n\r\nLast Name: &quot; . $lastname . &quot;\r\n\r\nE-mail address: &quot; . $email . &quot;\r\n\r\nMessage: &quot; . $message . &quot;\r\n&quot;;

   mail( &quot;$webmaster_email&quot;, &quot;Message on Contact Form&quot;, $msg );
   header( &quot;Location: $thankyou_page&quot; );
}?&gt;

&lt;form method=&quot;post&quot; action=&quot;&quot;&gt;
   ......
&lt;button type=&quot;submit&quot; tabindex=&quot;5&quot;&gt;Submit Contact Form&lt;/button&gt;
&lt;/form&gt;

答案3

得分: 0

以下是翻译好的内容:

将整个内容保存为一个名为contact.php的文件,代码如下:

<form method="post"><!--将action移除,因为表单提交后会返回到当前页面-->
    ......
    <button type="submit" tabindex="5">提交联系表单</button>
</form>
......

<!--PHP部分....-->
<?php
$webmaster_email = "me@mail.com";
$thankyou_page = "thanks.html";
$error_page = "errors.html";
$firstname = $_REQUEST['firstname'];
$lastname = $_REQUEST['lastname'];
$email = $_REQUEST['email'];
$message = $_REQUEST['message'];

$msg = "\r\n名字: " . $firstname . "\r\n\r\n姓氏: " . $lastname . "\r\n\r\n电子邮件地址: " . $email . "\r\n\r\n留言内容: " . $message . "\r\n";

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    mail("$webmaster_email", "联系表单上的留言", $msg);
    header("Location: $thankyou_page");
} else {
    header("Location: $error_page");
}
?>
英文:

The entire content in a single file as contact.php. code as follows...

&lt;form method=&quot;post&quot;&gt;&lt;!--remove action as it comes back here(this page) only--&gt;
......
&lt;button type=&quot;submit&quot; tabindex=&quot;5&quot;&gt;Submit Contact Form&lt;/button&gt;
&lt;/form&gt;
......

&lt;!--php part....--&gt;

&lt;?php
$webmaster_email = &quot;me@mail.com&quot;;
$thankyou_page = &quot;thanks.html&quot;;
$error_page = &quot;errors.html&quot;;
$firstname = $_REQUEST[&#39;firstname&#39;] ;
$lastname = $_REQUEST[&#39;lastname&#39;] ;
$email = $_REQUEST[&#39;email&#39;] ;
$message = $_REQUEST[&#39;message&#39;] ;

$msg = &quot;\r\nFirst Name: &quot; . $firstname . &quot;\r\n\r\nLast Name: &quot; . $lastname . &quot;\r\n\r\nE-mail address: &quot; . $email . &quot;\r\n\r\nMessage: &quot; . $message . &quot;\r\n&quot;;

if ($_SERVER[&#39;REQUEST_METHOD&#39;] === &#39;POST&#39;) {
    mail( &quot;$webmaster_email&quot;, &quot;Message on Contact Form&quot;, $msg );
    header( &quot;Location: $thankyou_page&quot;);
}

else {
    header( &quot;Location: $error_page&quot; );
}
?&gt;

答案4

得分: -1

我现在已经搞定了。非常感谢大家的帮助!以下是我是如何做到的:

第一行(必须是最开始的一行):

<?php ob_start(); ?>   // 我不知道这是什么,但它让它工作了!

其余的代码可以放在任何你想要的地方(一起放,php代码放在最上面):

<?php
$webmaster_email = "email@domain.com";
$thankyou_page = "thanks.html";
$name = $_REQUEST['name'] ;
$comment = $_REQUEST['comment'] ;
if (empty($name)) { $name='Anonymous';  }
$msg = "\r\nName: " . $name . "\r\n\r\nComment: " . $comment . "\r\n";	
if ($_SERVER['REQUEST_METHOD'] === 'POST') {   // 如果点击了提交按钮
    mail( "$webmaster_email", "Comment on domain.com", $msg );
    header( "Location: $thankyou_page" ); }   // 跳转到感谢页面
?>
<form method="post" action="">
    <label><h4>Name</h4>
    <input type="text" name="name" tabindex="1">
    </label>
    <br>
    <font size="3">
    <label><h4>Comment</h4>
    <textarea name="comment" minlength="10" maxlength="650" tabindex="2"> 
    </textarea>
    </label>
    <br>
    <button type="submit" tabindex="3">Submit Comment</button>
</form>
英文:

I got it working now. Thank you so much everyone for the help guys! Here is how I did it:

Line 1 (Must be the very first line):

&lt;?php ob_start(); ?&gt;   // I don&#39;t know what this is but it made it work!

The Rest of the code wherever you want it (together, php right on top):

&lt;?php
$webmaster_email = &quot;email@domain.com&quot;;
$thankyou_page = &quot;thanks.html&quot;;
$name = $_REQUEST[&#39;name&#39;] ;
$comment = $_REQUEST[&#39;comment&#39;] ;
if (empty($name)) { $name=&#39;Anonymous&#39;;  }
$msg = &quot;\r\nName: &quot; . $name . &quot;\r\n\r\nComment: &quot; . $comment . &quot;\r\n&quot;;	
if ($_SERVER[&#39;REQUEST_METHOD&#39;] === &#39;POST&#39;) {   // if clicked submit button
    mail( &quot;$webmaster_email&quot;, &quot;Comment on domain.com&quot;, $msg );
    header( &quot;Location: $thankyou_page&quot; ); }   // takes you to thank you page
?&gt;
&lt;form method=&quot;post&quot; action=&quot;&quot;&gt;
    &lt;label&gt;&lt;h4&gt;Name&lt;/h4&gt;
    &lt;input type=&quot;text&quot; name=&quot;name&quot; tabindex=&quot;1&quot;&gt;
    &lt;/label&gt;
    &lt;br&gt;
    &lt;font size=&quot;3&quot;&gt;
    &lt;label&gt;&lt;h4&gt;Comment&lt;/h4&gt;
    &lt;textarea name=&quot;comment&quot; minlength=&quot;10&quot; maxlength=&quot;650&quot; tabindex=&quot;2&quot;&gt; 
    &lt;/textarea&gt;
    &lt;/label&gt;
    &lt;br&gt;
    &lt;button type=&quot;submit&quot; tabindex=&quot;3&quot;&gt;Submit Comment&lt;/button&gt;
&lt;/form&gt;

huangapple
  • 本文由 发表于 2023年8月8日 22:32:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76860555.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定