0

I need help. I am getting problem in returning value from Codeigniter. Whenever, I use exit; after echo it work fine but whenever i try return true it's dosen't work.

Same as i have comment code in PHP code. if i use exit after echo it works but if i don't do that it returns nothing

Ajax Request

$('#social-form').on('submit', function(e){

    e.preventDefault();
    var str = $( "#social-form" ).serialize();
    if (str === '') {
        swal("Please Fill All Fields");
    } else {
        $.ajax({
            type: "POST",
            url: baseUrl + "/admin/social/",
            data: str
        })
        .done(function (data) {
                console.log(data);
                swal("Information", data, "info");
            })
        .error(function () {
            swal("Oops", "We couldn't connect to the server!", "error");
        });
    }
});

Codeigniter-3

public function social(){
    $name = $this->input->post('name');
    $profile = $this->input->post('profile');
    $this->form_validation->set_rules('name', 'name', 'required|trim');
    $this->form_validation->set_rules('profile', 'profile', 'required|trim');
    if ($this->input->post() && $this->form_validation->run() != FALSE) {
        $this->load->model('Social_model','social');
        $this->social->update($name,$profile);
        echo 1;
        //exit;
        //return true;
    }
    else
    {
        echo 0;
        //exit;
        //return false;
    }
}
10
  • Doesn't work HOW? Commented Aug 31, 2016 at 13:22
  • I have tried with retuen but it won't works. thanks for comment Commented Aug 31, 2016 at 13:23
  • 1
    What is it supposed to do that it isn't doing? Please edit your question to add some more explanation of the problem. Commented Aug 31, 2016 at 13:24
  • 1
    I think you are experiencing this stackoverflow.com/questions/10107144/… Commented Aug 31, 2016 at 13:35
  • 1
    Since your method ending with if than else block nothing wrong to leave just echo 1 and echo 0 in their locations. There is no more code to be executed at the end of social(). Commented Aug 31, 2016 at 14:47

2 Answers 2

1

CodeIgniter has a layout, so after outputting a response there could be views that are outputted after your response, such as a footer or a debug bar.

Try using your console to see the status code of the response. Also note that it isn't bad practice in CodeIgniter to exit after AJAX calls, so perhaps you should just write a AJAX response helper which does all that for you (like setting the header and adding the exit).

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your advice. i'll go with your ans but let me get some more examples if there something new you also will get awesome answer.
I would also recommend you set your response type to JSON in JavaScript and return a JSON result from PHP. Like echo json_encode(array('result',1)); (and of course the exit() ;) ). This way you get a complete response object in JavaScript.
1

You probably need to be more specific about what you echo. This is one of several possible solutions.

controller

 public function social(){
    $name = $this->input->post('name');
    $profile = $this->input->post('profile');
    $this->form_validation->set_rules('name', 'name', 'required|trim');
    $this->form_validation->set_rules('profile', 'profile', 'required|trim');
    if ($name && $this->form_validation->run() != FALSE) {
        $this->load->model('Social_model','social');
        $this->social->update($name,$profile);
        $out = json_encode(array('result' => 'success'));
    }
    else
    {
        $out = json_encode(array('result' => 'failed'));
    }
        echo $out;
}

javascript

$('#social-form').on('submit', function (e) {
    e.preventDefault();
    var str = $("#social-form").serialize();
    if (str === '') {
        swal("Please Fill All Fields");
    } else {
        $.ajax({
            type: "POST",
            url: baseUrl + "/admin/social/",
            data: str,
            dataType: 'json'
        })
          .done(function (data) {
              console.log(data);
              if (data.result === 'success') {
                  swal("Information", "Success", "info");
              } else {
                  swal("Information", "Failed", "info");
              }
          })
          .error(function () {
              swal("Oops", "We couldn't connect to the server!", "error");
          });
    }
});

Comments

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.