dssu33392
2016-06-26 23:47
浏览 50
已采纳

如何使用Ajax在按钮单击时将值发布到控制器功能并同时重定向到新页面?

I have a button Next that changes the page -

<a class="disabled" href="step">
   <button class="btn btn-primary launch-btn disabled next">NEXT</button>
</a>

Also, when this button is clicked, I have an Ajax function that sends data to a controller function -

<script type="text/javascript">
$(function(){
    $('.next').click(function() {
    var a = $('#box').data('val');

    $.ajax({
       type: "POST",
       url: "<?php echo base_url(); ?>study/CreateData",
       data: a,
       success: function(data)
       {
          console.log(data); 
       },
       error: function()
       {
          console.log("fail");
       } 
     });
  });
});
</script>

And I'm using this to receive the value from Ajax -

public function CreateData() {
        $data = $this->input->post('data');
        return $data;
    }

When the Next button hits its controller where it changes to a new page, I'm trying to retrieve the value posted by Ajax to the CreateData() function -

public function step()
    {
        $data = $this->CreateData();
        if(!empty($data)){
            print_r($data); exit;
        }
        else {
            echo "blank"; exit;
        }
        $this->load->view('step2');
    }

However, this keeps echoing blank. This obviously means that the $data being returned is empty but I'm not sure why. The success function in my Ajax script is logging data, so it's posting the value to CreateData(). What am I doing wrong here?

图片转代码服务由CSDN问答提供 功能建议

我有一个按钮 Next 更改页面 - \ n

 &lt; a class =“disabled”href =“step”&gt; 
&lt; button class =“btn btn-primary launch-btn next next”&gt; NEXT&lt; / button&gt; 
&lt;  / a&gt; 
   
 
 

此外,单击此按钮时,我有一个Ajax函数将数据发送到控制器函数 - < pre> &lt; script type =“text / javascript”&gt; $(function(){ $('。next')。click(function(){ var a = $('# box')。data('val'); $ .ajax({ type:“POST”, url:“&lt;?php echo base_url();?&gt; study / CreateData”, data:a, success:function(data) { console.log(data); }, error:function() { console.log(“fail”) ; } }); }); }); &lt; / script&gt;

我正在用它来接收 来自Ajax的值 -

 公共函数CreateData(){
 $ data = $ this-&gt; i  nput-&gt; post('data'); 
返回$ data; 
} 
   
 
 

Next 按钮命中时 控制器更改为新页面,我正在尝试将Ajax发布的值检索到 CreateData()函数 -

 公共函数 step()
 {
 $ data = $ this-&gt; CreateData(); 
 if(!empty($ data)){
 print_r($ data); 退出; 
} 
其他{
 echo“blank”; 退出; 
} 
 $ this-&gt; load-&gt; view('step2'); 
} 
   
 
 

然而,这会一直回显空白。 这显然意味着返回的 $ data 是空的,但我不知道为什么。 我的Ajax脚本中的 success 函数正在记录 data ,因此它将值发布到 CreateData()。 我在这里做错了什么?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

  • doushadu0901 2016-06-27 01:25
    最佳回答

    Your javascript should be something like this

    <script type="text/javascript">
    $(function(){
        $('.next').click(function() {
        var a = $('#box').data('val');
    
        $.ajax({
           type: "POST",
           url: "<?php echo base_url(); ?>study/CreateData",
           data: {my_data: a},
           success: function(data)
           {
              console.log(data); 
           },
           error: function()
           {
              console.log("fail");
           } 
         });
      });
    });
    </script>
    

    Notice the data property. I have changed it to an object containing the variable a.

    Now you should be able to retrieve this in your php function using

    public function CreateData() {
            $data = $this->input->post('my_data');
            return $data;
        }
    
    评论
    解决 无用
    打赏 举报
查看更多回答(1条)

相关推荐 更多相似问题