简体   繁体   English

如何将php文件链接到html文件

[英]how to link a php file to a html file

Is it possible to link a PHP file to an HTML file? 是否可以将PHP文件链接到HTML文件?

I have a form in HTML code, and I want the data from the form to be processed by the PHP code I have written and to be stored in a database. 我有一个使用HTML代码的表单,并且我希望表单中的数据由我编写的PHP代码处理并存储在数据库中。 The code is ready and when I have the HTML code and PHP code in a single PHP file it works fine. 代码已经准备好,当我在单个PHP文件中包含HTML代码和PHP代码时,它工作正常。 But what I need is to have two separate files; 但是我需要有两个单独的文件; one in HTML and one in PHP. 一种是HTML,另一种是PHP。 How can I do that? 我怎样才能做到这一点?

If you have the form and the PHP code on the same page and it works, then your opening form tag either doesn't have an action attribute, has a blank action attribute, or has an action attribute explicitly set to the URI of the same page that contains the form. 如果您在同一页面上拥有表单和PHP代码并且可以正常工作,则您的开始表单标签要么没有action属性,要么没有action属性,要么将action属性显式设置为相同的URI包含表单的页面。

All you need to do is change that so that the form submits to the separate PHP script instead. 您需要做的就是更改此内容,以便将表单提交到单独的PHP脚本。

<form action="something.php"> ...

(Of course, you'll also need to move your PHP code to something.php . And obviously "something" is just for example. I'm sure you have some sort of meaningful name you'll be using instead.) (当然,您还需要将PHP代码移动到something.php 。显然,“ something”仅是示例。我确定您会使用某种有意义的名称来代替。)

index.html 的index.html

<form action="some.php" method="POST">
<input type="text" name="inp">
<input type="submit" name="submit" value="submit">
</form>

some.php some.php

echo $_POST['inp'];

In addition to the answer by "Don't Panic" dont forget to set the method attribute in your form tag to match your php code most likely it is POS or GET 除了“不要惊慌”的答案外,别忘了在表单标签中设置method属性以匹配您的php代码,最有可能是POS或GET

<form action="something.php" method="POST"> 

or 要么

<form action="something.php" metod="GET"> 

and you you can omit the method="GET" since it is the default method. 并且您可以省略method =“ GET”,因为它是默认方法。 It worth mentioning that POST is more secure and can carry more data since it uses the actual request body unlike GET that appends the parameters to the url. 值得一提的是POST更安全,并且可以携带更多数据,因为它使用实际的请求主体,而不像GET那样将参数附加到url。

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

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