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;
    }
}
Sparky
  • 98,165
  • 25
  • 199
  • 285
Gaurang Joshi
  • 684
  • 16
  • 32
  • Doesn't work HOW? – u_mulder Aug 31 '16 at 13:22
  • I have tried with `retuen` but it won't works. thanks for comment – Gaurang Joshi Aug 31 '16 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. – Don't Panic Aug 31 '16 at 13:24
  • someone flagged it as off-topic. can you describe how it is? – Gaurang Joshi Aug 31 '16 at 13:24
  • _"Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself."_ You haven't included the desired behavior, and "doesn't work" is not a specific problem or error. – Don't Panic Aug 31 '16 at 13:27
  • i am not asking you "why isn't this code working?" i am looking for there is any mistakes or any problem i have made in this code? whatever!! contributor always helps if they have an idea but person like you don't. after all thanks for looking in to my code. – Gaurang Joshi Aug 31 '16 at 13:31
  • 1
    I think you are experiencing this http://stackoverflow.com/questions/10107144/difference-between-php-echo-and-return-in-terms-of-a-jquery-ajax-call – nerdlyist Aug 31 '16 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()`. – Tpojka Aug 31 '16 at 14:47
  • Refer to the PHP documentation. [`echo` - Output one or more strings](http://php.net/manual/en/function.echo.php) and [`return` - returns program control to the calling module](http://php.net/manual/en/function.return.php). Since you're not returning control to another PHP module, you need to use `echo`. – Sparky Aug 31 '16 at 20:10
  • someone commented it as unclear :D can you please read it again. i don't understands how you flagged it as unclear. :D – Gaurang Joshi Sep 02 '16 at 10:13

2 Answers2

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).

Tum
  • 6,937
  • 2
  • 25
  • 23
  • 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. – Gaurang Joshi Aug 31 '16 at 13:34
  • 1
    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. – Tum Aug 31 '16 at 13:38
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");
          });
    }
});
DFriend
  • 8,869
  • 1
  • 13
  • 26