Gambit
Gambit

Reputation: 13

Json not posting values to PHP

i'm trying out a simple login form through phonegap, the code is herewith. my problem is that JSON is not passing the values from my phonegap page to the PHP service. Any help is appreciated Here is my script:

 $('form').submit(function(){
    //var postData = $(this).serialize();
    var username = document.getElementById("username").value;
    var password = document.getElementById("password").value;
    $.ajax({
        type: 'POST',
        data:JSON.stringify({username:"username",password:"password"}),
        ContentType: "application/json; charset=utf-8",
        crossDomain: true,
        dataType: 'json',
        url: 'http://10.0.2.2:81/comment.php',
        success: function(response){
             alert ("response"); 
                        if (response) { 
                            alert("you're logged in");
                            }
                            else {

                            alert("Your login failed");

                        }

        },
        error: function(){

            alert('There was an error with your login');
        }
    });

    return True;
});

My PHP page is as follows

    <?php 
    header('content-type: application/json');
    header("access-control-allow-origin: *");

    $server = "localhost";
    $username = "root";
    $password = "";
    $database = "comment";

    $con = mysql_connect($server, $username, $password) or die ("Could not connect: " . mysql_error());

    mysql_select_db($database, $con);

     $username=$_POST["username"]; 
     $password=$_POST["password"]; 
     $sql="SELECT username, password FROM comment WHERE username = '".$username."' AND password = '".$password."'"; 
     $result = mysql_query($sql); 
     if (mysql_num_rows($result) < 1)
     { $response =true; }
    else
    { $response =false; 
    }

    mysql_close($con); 
    echo json_encode($response);
     ?> 

Upvotes: 1

Views: 121

Answers (2)

Sabbir Ahmed Sourove
Sabbir Ahmed Sourove

Reputation: 515

you can not get that data straight forward using $_POST varriable.to accept json you need to read from stdin input

<?php 
header('content-type: application/json');
header("access-control-allow-origin: *");

$server = "localhost";
$username = "root";
$password = "";
$database = "comment";

$con = mysql_connect($server, $username, $password) or die ("Could not connect: " . mysql_error());

mysql_select_db($database, $con);
 ////here you need to change
 $request = file_get_contents('php://input');
 $reqRarray = json_decode($request,true);
//////`enter code here`
 $username=$reqRarray["username"]; 
 $password=$reqRarray["password"]; 
 $sql="SELECT username, password FROM comment WHERE username = '".$username."' AND password = '".$password."'"; 
 $result = mysql_query($sql); 
 if (mysql_num_rows($result) < 1)
 { $response =true; }
else
{ $response =false; 
}

mysql_close($con); 
echo json_encode($response);

?>

I have tested with following javascript code:

<html>
<head>
    <script src="http://code.jquery.com/jquery-2.1.3.min.js"></script>
        <script  type='text/javascript'>
         $(document).ready(function(){
            //var postData = $(this).serialize();
            var username = 'test';
            var password = 'meu';
            $.ajax({
                type: 'POST',
                data:JSON.stringify({username:"username",password:"password"}),
                ContentType: "application/json; charset=utf-8",
                crossDomain: true,
                dataType: 'json',
                url: 'ajax.php',
                success: function(response){
                     alert ("response"); 
                                if (response) { 
                                    alert("you're logged in");
                                    }
                                    else {

                                    alert("Your login failed");

                                }
                },
                error: function(){

                    alert('There was an error with your login');
                }
            });

            return true;
         });
        </script>
</head>
<body></body>

and with folling php code:

<?php
$request = file_get_contents('php://input');
$reqRarray = json_decode($request,true);
var_dump($reqRarray);
?>

and get that output on ajax:

array (size=2)  'username' => string 'username' (length=8)  'password' => string 'password' (length=8)

Upvotes: 0

Akash
Akash

Reputation: 114

$('form').submit(function(){
    //var postData = $(this).serialize();
    var username = document.getElementById("username").value;
    var password = document.getElementById("password").value;
    $.ajax({
        type: 'POST',
         data : {username:username, password:password},

        crossDomain: true,
        dataType: 'json',
        url: 'http://10.0.2.2:81/comment.php',
        success: function(response){
             alert ("response"); 
                        if (response) { 
                            alert("you're logged in");
                            }
                            else {

                            alert("Your login failed");

                        }

        },
        error: function(){

            alert('There was an error with your login');
        }
    });

    return True;
});

Upvotes: 1

Related Questions