Search code examples
phpmysqlajaxresponse

I couldn't find my error; empty returns from Ajax request


Here is my source; lol

 $(document).on('click', '#findFirstUndefined', function (event) {
       event.preventDefault();
       $.ajax({
           type: 'post',
           url: './custom/tool.node.php',
           dataType: 'text',
           data: {
               m: 't',
               f: 'fu'
           },
           beforeSend: function () {
               console.log('Request find first undefined node..');
           },
           success: function (data) {
               console.log('Request success.');
               console.log(data);
           },
           error: function () {
               console.log('Request failure.');
           }
       });
   });
    // Following is tool.node.php
   if ($_POST['m'] === 'n') {
       if (createNode($db)) echo $db - > insert_id;
       else echo 'false';
   }
    // ..
    else if ($_POST['m'] === 't') {
       if ($_POST['t'] === 'fu') {
           // Find first undefined node..
           $sql = "select `id` from `v3_node` where `type`=0 limit 0,1;";
           $rst = $db - > query($sql);
           // Initialize array for json string
           $return = array('rstExist' = > null);
           if ($rst - > num_rows === '0') {
               // Undefined Node NOT exist.
               $return['rstExist'] = false;
           } else {
               // Undefined Node exist.
               $return['rstExist'] = true;
               // Find 20 items around first undefined node.. 
               $row = $rst - > fetch_assoc();
               $sql = "select * from `v3_node` where `id`>=".($row['id'] - fmod($row['id'], 20) + 1).
               " limit 0, 20;";
               $rst = $db - > query($sql);
               $return['node'] = array();
               while ($row = $rst - > fetch_assoc()) {
                   array_push($return['node'], $row);
               };
           };
           echo json_encode($return);
       };
   };

All I wanted was getting rows through ajax and response is empty .. nothings in it. I can't even find my error so need help!

and also if I change a dataType attribute to 'json', console prints Request Failure which is error callback; why is that???


Solution

  • Following this line data: { m: 't', f: 'fu' },. It will do the else statement because m == 't'

    else if($_POST['m'] === 't')
    

    In this else statement it needs to be t == 'fu', but this isn't true because $_POST['t'] doensn't exists. I think this need to be f instead of t. So change this line:

    if($_POST['t'] === 'fu') {
    

    To

    if($_POST['f'] === 'fu') {