如何在成功 AJAX/jQuery POST 时返回 PHP 变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13212882/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:59:52  来源:igfitidea点击:

How to return PHP variables on success AJAX/jQuery POST

phpjqueryajaxcodeigniter

提问by fabbb

How do I use AJAX to return a variable in PHP? I am currently using echo in my controller to display a price on dropdown .changein a divcalled price.

如何在 PHP 中使用 AJAX 返回变量?我目前使用我的控制器回波上显示的价格dropdown .change在一个div叫价。

However I have a hidden field which I need to return the row id to on change. How do I assign the return var in jQuery so that I can echo it in my hidden field?

但是,我有一个隐藏字段,我需要在更改时将行 ID 返回到该字段。我如何在 jQuery 中分配返回变量,以便我可以在我的隐藏字段中回显它?

jQuery

jQuery

$(document).ready(function() {
    $('#pricingEngine').change(function() {
         var query = $("#pricingEngine").serialize();
         $('#price').fadeOut(500).addClass('ajax-loading');
         $.ajax({
             type: "POST",
             url: "store/PricingEngine",
             data: query,
             success: function(data)
             {
                  $('#price').removeClass('ajax-loading').html('$' + data).fadeIn(500);
             }
         });
    return false;
   });

});

Controller

控制器

function PricingEngine()
{
    //print_r($_POST);
    $this->load->model('M_Pricing');
    $post_options = array(
      'X_SIZE' => $this->input->post('X_SIZE'),
      'X_PAPER' => $this->input->post('X_PAPER'),
      'X_COLOR' => $this->input->post('X_COLOR'),
      'X_QTY' => $this->input->post('X_QTY'),
      'O_RC' => $this->input->post('O_RC')
                          );

    $data = $this->M_Pricing->ajax_price_engine($post_options);

    foreach($data as $pData) {
        echo number_format($pData->F_PRICE / 1000,2);
        return $ProductById = $pData->businesscards_id;
    }
}

View

看法

Here is my hidden field I want to pass the VAR to every-time the form is changed. " />

这是我的隐藏字段,我想在每次更改表单时将 VAR 传递给。" />

Thanks for the help!

谢谢您的帮助!

回答by Lix

Well.. One option would be to return a JSON object. To create a JSON object in PHP, you start with an array of values and you execute json_encode($arr). This will return a JSON string.

嗯.. 一种选择是返回一个 JSON 对象。要在 PHP 中创建 JSON 对象,您需要从一组值开始,然后执行json_encode($arr). 这将返回一个 JSON 字符串。

$arr = array(
  'stack'=>'overflow',
  'key'=>'value'
);
echo json_encode($arr);

{"stack":"overflow","key":"value"}

Now in your jQuery, you'll have to tell your $.ajaxcall that you are expecting some JSON return values, so you specify another parameter - dataType : 'json'. Now your returned values in the successfunction will be a normal JavaScript object.

现在在您的 jQuery 中,您必须告诉您的$.ajax调用您需要一些 JSON 返回值,因此您指定另一个参数 - dataType : 'json'。现在success函数中的返回值将是一个普通的 JavaScript 对象。

$.ajax({
  type: "POST",
  url: "...",
  data: query,
  dataType: 'json',
  success: function(data){
    console.log(data.stack); // overflow
    console.log(data.key);   // value
  }
});

回答by Svetoslav

echo json_encode($RESPONDE);
exit(); 

The exit is not to display other things except answer. RESPONDE is good to be array or object. You can access it at

退出是不显示除回答之外的其他内容。RESPONDE 最好是数组或对象。你可以访问它

success: function(data)
             { data }

data is the responde array or whatever you echo.. For example...

数据是响应数组或您回显的任何内容..例如...

echo json_encode(array('some_key'=>'yesss')); exit();

at jquery

在 jquery

success: function(data){ alert(data.some_key); }

回答by mohan.gade

if u are returning only single value from php respone to ajax then u can set it hidden feild using val method

如果您只从 php 响应返回单个值到 ajax,那么您可以使用 val 方法将其设置为隐藏字段

$("#hidden_fld").val(return_val);