简体   繁体   English

没有退出功能,PHP无法返回值

[英]PHP not return value without exit function

I need help. 我需要帮助。 I am getting problem in returning value from Codeigniter. 我从Codeigniter返回价值时遇到问题。 Whenever, I use exit; 每当我使用exit; after echo it work fine but whenever i try return true it's dosen't work. echo后,它工作正常,但是每当我尝试return true它就不会工作。

Same as i have comment code in PHP code. 与我在PHP代码中使用注释代码相同。 if i use exit after echo it works but if i don't do that it returns nothing 如果我在echo后使用exit可以正常工作,但是如果我不这样做,则不返回任何内容

Ajax Request Ajax请求

$('#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 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;
    }
}

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. CodeIgniter具有布局,因此在输出响应后,可能会在响应后输出视图,例如页脚或调试栏。

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 ). 还要注意,在CodeIgniter中退出AJAX之后退出并不是一个坏习惯,因此也许您应该编写一个AJAX响应帮助程序来完成所有工作(例如设置标头和添加exit )。

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 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");
          });
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM