将 php 代码链接到 html 页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26534018/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Link php code to a html page
提问by Charley Baker
OK, so I have a entry form login page saved as a .php file, its content is displayed in html and css. It displays a few things and then a log in form with 2 fields: Username, Password and then a 'Log in' button.
好的,所以我有一个条目表单登录页面保存为 .php 文件,其内容以 html 和 css 显示。它会显示一些内容,然后是一个包含 2 个字段的登录表单:用户名、密码,然后是“登录”按钮。
I have created a separate .php file which looks like this:
我创建了一个单独的 .php 文件,如下所示:
<?php
$username = $_POST ['Username'];
$password = $_POST['Password'];
if ($username && $password)
{
$connect = mysql_connect("localhost", "root", "root") or die ("couldn't connect");
mysql_select_db("sportsday") or die ("couldn't find db");
}
else
die("Please enter a username and password");
?>
I believe this code is now connected to my other page (http://jsfiddle.net/6yw8a2ue/) It shows some errors next to the fields in browser: "Notice: Undefined indiex: Username in G:\xampp\htdocs\Sportsday\entryformlongon.php"...
我相信此代码现在已连接到我的另一个页面(http://jsfiddle.net/6yw8a2ue/)它在浏览器中的字段旁边显示了一些错误:“注意:未定义的索引:用户名在 G:\xampp\htdocs\Sportsday \entryformlongon.php"...
and once i enter a username and password it goes through to blank page with the error: "Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in G:\xampp\htdocs\Sportsday\login.php on line 11"
一旦我输入了用户名和密码,它就会进入空白页面并显示错误:“已弃用:mysql_connect():不推荐使用 mysql 扩展并将在未来删除:在 G:\xampp\htdocs 中使用 mysqli 或 PDO 代替\Sportsday\login.php 第 11 行”
回答by iamcutler
Please look over this documentation. http://php.net/manual/en/function.require-once.php
请查看此文档。 http://php.net/manual/en/function.require-once.php
You can require the file in any page needed.
您可以在所需的任何页面中要求该文件。
回答by danielson317
point your form action atribute to the php file.
将您的表单操作属性指向 php 文件。
<form method="post" action="phpsubmit.php">
form stuff
</form>
回答by Mark Hulbrock
Require at the top of the page to be included. require 'filename';
需要包含在页面顶部。需要“文件名”;
回答by Adrian Stride
header('Location: http://www.example.com/');
exit;
I assume by link to other php file, you mean forward to another page once complete.
我假设通过链接到其他 php 文件,您的意思是在完成后转发到另一个页面。
回答by Salim Khan
You can redirect to other page when fields are empty or inputs are not matching.Can simply do
当字段为空或输入不匹配时,您可以重定向到其他页面。可以简单地做
$username=$row["username"];
$password=$row["password"];
header("location:page_to_link.php?u=".$username);
}
else{
header("location:page you want to redirect");
}
you have to start session on the page you redirecting(linking) them by session_start().
您必须在通过 session_start() 重定向(链接)它们的页面上启动会话。
Hope that helps
希望有帮助
回答by Ian
There are 4 options to include the file into the main document.
有 4 个选项可以将文件包含到主文档中。
include
include_once
require
require_once
include doc, include_once doc, require doc, require_once doc
包含文档,包含_once 文档,需要文档,需要_once 文档
Each of these will add the code to your main file and do essentially the same thing. include
and require
will include the file as many times as the line of code is called (which can cause issues if you just need it called once). include_once
and require_once
will import the file to the document only one time regardless of whether or not that line of code is called multiple times.
这些中的每一个都会将代码添加到您的主文件中,并且基本上做相同的事情。include
并且require
将在调用代码行时多次包含该文件(如果您只需要调用一次,这可能会导致问题)。include_once
并且require_once
只会将文件导入文档一次,而不管该行代码是否被多次调用。
A big difference between include
and require
lies in the type of error you will get. include
will return a warning error if the file isn't found but your code will continue to execute (with errors). require
will produce a fatal error which will kill the program then and there.
include
和之间的很大区别require
在于您将得到的错误类型。include
如果找不到文件,将返回警告错误,但您的代码将继续执行(有错误)。require
将产生一个致命错误,该错误会在当时和那里杀死程序。
So if you have form.php and want to include it it would look something like this for each option.
因此,如果您有 form.php 并希望包含它,那么对于每个选项,它看起来都像这样。
include 'form.php';
include_once 'form.php';
require 'form.php';
require_once 'form.php';
Depending on where the document lies that you are trying to include you will need to get the correct directory first (ie if you are trying to include from from /path/file.php from /path/path/file.php). Unlike normal HTML '/'
does not result in your site root. It results in your server root.
根据您尝试包含的文档所在的位置,您需要首先获得正确的目录(即,如果您尝试从 /path/path/file.php 中的 /path/file.php 中包含)。不像普通的 HTML'/'
不会导致你的站点根。它会导致您的服务器根目录。
So if you have a file that exists at website.com/path/file.php you couldn't just do include '/path/file.php' because that would actually look for the file at the root of the server which would not exist.
因此,如果您有一个存在于 website.com/path/file.php 的文件,您不能只包含 '/path/file.php',因为这实际上会在服务器的根目录中查找该文件,而该文件不会存在。
To get around this you will want to utilize $_SERVER['DOCUMENT_ROOT']
which will then grab the root of your document from the server which might look something like the following (depending on your setup):
为了解决这个问题,您需要使用$_SERVER['DOCUMENT_ROOT']
它然后从服务器获取文档的根目录,它可能如下所示(取决于您的设置):
/var/www/html/website.com
/var/www/html/website.com
And so rather than having to figure that out, you can just add the document root line to the include statements
因此,不必弄清楚这一点,您只需将文档根行添加到包含语句中
include $_SERVER['DOCUMENT_ROOT'] . '/path/file.php';
include_once $_SERVER['DOCUMENT_ROOT'] . '/path/file.php';
require $_SERVER['DOCUMENT_ROOT'] . '/path/file.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/path/file.php';
回答by jsp
You have forgotten to check if the form is submitted add the floowing in the beginning of your code:
您忘记检查表单是否已提交,请在代码开头添加流:
if(isset($_POST ['Username']) && isset($_POST ['Password'])){
if(isset($_POST ['Username']) && isset($_POST ['Password'])){
and off course, don't forget to place a close bracket at the end
当然,不要忘记在最后放置一个右括号