here is my view
<html>
<body>
<form method="post" name="myForm1" id="myForm1" enctype="multipart/form-data" >
Email: <input type="text" name="email" id="email">
Question: <input type="text" name="qText" id="qText">
<input id="submitbutton" type="submit">
</form>
<div id="abc"></div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script> //no need to specify the language
$(document).ready(function() {
$('#myForm1').on("submit",function(e) {
//var form = $(this);
//dataString = $("#myForm1").serialize();
e.preventDefault();
$.ajax({
type: "POST",
url: "<?php echo site_url('form_controller/insert_into_db'); ?>",
data: $(this).serialize(),
//dataType: "json",
success: function(data){
// top.location.href = "<?php echo site_url('form_controller/callform'); ?>";
//$.each(data.results, function(){
// $("#abc").append('<div><b>' + id.id + '</b></div><hr />');
//});
/*var site_url = "<?php// echo site_url('form_controller/callform/') ?>";
site_url = site_url +"/" + id;
$("#abc").load(site_url);*/
<?php //foreach(): ?>
var site_url = "<?php echo site_url('form_controller/callform'); ?>";
var mydata=window.JSON.stringify(data.trim());
alert(mydata);
//site_url = site_url +"/" +data.id;
alert(site_url);
$("#abc").load(site_url);
//$('#abc').html(data);
var item = data;
alert(item.id);
}//,
//error: function() { alert("Error posting feed."); }
});
});
});
</script>
</body>
</html>
controller
function index(){
$this->load->view('submit_form');
}
function callform($id){
$this->load->view('view_form',$id);
}
public function insert_into_db(){
$this->load->helper('url');
$this->load->model('form_model');
$data= $this->form_model->insertQ();
$this->output->set_output(json_encode($data));
}
}
model
<?php
class Form_model extends CI_Model{
function insertQ(){
$email = $this->input->post('email');
$text = $this->input->post('qText');
$this->db->query("insert into form (email,text) values('$email','$text')");
$this->load->database();
$query = $this->db->query("SELECT MAX(id) AS id FROM form");
return $query->result();
}
}
when insert record into database there is a auto increment id. I need to get that particular id from database and return it to the success function in ajax. then I need to load another page into a div and print that id on newly loaded content.here the problem is in view I couldn't get data into variable.for site_url
You can get the inserted id
$id = $this->db->insert_id();