2

The status is fine 200 but then the data is null here is my codes: in my controller

        var link = 'http://127.0.0.1/mobile/subject.php';
        var userid = localStorage.getItem('loginDetails');
        console.log(userid);

        $http.post(link, {userid}).then(function(res){
        console.log(res);
        })

in my php code. require_once('connection.php');

        $post_data = file_get_contents('php://input');
        $request = json_decode($post_data);

        $userid = $request->userid;

        $subject_sql ="SELECT SubjectName FROM subjects WHERE userid = '"  

        .$userid. "' ";

        $result = $con->query($subject_sql);
        $re=$result->fetch_assoc();
        echo json_encode($re);

        ?>

Here guys i put my localstorage value in a variable "userid" because i want to use my localstorage value in my php code. Is their something wrong with my codes specially in my controller?

Ameer Hamza
  • 586
  • 3
  • 16

3 Answers3

0

if your localstorage already contains the JSON object then you just need to change the {userid} to userid.

    var link = 'http://127.0.0.1/mobile/subject.php';
    var userid = localStorage.getItem('loginDetails');
    console.log(userid);

    $http.post(link, userid, {}).success(function(res){
      console.log(res);
    })
    .error(function(err) {
      console.log(err)
    });
Ameer Hamza
  • 586
  • 3
  • 16
  • nothing happens sir still it is null... sir i just want to ask if its correct on how to make my localstorage value a json object by this code. var userid= JSON.parse(localstorage.getitem('loginDetails')); and I used JSON.stringify in storing localstorage value.. – Benjie Pajimola Sep 23 '16 at 16:04
  • 1
    yes it is the correct way.. you are doing it right.. you should use here var userid = JSON.parse(localStorage.getItem('loginDetails')); tell me if it worked? – Ameer Hamza Sep 23 '16 at 16:35
  • great.. please accept the answer if it worked for you.. thanks – Ameer Hamza Sep 26 '16 at 05:38
0

You can't access $request->userid because you sent { userid } as data in $http.post(). Try $http.post(link, {userid : userid}).

Server side, you should :

  • Check if $post_data actually contains anything ( var_dump() ),
  • Check if localStorage.getItem('loginDetails') is defined.

Advice : Do not forget to escape your sql query. (How can I prevent SQL injection in PHP?)

Community
  • 1
  • 1
littleboy
  • 78
  • 1
  • 5
  • Yes i sent it as a data because i want to use that data to identify what subject that data handles in my database. yes i var_dump() my $post_data and the result is string '"the value in my localstorage"' (length=18). i also tried your suggestion for {userid : userid} but still it returns null. tnx for ur time sir :D – Benjie Pajimola Sep 23 '16 at 15:45
  • Try using $_POST (http://php.net/manual/en/reserved.variables.post.php). Check if the headers sent with the request are correct (http://stackoverflow.com/questions/4423061/view-http-headers-in-google-chrome). **I hardly advise you to use a (php) framework to design your server side application logic.** It will handle for you a lot of security issues like : escaping $_POST, protecting SQL queries... You also need a better layer of security to authenticate your users. – littleboy Sep 25 '16 at 10:22
0

Using JSON.parse to convert it to json object it solve my problem. var userid = JSON.parse(localStorage.getItem('loginDetails'));