gpt4 book ai didi

php - 将值从 php 传递到 html

转载 作者:行者123 更新时间:2023-11-29 08:07:22 25 4
gpt4 key购买 nike

嗨,我是 php 新手,我正在 php 中做一个简单的登录表单。用户输入用户名和密码。单击按钮后,我将其重定向到 php 页面。在那里,我使用一个简单的选择查询并使用该用户 ID 和密码检索行数。现在,如果返回行,那么它是有效的,我将他从那里重定向到其他页面。如果不是,我想将一个值传递回同一登录页面(最好是 bool 值),并使用它来启用显示无效凭据的标签。

验证php页面中的代码:

<?php
$user = $_GET['txtUserName'];
$password = $_GET['txtPassword'];
$link = mysql_connect("localhost",username,password);
mysql_select_db(dbname, $link);
$result = mysql_query("SELECT * FROM user_table WHERE user_name='$user' and password='$password'", $link);
$num_rows = mysql_num_rows($result);
if($num_rows!=0)
{
redirection to other pages
}
else
{
//code here to pass back to login form
}
?>

我对 php 非常陌生。尝试简单的形式。

最佳答案

  1. 不要使用GET方法提交包含密码等敏感数据的表单。
  2. 不要将表单提交到另一个页面,而是将其提交到同一页面。将 PHP 代码放在页面顶部。
  3. 初始化变量如 $user = $_POST['txtUserName'] 。现在假设您的输入框是 <input type ='text' name = 'txtUserName'> ,把value='<?PHP echo $user?>输入框中输入属性,就会显示值。

最后,阅读一些教程。互联网上有很多这样的内容 :-) 祝您使用 PHP 好运 :-)

这个演示可能会给您更多想法:

<?PHP

$username = "";
$password = "";
$error = "";

if($_SERVER['REQUEST_METHOD'] == "POST") {
$username = $_POST['txtUserName'];
$password = $_POST['txtPassword'];

if(/*everything OK*/) {
// sign the user in
// redirect to some other page of your choice or not
} else {
$error = "Please try again";
}
}
?>

<html>
... html goes here

<?PHP
if(strlen($error) ) {
echo $error;
}
?>
<form action = "" method = "POST">
<input type = 'text' name = 'txtUserName' value='<?PHP echo $username?>' />
<input type = 'password' name = 'txtPassword' />
<input type = 'submit' value = 'Sign In' />
</form>
?>
... rest of the html

关于php - 将值从 php 传递到 html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22434049/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com