我有一个包含两个部分的视图文件。
<!--first section-->
<section class="section-one">
<!--some code here like form with submit button-->
<?php echo form_open('user_control/submit'); ?>
<input type="submit" name="submit" value="Submit">
<?php echo form_close(); ?>
</section>
<!--two section-->
<section class="section-two"><!--some success code here--></section>
.section-two{display:none;}//hide section-two
当用户点击URL时,第一部分将首先显示,然后在填写表单后,用户将单击提交按钮。
它会调用我的控制器名为user_control/submit
。
我在控制器中添加了一些代码,以便在正在运行的数据库中提交数据。
现在我的问题是在数据库中提交数据后如何在同一个视图页面上重定向并隐藏第一部分并显示第二部分而不刷新页面?
我的意思是,我必须在同一个视图页面中将数据提交到数据库后显示成功文本。
你能帮助我吗?
此处的控制器代码
public function submit()
{
global $now;//current date
$check_fields = implode(",", $this->input->post('check-fields'));
$data = array(
'form_data'=> $check_fields,
'form_created_date'=> $now
);
$this->db->insert('form_submit',$data);
}
答案 0 :(得分:0)
尝试使用ajax提交表单。收到ajax响应后隐藏第一部分并显示第二部分。下面是您可以从中获取提示的代码。按下按钮调用ajax。另外,将输入类型提交更改为输入类型按钮以进行ajax调用。
if Rover.nav_angles.size:
...
答案 1 :(得分:0)
查看:
var url = $('html').data('base-url')+'index.php/controller/ajax_some_method';
$.ajax({
url: url,
method: "POST",
data : {data_to_pass_accross}
success: function(result) {
if(result) {
$(".section-one").hide();
$(".section-two").show();
} else {
//display some error message
console.log(response.message);
}
}
});
控制器:
public function ajax_some_method(){
//make model call
//get response back
//send response back to view
$data = ["success"=>true, "message"=>some message", "data"=>"what ever"];
return die(json_encode($data));
}