ajax 怎么判断 php传输过来值
<scripttype="text/javascript">functionuserLogin(){$("#user_msg").hide();$("#pass_msg"...
<script type="text/javascript">function userLogin(){ $("#user_msg").hide(); $("#pass_msg").hide(); var username = $("#username").val(); if(username == ""){ $("#user_msg").html("用户名不能为空!"); $("#user_msg").show(); return false; } var password = $("#password").val(); if(password == ""){ $("#pass_msg").html("密码不能为空!"); $("#pass_msg").show(); return false; } $.ajax({ type: "POST", url: "/php/login.php", data:{username:username,password:password}, success: function (data){ $("#msg").html(data) if(data= "good"){ window.location.href = "index.html"; } else {window.location.href = "1.html"; } } });}</script>login.php<?php$conn = mysqli_connect('127.0.0.1:3306','root','','db_weishi');//if (!$conn) die("失败:".mysqli_connect_error());//else echo "成功了";$username = $_POST['username'];$password = $_POST['password'];//echo "<br>",$username." - ".$password;echo "<br><br>";if ($username && $password){ $sql = "SELECT * FROM mod_users WHERE username='$username' and password='$password'"; $res = mysqli_query($conn, $sql); $rows = mysqli_num_rows($res); if($rows){ echo "good"; } else { echo "bad"; } mysqli_free_result($res); mysqli_close($conn);}?>想要的效果是php运行完返回“good”或者“bad”,在ajax里进行判断。一直找不到方法做。
展开
1个回答
展开全部
if ($_REQUEST['act'] == 'c**ignee_list')
{
/*
* 检查用户是否选择匿名购物
*/
if (isset($_REQUEST['direct_shopping']))
{
$_SESSION['direct_shopping'] = 1;
}
/*
* 检查用户是否已经登录
* 如果用户已经登录了则检查是否有默认的收货地址
* 如果没有登录则跳转到登录和注册页面
*/
if (empty($_SESSION['direct_shopping']) && $_SESSION['user_id'] == 0)
{
/* 用户没有登录且没有选定匿名购物,转向到登录页面 */
$result['error'] = 1;
$result['message'] = '对不起,您没有登录或者您未选择匿名购物!';
echo $json->encode($result);
exit;
}
}
echo $json->encode($result);
$('#addr_modify').click(function(){
$.ajax({
type: "POST",
url: "flow_ajax.php?act=c**ignee_list",
cache: false,
data: "&m=" + Math.random(),
beforeSend:function(){$('#addr_modify').html('[获取中...]')},
success:function(data){
re = $.evalJSON(data);
alert(re.message);
if(re.error == 1)
{
alert(re.message);
window.location.href="flow.php?step=cart" + "&m=" + Math.random();
return;
}
else if(re.error == 2)
{
alert(re.message);
window.location.href="flow.php?step=c**ignee";
return;
}
$('#addr_modify').hide();
$('#addr_okbox').hide();
$('#addr_editbox').show();
// $('#addr_editbox').html(re.content_list);
// $('#addr_edit_table').html(re.content_table);
}
})
});
flow_ajax.php
define('IN_ECS', true);
require(dirname(__FILE__) . '/includes/init.php');
require(ROOT_PATH . 'includes/cls_json.php');
if (!isset($_REQUEST['act']))
{
/* 只有在没有提交评论内容以及没有act的情况下才跳转 */
ecs_header("Location: flow.php\n");
exit;
}
$json = new JSON;
$result = array('error' => 0, 'message' => '', 'content' => '');
$result['message'] = '已接受请继续';
echo $json->encode($result);
{
/*
* 检查用户是否选择匿名购物
*/
if (isset($_REQUEST['direct_shopping']))
{
$_SESSION['direct_shopping'] = 1;
}
/*
* 检查用户是否已经登录
* 如果用户已经登录了则检查是否有默认的收货地址
* 如果没有登录则跳转到登录和注册页面
*/
if (empty($_SESSION['direct_shopping']) && $_SESSION['user_id'] == 0)
{
/* 用户没有登录且没有选定匿名购物,转向到登录页面 */
$result['error'] = 1;
$result['message'] = '对不起,您没有登录或者您未选择匿名购物!';
echo $json->encode($result);
exit;
}
}
echo $json->encode($result);
$('#addr_modify').click(function(){
$.ajax({
type: "POST",
url: "flow_ajax.php?act=c**ignee_list",
cache: false,
data: "&m=" + Math.random(),
beforeSend:function(){$('#addr_modify').html('[获取中...]')},
success:function(data){
re = $.evalJSON(data);
alert(re.message);
if(re.error == 1)
{
alert(re.message);
window.location.href="flow.php?step=cart" + "&m=" + Math.random();
return;
}
else if(re.error == 2)
{
alert(re.message);
window.location.href="flow.php?step=c**ignee";
return;
}
$('#addr_modify').hide();
$('#addr_okbox').hide();
$('#addr_editbox').show();
// $('#addr_editbox').html(re.content_list);
// $('#addr_edit_table').html(re.content_table);
}
})
});
flow_ajax.php
define('IN_ECS', true);
require(dirname(__FILE__) . '/includes/init.php');
require(ROOT_PATH . 'includes/cls_json.php');
if (!isset($_REQUEST['act']))
{
/* 只有在没有提交评论内容以及没有act的情况下才跳转 */
ecs_header("Location: flow.php\n");
exit;
}
$json = new JSON;
$result = array('error' => 0, 'message' => '', 'content' => '');
$result['message'] = '已接受请继续';
echo $json->encode($result);
追问
虽然是相近的例子,但是能不能点出几个核心的函数 我试着模仿改了 可是依旧没有效果
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询