0

I am calling a php function from javascript by passing three arguments, on the other side php function inside the php file gets two values from database and prints all the value. for that I have written this code but this is not working, means this code prints nothing in the output so kindly help.

javascript code

jQuery.ajax(
{
    type: "POST",
    url: 'save.php',
    dataType: 'json',
    data: {functionname:'saveUser', arguments:["className", "student_id", "isPresent"]},

    success: function (obj, textstatus) {
        if( !('error' in obj) ) {
            alert(obj.result);
        }
        else {
            console.log(obj.error);
        }
    }
});

php code

<?php

    header('Content-Type: application/json');   
    if( $_POST['functionname'] == 'saveUser' ) {
        include_once("dbConnection.inc");
        $db = db_connect();
        $newSql = "SELECT class_id, date FROM class_session WHERE class_id = (select max(class_id) from class_session)";
        $result = mysql_query($newSql, $db);
        $row = mysql_fetch_array($result);
        $class_id = $row["calass_id"];
        $date = $row["date"];
        echo json_encode(Array(
            'result' => $_POST['arguments'][0] .' '. $_POST['arguments'][1] .' '. $_POST['arguments'][2] .' '. $class_id .' '. $date
        ));
    }
?>
devpro
  • 16,184
  • 3
  • 27
  • 38
Ali Salman
  • 59
  • 1
  • 1
  • 6
  • Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Dec 28 '15 at 11:57
  • array should be lowercase. – jcubic Dec 28 '15 at 12:16
  • @ali-salman: i have tested this, it returns "className student_id isPresent 123 2015-12-10" in alert box for me... now please share the code of db_connect() .... we are close to solve this – devpro Dec 28 '15 at 12:49
  • but i hard coded these two values in php..like that.... $class_id = 123; $date = '2015-12-10'; ........ because i am not using this table. – devpro Dec 28 '15 at 12:50

1 Answers1

0

The right property is method not type, see jQuery.ajax()

jQuery.ajax({
    method: "POST",
    url: 'save.php',
    dataType: 'json',
    data: {functionname:'saveUser', arguments:["className", "student_id", "isPresent"]},

    success: function (obj, textstatus) {
        if( !('error' in obj) ) {
            alert(obj.result);
        }
        else {
            console.log(obj.error);
        }
    }
});
jcubic
  • 61,973
  • 54
  • 229
  • 402
  • @AliSalman do you have error from php, try adding: `ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);` at the begining. – jcubic Dec 28 '15 at 11:56
  • I dont know where is the problem, but I am getting nothing as a output – Ali Salman Dec 28 '15 at 12:02
  • @AliSalman: issue must be in php, your ajax is fine, and php also, if you disable your query and connection lines, and hard code $class_id = 123; $date = '2015-12-10'; .... you will get the result.... – devpro Dec 28 '15 at 13:00