-2

Help! i am learning php through online tutorial and the best way to learn is to do something, i actually did in the procedural way and it works, now i'm doing the same thing in OOP and MVC(my own implementation of mvc just for learning), it's also going well until i have to use jquery/ajax... Now my problem is the response received in JSON.

Here is the screenshot of the same Ajax call response in procedural which works:

enter image description here

Here is the screenshot of the same Ajax call response in OOP/MVC which doesn't work:

enter image description here

Code of page containing the jQuery:

$("#full-input").on('change', function() {
     var customer = $("#full-input").val();
     $.ajax({
        url: 'getSingle',
        data: {id: customer},
        type: 'post',
        success: function (result) {
             alert(result);//for debuging 
        }
    });
});

Controller to handle the request:

class CustomerController extends Controller{
    public function index(){
        $customer = new CustomerModel();
        $data = $customer->index();
        $this->setData($data);
        $this->setTitle('Customer List');
        $this->setView(VIEW_PATH.'customer/all_cust.php');
    }

    public function getSingle(){
        $sintgle = new CustomerModel();
        if(isset($_POST['id'])){
            $result = $sintgle->singleCust($_POST['id']);
            echo json_encode($result);
            $this->setTitle('Single Customer');
        }else{
             $data = $sintgle->index();//gets all customers to populate drop-down select
             $this->setData($data);
             $this->setTitle('Select Customer');
             $this->setView(VIEW_PATH.'customer/single_cust.php');
        }
    }
 }

What am i not doing right here?

Victor M Perez
  • 2,185
  • 3
  • 19
  • 22
Jay Wright
  • 21
  • 2
  • Have you checked the URL in your AJAX? Make sure it's going to the correct place by seeing what data you get back in your controller's method – snack_overflow May 09 '18 at 11:46
  • I can see from the image that it's hitting your controller because the echo is displaying the data. Maybe you should look into setting the content-type - https://stackoverflow.com/a/4064468/2401021 – majita May 09 '18 at 11:54

1 Answers1

0

Try adding a var_dump after $result,

var_dump($result);

and return false/true after echo statement.