简体   繁体   中英

Getting json response for android from PHP

I am newbie in php, kindly pardon me if I looks silly. I am still learning step by step with the help of online forums and stack community.

I have a form online which code is as below:

<?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>

This form works perfectly.

But my issue is I am creating an android form for this online version where I need a json response. So I tried the following in the browser:

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

This results in the same page.

I am confused why this is not entering the page. I can understand that somewhere I am wrong but can't figure it out.

Kindly help please.

As @Akhil @Vijay and @MiguelJimenez suggested

I changed $_POST to $_REQUEST and it worked like a charm

So, if any one getting stuck in this issue this might help you guys hopefully.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM