0

my php code is:

$query = "SELECT * FROM users WHERE user='admin' AND password='MTIz'";
$result = $link->query($query);
$yes = array();
$yes[] = $result->num_rows;
echo json_encode($yes);

And my HTML code is:

  $.ajax({
    url: 'vlogin.php',
    type: 'POST',
    data: myData,
    dataType: 'json',
    contentType: "application/json; charset=utf-8",
    success: function(yes) {
    alert(yes.Result);}
});

Not return anything. What appens? Thanks

7
  • 1
    what does alert show?can you try removing contentType Commented Jan 21, 2016 at 10:37
  • 2
    Try $yes['Result'] = $result->num_rows; Commented Jan 21, 2016 at 10:38
  • 2
    do a console.log(yes) to see exactly what's inside your response Commented Jan 21, 2016 at 10:40
  • Can you post your JSON code? Make sure it's valid. Commented Jan 21, 2016 at 10:40
  • What kind of Object is $link ? What is $result->num_rows supposed to contain? Is it an object? Is it an integer? Where do you define the Result index? When writing $yes = array(); $yes[] = $result->num_rows; you won't have any $yes['Result'] but a $yes[0]['Result'] Commented Jan 21, 2016 at 10:42

3 Answers 3

1

When using $.ajax(), I usually implement the error callback :

From the jquery doc : error (Type: Function( jqXHR jqXHR, String textStatus, String errorThrown ))

Implementing it allows you to see if the call is generating (bad JSON) or receiving (webservice error) errors : you can log/alert textStatus and errorThrown.

$.ajax({
      url: 'vlogin.php',
      type: 'POST',
      data: myData,
      dataType: 'json',
      contentType: "application/json; charset=utf-8",
      success: function(yes) {
          console.log(yes.Result);
      },
      error: function(jqXHR, textStatus, errorThrown) {
          console.log(textStatus);
          console.log(errorThrown);
      }
});
Sign up to request clarification or add additional context in comments.

Comments

0
$query = "SELECT * FROM users WHERE user='admin' AND       password='MTIz'";
$result = $link->query($query);
$yes = array();
$yes[] = $result->num_rows;
echo json_encode($yes);
die;

apply die after the json_encode($yes); it will give you result then

and alert only yes like this alert(yes);

3 Comments

Could you also please explain how does that die; help?
Because some times more code is ran before being sent to the next page.
sometimes without die; the function after encoding direct to any other controller. i faced the same problem when i applied die;, the issue was resolved. not sure it will work for you guyz or not! but it worked for me.
0

Try Below

 $query = "SELECT * FROM users WHERE user='admin' AND password='MTIz'";
 $result = $link->query($query);
 $yes = array();
 $yes['record'] = $result->num_rows;
 echo json_encode($yes);

And .ajax

  $.ajax({
          url: 'vlogin.php',
          type: 'POST',
          data: myData,
          dataType: 'json',
          contentType: "application/json; charset=utf-8",
          success: function(yes) {
          console.log(yes.record);
    }
 });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.