【问题标题】:Jquery refresh div block one time to show php sessionJquery刷新div块一次以显示php会话
【发布时间】:2013-02-04 05:10:09
【问题描述】:

如何刷新 id 包含 PHP 会话的 div?

我的 JQ ajax 调用:

$('#btn_1').click(function(){
    var parameters = $('#booking_form').serialize();

    $.ajax({
        url: baseurl + 'site/process',
        type: 'POST',
        data: parameters,
        dataType: 'json',
        success: function(output_str){
            if(output_str == "false"){
                $('#result_msg').html("please select one");

            }else{
                $('#result').html(output_str);
                $('#loaddiv').load();

            }
        }
    });
});

这是我想在 ajax 回调后重新加载的地方:

<p>
<div id="result" style="color:#606;"></div>
</p>

<div id="loaddiv">
    <p><?php echo $selected; ?></p>
    <p><?php echo $sum_charges; ?></p>
</div>

$selected$sum_charges是一个php会话,在处理过程中存储,只有手动刷新会话才会显示在页面中,如何让会话在结果回调到页面时立即显示?

谢谢!!

编辑:

我在我的流程中使用了 codeigniter(在 CI 中仍然是新的),这是我的控制器的一部分用于此流程:

public function process(){
    $confTypes = $this->input->post('cType');
    $user_email = $this->input->post('user_email');
    $output_str = NULL;
    $conf_charge_total = 0;

    if(!empty($confTypes)){

        foreach($confTypes as $confType){
            $explode_value = explode(",", $confType);

            $conf_type = $explode_value[0];
            $conf_charge = $explode_value[1];

            $conf_charge_total += $conf_charge; //sum up checked values

            $output_str .= $conf_type.":".$conf_charge.",";
            /*$output_str = array(
                    'selected' => $conf_type.','.$conf_charge,
                    'email' => $user_email,
                    'sum' => $conf_charge_total,
                    'flag' => 'true'
                );*/
            $this->session->set_userdata('selected', $output_str);

        }

        $output_str .= $conf_charge_total;

        $this->session->set_userdata('sum_charges', $conf_charge_total);

    }else{
        $output_str = "false";
    }

    echo json_encode($output_str);
}

【问题讨论】:

  • 您的 js 脚本是否在同一页面上...或者您在单独的 js 文件中维护它??
  • 据我了解,您想在处理 baseurl + 'site/process' 页面后更新 div#loaddiv
  • @bipen,我的 .js 文件是分开的。
  • @Rohan Kumar,是的,这就是我想要的。

标签: jquery refresh php


【解决方案1】:

ajax 调用页面返回json 就像你使用dataType: 'json' 一样,那么被调用页面应该返回一个json formatted string 就像

echo json_encode(array("selected"=&gt;"abc","sum_charges"=&gt;123));

在您的ajax function 中更改成功功能,如:

success: function(output_str){
    if(output_str == "false"){
        $('#result_msg').html("please select one");

    }else{
        $('#result').html(output_str);
        var str="<p>"+output_str.selected+"</p><p>"+output_str.sum_charges+"</p>";
        $('#loaddiv').html(str);
    }
}

【讨论】:

  • 我已经尝试过您的更改,但 loaddiv 仍然需要再次刷新页面以显示会话。
  • @conmen 首次加载页面时,它将通过变量$selected$sum_charges 显示default session values,然后单击一个按钮,这些值应根据baseurl + 'site/process' 页面更改,对吧?
  • 是的,我希望会话中的这些值用于进一步处理,这就是为什么我不能只通过output_str.selected 输出。
  • 好的,上面只会显示更新的值,你想将这些值存储在session 然后页面site/process 将通过设置session variable 来做到这一点,比如$_SESSION['selected'] 等,也适用于进一步使用您可以在$selected 变量中分配此值或直接使用$_SESSION['$selected']
【解决方案2】:

如果您的 js 脚本在同一页面中,您可以这样做..

success: function(output_str){
        if(output_str == "false"){
            $('#result_msg').html("please select one");

        }else{
            $('#result').html(output_str);
            var divContent="<p><?php echo $selected; ?></p><p><?php echo $sum_charges; ?></p>"; //this will echo the latest session variable...
              $('#loaddiv').html(divContent);

        }
    }

更新

以 json 格式发送会话变量

PHP

 echo json_encode(array('selected'=>$selected,'sum_charges'=>$sum_charges,'output_str'=>'yourStr'));

JQUERY

success: function(output_str){
        if(output_str == "false"){
            $('#result_msg').html("please select one");

        }else{
            $('#result').html(output_str.output_str);
            var dinContent="<p>"+output_str.selected+"</p><p>"+output_str.sum_charges+"</p>"; //this will echo the latest session variable...
              $('#loaddiv').html(dinContent);

        }
    }

【讨论】:

  • 其他解决方法是将会话变量作为 json 从服务器发送并显示...
猜你喜欢
  • 1970-01-01
  • 2015-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-18
  • 1970-01-01
  • 2014-10-16
相关资源
最近更新 更多