Reputation: 11
I'm currently doing an application which uses ajax in getting user inputs from form fields then concatenate it in a string, this works fine but I'm getting a "POST 500 (Internal Server Error")
and cannot pass the data. How do I pass a string from view to controller with ajax and access it in a controller.
Here are my codes:
the script
<script>
// $(document).ready(function(){
function saveData(){
var con = document.getElementById('tiks').value;
// var dataString = {'conid':con};
alert(con);
var cct = $("input[name=csrf_token_name]").val();
var base_url = <?php base_url(); ?>;
$.ajax({
url: base_url + "welcome/newShortestPath",
type: "POST",
data: {'conid':con, 'csrf_token_name':cct},
success:function(){
alert("SUCCESS KALUY-I "+con);
$('#myForm')[0].reset();
}
});
}
controller
public function newShortestPath(){
echo "REACHED";
$var = $this->input->post('conid');
//$this -> load -> model('edge_model');
//$this->edge_model->setConstraints();
}
really need your help guys. thanks in advance. :)
UPDATE
Im no longer getting any error by fixing the errors at calling the base_url() function but at the same time I cant fetch the passed data, is there any other way fetching data from post to controller?
Upvotes: 1
Views: 1606
Reputation: 507
Tips:
Tip 1: You can setup all url:
var url = <?php echo site_url('welcome/newShortestPath'); ?>;
Tip 2: Use site_url() instead base_url(). base_url must be used to resources (img, css, js, etc.) To build URL's use site_url()
Upvotes: 0
Reputation: 8528
you have to echo
the base_url
and wrap your var with ''
var base_url = '<?php echo base_url(); ?>';
Update
based on the error you are getting which is:
Fatal error: Call to undefined function base_url()
as you mentioned in your comment.
what you have to do is to go to application/config/autoload.php
and find
$autoload['helper'] = array();
then change it to
$autoload['helper'] = array('url');
or manually load the url
helper:
$this->load->helper('url');
read more here
Upvotes: 2