Php登录页面如何给action这个位置加上javascript怎么获取用户输入Id呢?

PHP+MYsql+Ajax实现省市区三级联动和用户注册/登录验证.项目目录数据库设计省市区数据库用户数据库数据库信息页面“conn.inc.php”<?php
define("HOST",'localhost');
define("USER",'root');
define("PWD",'Aa123123');
define("DBNAME",'news');
?>
数据库连接页面“mysqli.php”<?php
include 'conn.inc.php';
$mysqli=new mysqli(HOST,USER,PWD,DBNAME);
if($mysqli->connect_errno){
die('数据库链接出错'.$mysqli->connect_error);
}
?>
账户注册页面“region.php”<!DOCTYPE html>
<html>
<head>
<title>用户注册</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script src="../jquery/jquery-1.11.0.js" type="text/javascript"></script>
<script src="../js/showcity.js" type="text/javascript"></script>
</head>
<body>
<form method="post" action="changecity.php">
账户:<input id="account" name="account" type="text" />
密码:<input id="password" name="password" type="password"/>
</br>
<sapn id="showcity">我的位置:</span>
<select id="provinces" name="provinces"><option value="">请选择省</option></select>&nbsp;
<select id="citys" name="citys"><option value="">请选择市</option></select>&nbsp;
<select id="countys" name="countys"><option value="">请选择区/县</option></select>&nbsp; </br>
<input type="submit" value="注册"/>
</form>
</body>
</html>
省市区三级联动信息,ajax请求后台获取数据“showcity.js”// JavaScript Document
$(document).ready(function() {
//
加载所有的省份
$.ajax({
type: "get",
url: "region_action.php", // type=1表示查询省份
data: {"parent_id": "1", "type": "1"},
dataType: "json",
success: function(data) {
$("#provinces").html("<option value=''>请选择省</option>");
$.each(data, function(i, item) {
// alert(item.region_id);
$("#provinces").append("<option value='" + item.region_id + "'>" + item.region_name + "</option>");
});
}
});
$("#provinces").change(function() {
$.ajax({
type: "get",
url: "region_action.php", // type =2表示查询市
data: {"parent_id": $(this).val(), "type": "2"},
dataType: "json",
success: function(data) {
$("#citys").html("<option value=''>请选择市</option>");
$.each(data, function(i, item) {
$("#citys").append("<option value='" + item.region_id + "'>" + item.region_name + "</option>");
});
}
});
});
$("#citys").change(function() {
$.ajax({
type: "get",
url: "region_action.php", // type =2表示查询市
data: {"parent_id": $(this).val(), "type": "3"},
dataType: "json",
success: function(data) {
$("#countys").html("<option value=''>请选择区/县</option>");
$.each(data, function(i, item) {
$("#countys").append("<option value='" + item.region_id + "'>" + item.region_name + "</option>");
});
}
});
});
//显示地址
/*
$("#countys").change(function() {
var value = $("#provinces").find("option:selected").text()
+ $("#citys").find("option:selected").text()
+ $("#countys").find("option:selected").text();
// alert(value);
$("#region").text("选择的位置:"+ value);
});
*/
});
省市区信息查询页面"region_action.php"<?php
header("Content-Type:text/html;charset=utf-8");
include '../conn/mysqli.php';
$type = isset($_GET["type"]) ? $_GET["type"] : "";
$parent_id = isset($_GET["parent_id"]) ? $_GET["parent_id"] : "";
// 链接数据库
if ($type == ""
$parent_id == "") {
exit(json_encode(array("flag" => false, "msg" => "查询类型错误")));
} else {
// 链接数据库
$sql="select *from global_region where parent_id=$parent_id AND region_type=$type";
$result=$mysqli->query($sql);
if($result->num_rows>0)
{
$arr=[];
while ($row=$result->fetch_assoc())
{
$arr[$row["region_id"]]['region_id']=$row["region_id"];//$arr[1]["title"]=$row["title"]
$arr[$row["region_id"]]['region_name']=$row["region_name"];//$arr[1]["content"]=$arr["content"]
}
}
$provinces_json = json_encode($arr);
exit($provinces_json);
}
?>
账户登录页面"login.php"<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>用户登录</title>
</head>
<body>
<form method="post" action="checklogin.php" >
账户:<input id="account" name="account" type="text"/>
密码:<input id="password" name="password" type="password"/>
<input id="login" name="login" type="submit" value="登录"/>
<input id="reset" name="reset" type="reset" value="重置"/>
<a href="../register/region.php" target="_parent">没有账号,前往注册</a>
</form>
</body>
</html>
账户注册验证页面"changecity.php"<?php
header("Content-Type:text/html;charset=utf-8");
$a = trim($_POST["provinces"]);
//获取选择省份的ID
$b = trim($_POST["citys"]);
// 获取选择市的ID
$c = trim($_POST["countys"]);
//获取选择区的ID
$d = trim($_POST["account"]);
//获取提交的账户
$e = md5(trim($_POST["password"]));
//获取提交的密码并用md5算法加密
$f = trim($_POST["password"]);
//获取提交的明文密码
include("../conn/mysqli.php");
//引入数据库连接核心文件
//判断省市区提交的ID信息是否为空
if($a=="" && $b=="" && $c==""){
echo "<script>setTimeout(function(){window.location.href='region.php';},1000);</script>";
exit("未选择省市区信息!");
}
//查询省份ID对应的省名称,并以数组方式返回
$provinces = "select region_name from global_region where region_id = $a" ;
$res = $mysqli->query($provinces);
$rows = $res->fetch_assoc();
//查询市ID对应的市名称,并以数组方式返回
$city = "select region_name from global_region where region_id = $b";
$res1 = $mysqli->query($city);
$rows1 = $res1->fetch_assoc();
//查询区ID对应的区名称,并以数组方式返回
$country = "select region_name from global_region where region_id = $c";
$res2 = $mysqli->query($country);
$rows2 = $res2->fetch_assoc();
//赋值查询结果的省市区名称连接起来。
$pcc = $rows["region_name"].$rows1["region_name"].$rows2["region_name"];
var_dump($pcc);
//查询数据库已存在账户与提交的是否重复,是,则false,返回注册页面,否,则true,往下继续执行。
$acc = "select account from user where account = '".$d."'";
$res3 = $mysqli->query($acc);
$rows3 = $res3->fetch_assoc();
if($rows3){
echo "<script>alert('Sorry,此账户已存在!');window.location.href='region.php';</script>";
exit();
}
//判断提交的账户和密码是否都不为空值.是,则true,将数据写进数据库,否,则false,返回注册页面。
if($d!="" && $f!=""){
$register = "insert into user (account,password,address) values ('".$d."','".$e."','".$pcc."')";
$regres = $mysqli->query($register);
if($regres){
echo "<script>setTimeout(function(){window.location.href='../login/index.php';},3000);</script>";
}
}else{
echo "<script>alert('注册失败!');window.location.href='region.php'</script>";
}
/*
$provinces = "select region_name from global_region where region_id = $a" ;
$res = $mysqli->query($provinces);
if($res->num_rows > 0 ){
$rows = $res->fetch_assoc();
echo $rows["region_name"];
}else{
die("未查找匹配到当前数据".mysqli_error());
}
*/
/*
$provinces = "select region_name from global_region where region_id = $a or region_id = $b or region_id = $c " ;
$res = $mysqli->query($provinces);
echo $res1->num_rows;
if($res->num_rows > 0
$res->num_rows < 4){
echo "选择的地址:";
while($rows = mysqli_fetch_array($res)){
echo($rows["region_name"]);
}
}else{
echo "未查找匹配到当前数据".die(mysqli_error());
}
*/
/*
$provinces1 = "select region_name from global_region where region_id = $a or region_id = $b";
$res1 = $mysqli->query($provinces1);
if($res1->num_rows > 0
$res1->num_rows < 3){
while($rows1 = mysqli_fetch_array($res1)){
echo($rows1["region_name"]);
}
}
*/
?>
账户登录验证页面"checklogin.php"<?php
//header("Content-Type: text/html; charset=utf8");
if(!isset($_POST["login"])){
exit("非法操作!");
}
include("../conn/conn_mysql.php");
$account = trim($_POST["account"]);
$password = md5(trim($_POST["password"]));
var_dump($account);
var_dump($password);
if($account && $password){
/*
$link = mysqli_connect('localhost','root','Aa123123');
if(!$link){
echo "数据库连接失败!</br>";
}else{
echo "数据库连接成功";
}
mysqli_set_charset($link,'utf8');
mysqli_select_db($link,'news');
*/
$sql = "select * from user where account = '$account' and password = '$password'";
$res = mysqli_query($link,$sql);
$result = mysqli_fetch_assoc($res);
$asd = mysqli_num_rows($res);
var_dump($result);
echo "</br>";
echo "</br>";
var_dump($res);
echo "</br>";
echo "</br>";
var_dump($asd);
if($asd){
echo "账户、密码正确!";
echo "<script>window.location.href='index.php';</script>";
}else{
echo "账户或密码错误!";
echo "<script>setTimeout(function(){window.location.href='login.php';},3000);</script>";
}
}else{
echo "账户或密码为空!";
echo "<script>setTimeout(function(){window.location.href='login.php';},3000);</script>";
}
mysqli_close($link);
?>
登录/注册成功后,跳转到的页面"index.php"<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>网站主页</title>
</head>
<body>
<h4>欢迎你,程序员1.</h4>
</body>
</html>
到此,所有数据库设计和代码设计已经展示完毕,接下来,继续查看项目运行效果!!!浏览器地址栏运行http://localhost:8080/news/login/login.php点击登录,返回失败!3秒后,自动返回登录页面,点击“没有账号,前往注册”尝试直接点击注册按钮,返回失败。1秒后,再次返回注册页面,查看数据库,无非法数据注入!尝试仅输入省市区信息,然后点击注册按钮,返回注册失败,数据库查询,非法信息并未写进数据库。输入账户和省市区信息,返回注册失败,数据库亦无非法信息写入!输入密码和省市区信息,返回注册失败,数据库没有非法数据写入同理,输入账户和密码,返回注册失败,数据库没有非法信息写入。最后,依次输入账户,密码和省市区信息,成功注册,数据库录入数据。返回注册页面,修改密码和省市区信息,再次点击注册,返回用户已存在,查询数据库,原有账户注册信息并未改变。最后,所有功能均已演示完毕,省市区联动信息参考PHP中文网作者分享的项目内容,我加以修改和变动。
php普通表单参数提交及获取php提交数据有2种方式:1、get方式,在给form标签添加method="get"属性进行表单提交,提交的数据可通过“$_GET”来获取到;2、post方式,在给form标签添加method="post"属性进行表单提交,提交的数据可通过“$_POST”来获取到。tip:php提供了很多预定义变量,其中包括$_GET、$_POST、$_REQUEST、$_SERVER、$_COOKIE、$_ENV、$_SESSION等。这些预定义变量的数据类型都为数组。post和get的区别:从安全性上来讲,get提交的数据在url栏可以看见,而post提交的数据是不可见的,因此post更安全。从提交原理上讲, get提交是参数一个一个的提交,post提交是所有参数作为一个整体一起提交。从提交数据的大小上来讲, get提交一般不超过255个字节(对url长度的限制),post提交的大小取决于服务器(php.ini配置文件中的post_max_size决定)。从灵活性上讲,get很灵活,只要有页面的跳转就可以传递参数,post不灵活,post提交需要有表单的参与。php页面路径php中不像其他语言那样“/”表示根目录,代之以$_SERVER[‘DOCUMENT_ROOT’],其它则相同:…/表示向上级目录。./表示当前目录。假如现在a/b/c/s.php要调用根目录下的 /bb/s2.txt,则:或者采用相对路径“…/…/…/bb/s2.txt”,表示向上返回到b再向上到a再向上到根目录然后再到bb下找到s2.txt。 1.绝对路径 绝对路径是指目录下的绝对位置,直接到达目标位置。例如:d:/www/index.html。只要页面中的目标文件位置不变你的链接还是指向正确的URL。在Web开发的时候一般很少使用绝对路径,因为你本地的环境和服务器上的路径可能不一样。 2.相对路径 相对路径,就是相对于当前文件的目标文件位置。例如:Web服务器文件夹php下面有index.html和image.jpg两个文件。index.html文件里引入image.jpg,只要这两个文件的相对位置没有变(也就是说还是在文件夹php下面),那么无论上传到Web服务器的哪个位置,这个路径都是正确的。在相对路径里面,./表示同级目录,…/表示上级目录。优点:当你整个项目移动时,你项目内文件之间的相对关系没有改变,之前设置的路径任然是准确的 页面重定向javaScript页面重定向使用javaScript重定向很简单,就是一个函数,在传递请求的时候也可以封装参数进去,就是手动拼接url请求的一个过程。window.location.replace("xxx.php|xxx.html") // 或者 window.location.href = url("xxx.php|xxx.html"); // 这两个函数是等效的,任选其一即可 还是不太清楚的话,可以看看这个例子首先有一段请求 http://localhost:81/login.php当你以get方式或者超链接传递用户名参数的时候,请求会变成这样 http://localhost:81/login.php?userName=小白&password=123就是跳转页面的时候以 ?开头拼接第一个参数,其余参数使用&拼接,每个参数和参数值使用=来说明参数和参数值的对应关系。所以javaScript页面传递参数的请求可以这样写:window.location.replace("xxx.php?参数名1=参数值1&参数名2=参数值2..."); // 或者 window.location.href = url("xxx.php?参数名1=参数值1&参数名2=参数值2..."); // 这两个函数是等效的,任选其一即可 php页面重定向php实现重定向页面跳转的方法:可以利用header()函数来进行重定向。header()函数用于向客户端发送原始的HTTP报头。使用方法如:header('location:xxx.php');。语法格式:header(string,replace,response_code); 函数参数:string 必需。规定要发送的请求头的字符串。replace 可选。指示该请求头是否替换之前的请求头,或添加第二个请求头。默认是 true(替换)。false(允许相同类型的多个请求头)。http_response_code 可选。把 HTTP 响应代码强制为指定的值。小例子-表单参数的封装和页面重定向例子:先创建一个网页的首页,用于提交表单数据的php页面.index.php
账号:
密码:
然后创建一个登录的页面 login.php(假设我们已经存在的用户只有 admin 0000,如果是其他的账号和密码则一律不许登录): 最后在创建一个数据显示页面show.php登录成功"; echo "

账号:$uname

密码:$upwd

"; ?> 暂时就这些啦,文件上传表单,以及session.cookie会话技术,后面的笔记会详细的展开。tip: “学过javaweb的uu看这个一定贼轻松”

我要回帖

更多关于 javascript怎么获取用户输入 的文章

 

随机推荐