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;
}
...

?>

2 Answers 2

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");
        }
    })
Sign up to request clarification or add additional context in comments.

8 Comments

Just a question...I thought jquery tried to do this automatically if you leave it out....doesnt it?
in the docs it does say dataType (default: Intelligent Guess (xml, json, script, or html)) but just to be sure i would specify it
if I include the data and datatype per above, I get "data is not defined" error in firebug
@om123 are you sure you pointed to the correct json data source ?
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.
|
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");
    }
});

2 Comments

Hi, this returns error alert. Firebug says JSON.parse: unexpexted character
@om123 use this one and say what is your php returned value to javascript $numRec = (object)array( 'ajax' => 'Hello world!', 'numRec' => $num_records );

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.