Problem when inserting data in MYSQL with Codeigniter and Ajax

0

Good morning,

What I want to do is that by pressing "Enter" I insert a new tag into the database.

I created my script.js file:

$(".inputTag").keypress(function(e){
    if(e.which == 13) {
        $.ajax({
            type: 'POST',
            url:"../system/libraries/InsertTag.php",
            data:{ 
                'inputTag' :$(".inputTag").val()
            },
            success:function(){
                alert("hecho");

            },
            error:function(){
                alert("error");
            }
        });
    }
})

But I receive the following error:

POST http://localhost/MIAPP/system/libraries/InsertTag.php 403 (Forbidden)

Can someone tell me what I'm doing wrong and how to fix it? I guess my problem is that I do not usually use codeigniter because this without it works perfectly.

    
asked by Korzan 22.03.2017 в 13:17
source

3 answers

2

From what I see InserTag is a library, since it is located inside that folder, I think the problem is that you access the library directly. You should create a controller that is responsible for managing the operations of the library and from there call the library with

$this->load->library('InsertTag'). 

I do not know if you use the functionality that exists in the library in other sites ... because if you only use InserTag for this ajax, you can remove InserTag from the library and create it as a controller.

A controller can be called directly from ajax but a library can not.

    
answered by 22.03.2017 / 14:04
source
0

Codeigniter does not allow access to directories or files in the system folder directly, your url is ../system/libraries/InsertTag.php , so you can not access your library directly, Codeigniter follows the MVC pattern, so calls to your libraries you should do them from a controller.

Assuming that your library is InsertTag.php , it is recommended to place the libraries in the directory application/libraries/ , then load it on your controller with:

$this->load->library('InsertTag').
    
answered by 22.03.2017 в 14:15
0

That as commented by Juan Pizon, codegnaiter handles the MVC model (Movel, view, controller).

in your ajax in the url: "functionDeTuControlador". There must be a function in your controller.php, which is in the applications / controllers / controller.php folder.

example called Ajax:

$.ajax({
                type:"POST",  
                dataType:"json",
                url:"nombreFuncionDeTuControlador",
                data:'inputTag' :$(".inputTag").val(),
                success:function(data){
                  alert("ok: "+data);
                },error: function(data){
                    console.error(data);            
                }       
            });

Example code in controller remembers the path of the folders of your project in codegnaiter usually all have the same name unless you change it (applications / controllers / controller.php.)

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class controlador extends CI_Controller {
    public function nombreFuncionDeTuControlador(){
     //y aqui obtienes los valores que envias del form
     $valorExtraido = $this->input->post("inputTag");
   //o puedes usar la forma tradicional
  $valorExtraido = $_POST["inputTag"];
   echo "ok"
     } 
}
?>

all this for the MVC model that manages codegnaiter.

In fact you can invoke the function from the url link

And one thing when you get the value of an input I recommend you use the elmento id since you used the class $ (". inputTag"). val () with id $ ("# inputTag"). val () since the id is unique and a class is used in different areas and is used more for styles that can be jquery jquery can also access html elements with the class but for a form is recommended the id of your input. p>

I hope you get the information greetings.

    
answered by 01.12.2017 в 04:03