【问题标题】:PHP not return value without exit functionPHP没有退出函数不返回值
【发布时间】:2017-01-08 02:44:43
【问题描述】:

我需要帮助。我在从 Codeigniter 返回值时遇到问题。无论何时,我在echo 之后使用exit; 都可以正常工作,但每当我尝试return true 时,它都不起作用。

与我在PHP 代码中的注释代码相同。如果我在echo 之后使用exit,它可以工作,但如果我不这样做,它什么也不会返回

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

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

【问题讨论】:

  • 不起作用怎么办?
  • 我试过retuen,但它不起作用。感谢您的评论
  • 它应该做什么而不是做什么?请edit您的问题添加更多问题的解释。
  • 我想你正在经历这个stackoverflow.com/questions/10107144/…
  • 由于您的方法以 if than else 结尾,因此将echo 1echo 0 留在它们的位置没有任何问题。 social() 的末尾没有更多代码要执行。

标签: php ajax codeigniter codeigniter-3


【解决方案1】:

您可能需要更具体地说明您的回声。这是几种可能的解决方案之一。

控制器

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

【讨论】:

    【解决方案2】:

    CodeIgniter 有一个布局,因此在输出响应后,可能会在您的响应之后输出视图,例如页脚或调试栏。

    尝试使用您的控制台查看响应的状态代码。另请注意,在 CodeIgniter 中,在 AJAX 调用后退出并不是一个坏习惯,因此也许您应该编写一个 AJAX 响应帮助器来为您完成所有这些工作(例如设置标头并添加 exit)。

    【讨论】:

    • 感谢您的建议。我会和你一起去,但如果有新的东西,让我再举一些例子,你也会得到很棒的答案。
    • 我还建议您在 JavaScript 中将响应类型设置为 JSON,并从 PHP 返回 JSON 结果。就像echo json_encode(array('result',1));(当然还有exit() ;))。这样您就可以在 JavaScript 中获得完整的响应对象。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-05
    • 1970-01-01
    • 2013-08-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多