1

I am having difficulty with properly setting up JSON post to get number of rows from MYSQL database from a php file. I am getting "undefined" alert, when I am looking to alert the number of rows as an integer. I have been using a different stackoverflow post to try to build this Get variable from PHP file using JQuery/AJAX.

Here is the ajax call:

// check number of records in Mine
$.ajax({
    url: 'pyrAddToMine.php',
    type: 'POST',
    success : function (result) {
    alert(result['ajax']); // "Hello world!" alerted
    console.log(result['numRec']) // The value of your php $row['numRec'] will be displayed
    },
    error : function () {
        alert("error");
    }
});

Here is the php code in pyrAddToMine.php: mysql_select_db($database_cms_test, $cms_test); $query = "SELECT * FROM $favUserTableName"; $result = mysql_query($query) or die(); $row = mysql_fetch_array($result); $num_records = mysql_numrows($result);

IF ($num_records >= 15){
    $numRec = array(
        'ajax' => 'Hello world!',
        'numRec' => $num_records,
            );
    echo json_encode($numRec);
    exit;
}

Here are more details on the php file:

<?php
require_once('../Connections/cms_test2.php');

...

mysql_select_db($database_cms_test, $cms_test);
$query = "SELECT * FROM `$favUserTableName`";
$result = mysql_query($query) or die();
$row = mysql_fetch_array($result);
$num_records = mysql_numrows($result);

IF ($num_records >= 15){

    $numRec = array(
        'ajax' => 'Hello world!',
        'numRec' => $num_records,
    );
    echo json_encode($numRec);

    exit;
}
...

?>
Community
  • 1
  • 1
om123
  • 21
  • 10

2 Answers2

1

Hello you forgot to specify the datatype

   $.ajax({
        url : 'myAjaxFile.php',
        type : 'POST',
        data : data,
        dataType : 'json',
        success : function (result) {
           alert(result['ajax']); // "Hello world!" alerted
           console.log(result['advert']) // The value of your php $row['adverts'] will be displayed
        },
        error : function () {
           alert("error");
        }
    })
Mihai Vilcu
  • 1,947
  • 18
  • 24
  • Just a question...I thought jquery tried to do this automatically if you leave it out....doesnt it? – Kylie Jun 08 '13 at 04:32
  • in the docs it does say `dataType (default: Intelligent Guess (xml, json, script, or html))` but just to be sure i would specify it – Mihai Vilcu Jun 08 '13 at 04:38
  • if I include the data and datatype per above, I get "data is not defined" error in firebug – om123 Jun 08 '13 at 14:17
  • @om123 are you sure you pointed to the correct json data source ? – Mihai Vilcu Jun 08 '13 at 19:36
  • Hello @ionutvmi, I think you may be onto something since when I extracted the above code into standalone test files, Hello World worked. But when it is in the main code not working. The URL pyrAddToMine.php is definitely correct. If you have suggestions please let me know. – om123 Jun 08 '13 at 21:29
  • Also, the problem seems to be in the php file since Hello World also works within the full js code if I reference the simple test php file. Perhaps something going on with mysql database queries. – om123 Jun 08 '13 at 21:43
  • yes you have to make sure that the php file generates the correct json code or else it won't be able to read it. remember to choose the answer if it was helpful – Mihai Vilcu Jun 09 '13 at 06:35
  • Hi your answer is useful but it seems I need 15 reputation to mark as such ... any suggestions on that front? – om123 Jun 10 '13 at 17:22
1

if you do not set required options for accepting json, you recieve an string that contain ajax format and you did not recieve an object in javascript

$.ajax({
    url: 'pyrAddToMine.php',
    type: 'POST',
    /* required for accept json for ajax */
    accepts:'application/json',
    dataType:'json',
    /* */
    success : function (result) {
    alert(result['ajax']); // "Hello world!" alerted
    console.log(result['numRec']) // The value of your php $row['numRec'] will be displayed
    },
    error : function () {
        alert("error");
    }
});
iman
  • 6,062
  • 1
  • 19
  • 23
  • Hi, this returns error alert. Firebug says _JSON.parse: unexpexted character_ – om123 Jun 08 '13 at 14:29
  • @om123 use this one and say what is your php returned value to javascript `$numRec = (object)array( 'ajax' => 'Hello world!', 'numRec' => $num_records );` – iman Jun 09 '13 at 10:44