json object in php not being read json object in php not being read json json

json object in php not being read


Echo and die the statement in order for ajax to have a success event

Js File

 var myobj = {};        myobj["usrname"] = 'myUsername';        myobj["usrpass"] = 'myPassword';        $.ajax({            type: "post",            url: "url",            dataType: "json",            data: {post_data: myobj},            contentType: "application/x-www-form-urlencoded",            success: function (responseData) {                console.log(responseData);            },            error: function (errorThrown) {                console.log(errorThrown);            }        });

PHP action File

           /** if we print post we will get the following array * *///print_r($_Post);//die()//Array//(//    [post_data] => Array//        (//            [usrname] => myUsername//            [usrpass] => myPassword//        )////)if (isset($_Post['post_data'])) {    $myPost = $_Post['post_data'];    $usrname = $myPost['usrname'];    $usrpass = $myPost['usrpass'];    $sql = "select count(*) from glusers where EmpName='$usrname' and EmpPass='$usrpass'";    $result = $conn->query($sql);    $num_row = $result->num_rows;    if ($num_row > 0) {        $output = 'Success';    } else {        $output = 'fail';    }    echo json_encode($output);    die();}


Try This : in js file :

$(document).on("ready", function(){            // Create an object using an object literal.            var ourObj = {};            // Create a string member called "data" and give it a string.            // Also create an array of simple object literals for our object.            ourObj.data = "Some Data Points";            ourObj.arPoints = [{'x':1, 'y': 2},{'x': 2.3, 'y': 3.3},{'x': -1, 'y': -4}];            var savedata = JSON.stringify(ourObj)            $.ajax({                type:"POST",                url:"Users.php",                data: {"points" : JSON.stringify(ourObj)},               success: function(data) {                    // Do something with data that came back.                     alert(data);               }            })        }); 

In PHP File :

if (isset($_POST["points"])) {$points = json_decode($_POST["points"]);echo "Data is: " . $points->data . "<br>";echo "Point 1: " . $points->arPoints[0]->x . ", " . $points->arPoints[0]->y;}


Try this:

var myobj = '{usrname:'+$( "#customer option:selected" ).text()+',usrpass:'+$("#psw").val()+'}';

or

var myobj = {};myobj.usrname= $( "#customer option:selected" ).text();myobj.usrpass= $("#psw").val();