如何使用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?
我有一个按钮 此外,单击此按钮时,我有一个Ajax函数将数据发送到控制器函数 - p>
我正在用它来接收 来自Ajax的值 - p>
当 然而,这会一直回显 Next code>更改页面 - p>
\ 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;
code> 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;
code> pre>
公共函数CreateData(){
$ data = $ this-&gt; i nput-&gt; post('data');
返回$ data;
}
code> pre>
Next code>按钮命中时 控制器更改为新页面,我正在尝试将Ajax发布的值检索到
CreateData() code>函数 - p>
公共函数 step()
{
$ data = $ this-&gt; CreateData();
if(!empty($ data)){
print_r($ data); 退出;
}
其他{
echo“blank”; 退出;
}
$ this-&gt; load-&gt; view('step2');
}
code> pre>
空白代码>。 这显然意味着返回的
$ data code>是空的,但我不知道为什么。 我的Ajax脚本中的
success code>函数正在记录
data code>,因此它将值发布到
CreateData() code>。 我在这里做错了什么? p>
div>
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;
}
Adr's answer solved part of the problem. In order to actually store and access the data, storing it in the session instead of a variable did the trick -
public function CreateData() {
$data = $this->input->post();
$this->session->unset_userdata('createData');
$this->session->set_userdata('createData', $data);
}
This prevented the $data
variable from being overwritten when called the second time.