1

I have a problem when sending data with ajax,in the URL it adds .php to my controller

need help :(

this is the AJAX script to make call to Welcome controller:

<script>
$(document).ready(function () {
$('#palier').change(function(){
   var idf=$(this).val();
 console.log(idf);
    $.ajax({
       url : "<?php echo base_url(); ?>"+"Welcome/affectation_exam",
       type : "POST",
       data:{idf:idf},
       datatype:"text",
       success:function(data){
           $('#specialite').html(data); 
          // console.log(data);
       }


    });

});
})

</script>

in the controller i have :

public function affectation_exam($page='affectation des examens')
  {

    $data['title']=$page;

      $this->load->model('Palier');
      $this->load->model('Specialite');
      $data['records']=$this->Specialite->get_specialite();
      $data['palier']=$this->Palier->get_palier();
      $this->load->view('template/main',$data);
      echo $_POST["idf"];


  }
3
  • May be Slash is missing in url. "<?php echo base_url(); ?>"+"/Welcome/affectation_exam" Commented Dec 15, 2017 at 9:38
  • you need to pass csrf token along with the post request Commented Dec 15, 2017 at 10:16
  • i didn't anderstand .. can u show me an example ? Commented Dec 15, 2017 at 10:39

2 Answers 2

2

Try this

$.ajax({      
  url  : "<?php echo base_url(); ?>/welcome/affectation_exam";
  ...
});

If you have not removed index.php then use it as

$.ajax({      
  url  : "<?php echo base_url(); ?>/index.php/welcome/affectation_exam";
  ...
});

Edited

Change your controller code like

public function affectation_exam($page='affectation des examens')
{

    $data['title']=$page;
    $this->load->model('Palier');
    $this->load->model('Specialite');
    $data['records']=$this->Specialite->get_specialite();
    $data['palier']=$this->Palier->get_palier();
    $result = $this->load->view('template/main',$data, TRUE);
    //$_POST["idf"];
    echo $result; exit;

}
Sign up to request clarification or add additional context in comments.

10 Comments

doesn't work its sending welcome.php/affectation_exam .. ...is that a problem with htaccess ?
Yes it might be htaccess problem. did you removed index.php?. Check my updated answer.
yep ... i have this : RewriteEngine On RewriteCond $1 !^(index\.php|public|resources|assets|css|img|js|fonts|robots\.txt) RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 [L]
did you set $config['index_page'] = ""; in config.php file?
yes i did ... acctually the url welcome/affectation_exam work but the problem is that its sending welcome.php and that url does't much any page (404)
|
1

1) There is no requirement for concatenation of URL string.

2) The controller name needs to be in lower-case.

$.ajax({
       url : "<?php echo base_url(); ?>welcome/affectation_exam",
       .....
     });

3 Comments

may be there is some issue with your htaccess file. Also does in console it show .php extension ?
yep and the page does not exist: 404page not found
check $config['url_suffix'] variable in your config.php. It should be set to empty string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.