从PHP获取android的json响应

时间:2016-03-31 07:28:21

标签: php android json

我是php的新手,如果我看起来很傻,请原谅我。我仍然在网上论坛和堆栈社区的帮助下一步一步地学习。

我在网上有一个表格,代码如下:

<?php
define('DIR_APPLICATION', str_replace('\'', '/', realpath(dirname(__FILE__))) . '/');
include(DIR_APPLICATION."config.php");
ob_start();
session_start();
$msg = 'none';
$sql = '';
if(isset($_POST['email']) && $_POST['email'] != '' && isset($_POST['password']) && $_POST['password'] != ''){
  if($_POST['usertype'] == 'admin'){
    //here for admin
    $sql= mysql_query("SELECT * FROM admin WHERE a_email = '".make_safe($_POST['email'])."' and password = '".make_safe($_POST['password'])."'",$link);
  }
  else if($_POST['usertype'] == 'teacher'){
    //here for teacher
    $sql= mysql_query("SELECT * FROM teacher WHERE t_email = '".make_safe($_POST['email'])."' and t_password = '".make_safe($_POST['password'])."'",$link);
  }
  else if($_POST['usertype'] == 'student'){
    //here for student
    $sql= mysql_query("SELECT * FROM student WHERE s_email = '".make_safe($_POST['email'])."' and s_password = '".make_safe($_POST['password'])."'",$link);
  }
  else if($_POST['usertype'] == 'parents'){
    //here for parent
    $sql= mysql_query("SELECT * FROM parent WHERE p_email = '".make_safe($_POST['email'])."' and p_password = '".make_safe($_POST['password'])."'",$link);
  }
  else if($_POST['usertype'] == 'librarian'){
    //here for employee
    $sql= mysql_query("SELECT * FROM user WHERE u_email = '".make_safe($_POST['email'])."' and u_password = '".make_safe($_POST['password'])."'",$link);
  }
  else if($_POST['usertype'] == 'accountant'){
    //here for employee
    $sql= mysql_query("SELECT * FROM user WHERE u_email = '".make_safe($_POST['email'])."' and u_password = '".make_safe($_POST['password'])."'",$link);
  }
  if($row = mysql_fetch_array($sql)){
    //here success
    $_SESSION['objLogin'] = $row;
    $_SESSION['login_type'] = $_POST['usertype'];
    if($_POST['usertype'] == 'admin'){
      header("Location: dashboard.php");
      die();
    }
    else if($_POST['usertype'] == 'teacher'){
      header("Location: t_dashboard.php");
      die();
    }
    else if($_POST['usertype'] == 'student'){
      header("Location: s_dashboard.php");
      die();
    }
    else if($_POST['usertype'] == 'parents'){
      header("Location: p_dashboard.php");
      die();
    }
    else if($_POST['usertype'] == 'accountant'){
      header("Location: a_dashboard.php");
      die();
    }
    else if($_POST['usertype'] == 'librarian'){
      header("Location: l_dashboard.php");
      die();
    }
  }
  else{
    $msg = 'block';
  }
}
function make_safe($variable) 
{
   $variable = strip_tags(mysql_real_escape_string(trim($variable)));
   return $variable; 
}
?>
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>SITE TITLE | Log in</title>
  <!-- Tell the browser to be responsive to screen width -->
  <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
  <!-- Bootstrap 3.3.5 -->
  <link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
  <!-- Font Awesome -->
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">
  <!-- Ionicons -->
  <link rel="stylesheet" href="https://code.ionicframework.com/ionicons/2.0.1/css/ionicons.min.css">
  <!-- Theme style -->
  <link rel="stylesheet" href="dist/css/AdminLTE.min.css">
  <!-- iCheck -->
  <link rel="stylesheet" href="plugins/iCheck/square/blue.css">

  <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
  <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
  <!--[if lt IE 9]>
  <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
  <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
  <![endif]-->
</head>
<body class="hold-transition login-page">
<div class="login-box">
  <div class="login-logo">
    <a href="index2.html"><b>SITE</b> TITLE</a>
  </div>
  <!-- /.login-logo -->
  <div class="login-box-body">
    <p class="login-box-msg">Sign in to start your session</p>

    <form onSubmit="return validationForm();" role="form" id="form" method="post">
      <div class="form-group has-feedback">
        <input type="email" name="email" id="email" class="form-control" placeholder="Email">
        <span class="glyphicon glyphicon-envelope form-control-feedback"></span>
      </div>
      <div class="form-group has-feedback">
        <input type="password" name="password" id="password" class="form-control" placeholder="Password">
        <span class="glyphicon glyphicon-lock form-control-feedback"></span>
      </div>
      <div class="form-group has-feedback">

                  <select class="form-control" name="usertype" id="usertype">
                <option value="-1">-- Select User Type --</option>
                <option value="admin">Admin</option>
                <option value="teacher">Teacher</option>
                <option value="student">Student</option>
                <option value="parents">Parents</option>
                <option value="accountant">Accountant</option>
                <option value="librarian">Librarian</option>
                  </select>
                </div>
      <div class="row">
        <div class="col-xs-8">
         <a href="#">Forget Password?</a><br> 
        </div>
        <!-- /.col -->
        <div class="col-xs-4">
          <button type="submit" id="login" class="btn btn-primary btn-block btn-flat">Sign In</button>
        </div>
        <!-- /.col -->
      </div>
    </form>

  </div>
  <!-- /.login-box-body -->
</div>
<!-- /.login-box -->

<!-- jQuery 2.2.0 -->
<script src="plugins/jQuery/jQuery-2.2.0.min.js"></script>
<!-- Bootstrap 3.3.5 -->
<script src="bootstrap/js/bootstrap.min.js"></script>
<!-- iCheck -->
<script src="plugins/iCheck/icheck.min.js"></script>
<script>
  $(function () {
    $('input').iCheck({
      checkboxClass: 'icheckbox_square-blue',
      radioClass: 'iradio_square-blue',
      increaseArea: '20%' // optional
    });
  });
</script>
<script type="text/javascript">
function validationForm(){
  if($("#email").val() == ''){
    alert("Email Required !!!");
    $("#email").focus();
    return false;
  }
  else if($("#password").val() == ''){
    alert("Password Required !!!");
    $("#password").focus();
    return false;
  }
  else if($("#usertype").val() == '-1'){
    alert("Select User Type !!!");
    return false;
  }
  else{
    return true;
  }
}
</script>
<!-- SCRIPTS -AT THE BOTOM TO REDUCE THE LOAD TIME-->
<!-- JQUERY SCRIPTS -->
<script src="assets/js/jquery-1.10.2.js"></script>
<!-- BOOTSTRAP SCRIPTS -->
<script src="assets/js/bootstrap.min.js"></script>
</body>
</html>

此表单效果很好。

但我的问题是我正在为这个在线版本创建一个Android表单,我需要一个json响应。所以我在浏览器中尝试了以下内容:

mydomain.com/index.php?email=user@example.com&password=mypass&usertype=student

这会产生相同的页面。

我很困惑,为什么这不进入页面。 我可以理解,在某个地方,我错了,但无法理解。

请帮忙。

1 个答案:

答案 0 :(得分:0)

@Akhil @Vijay和@MiguelJimenez建议

我将$_POST更改为$_REQUEST,它就像一个魅力

所以,如果有人在这个问题上遇到困难,这可能会对你有所帮助。