I am trying to use ajax method to insert form data into a database and then redirect it to the next page. I have successfully passed the data in ajax and inserted it into the database table. However, I am facing an issue with getting the generated reference ID in the response. I need this reference ID to control other form entries on the next page. How can I achieve this?
Below is my form part (having two or three inputs for reference) with Ajax code:
<form id="visaForm" name="visaForm" action="https://www.domainurl.com/india-visa/personal-details" method="post">
<input type="hidden" id="country" name="country" value="DZA">
<input type="hidden" id="reference_number" name="reference_number" value="">
<input type="hidden" id="visa_type_id" name="visa_type_id" value="1">
<input id="next" class="btn primary-btn" type="button" value="Save & Continue">
</form>
<script>
$('#next').on('click',function(){
var $form = $('#visaForm');
var serializedData = $form.serialize();
$.ajax({
type:'POST',
url:"https://www.domainurl.com/india-visa/add-applicants",
data: "gofor=save_data&"+serializedData,
success:function(response){
responsepre = response.split('~');
document.getElementById("reference_number").value= responsepre[0];
$('#visaForm').submit();
}
});
});
</script>
This is my Controller Method:
public function add_applicants()
{
$this->load->model('visa/Process_model');
$this->Process_model->insertappdata();
}
This is my Model:
function insertappdata()
{
$query=$this->db->query("select * from appstbl ORDER BY id DESC LIMIT 1");
$data = $query->result_array();
$str = $data[0]['appid'];
$int = preg_replace('/[^0-9]/', '',$str);
$pure_str = str_replace($int, "", $str);
$appid = $pure_str.($int+147);
$data = array(
'phoneno' => $this->input->post('mobile'),
'whatsappno' => $this->input->post('whatsappnumber'),
'email' => $this->input->post('email_id'),
'portofarrival' => $this->input->post('port_of_arrival'),
'appid'=>$appid,
);
$this->db->insert('appstbl', $data);
}
I would like to pass the inserted "Appid" into Myview so that it can be used on the next Controller Method using the Form Action URL.