如果要跳转页面,那么form上要定义action跳转到相关页面,同时提交按钮的type要为submit,如下:
不跳转用ajax刷新的代码如下:
html代码:
用户注册 | |
用户名: | |
密码: | |
JS代码:
function f1()
{
//创建xmlHttp对象
var xmlHttp;
if(window.ActiveXObject)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest)
{
xmlHttp=new XMLHttpRequest();
}
//获取表单值
var username=document.form1.username.value;
var password=document.form1.password.value;
var datastr="username="+username+"&password="+password;
var url="/test2.php";
//提交数据
xmlHttp.onreadystatechange=function(){
if(xmlHttp.readyState==4&&xmlHttp.status==200){
document.getElementById("d1").innerHTML=xmlHttp.responseText;
}
}
xmlHttp.open("POST",url,true);
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlHttp.send(datastr);
}
PHP代码:
<?php $conn = mysql_connect('localhost','root','');mysql_query("set names utf-8");mysql_select_db( "test" ); function inject_check($sql_str){ return preg_match("/select|insert|update|delete|\'|\/\*|\*|\.\.\/|\.\/|union|into|load_file|outfile|%|eval|=|and|'|;|exec|count/i", $sql_str); // 进行过滤} if(!empty($_POST)){ foreach($_POST as $key => $value){ if(inject_check($value)){ exit (''); die (); } }}//这里的变量名改成相应的变量名 $res = mysql_query("SELECT count(*) as m from `user` where username='${_POST['username']}' AND password='${_POST['password']}'"); $row = mysql_fetch_object($res);if($row->m >0){ echo "登陆成功";}else{ echo "登陆失败";}exit;?>