bring data from the database with Javascript

Asked

Viewed 1,165 times

0

I have a <a href="javascript:void(0);" class="justificar" data-value="<?= $l->img_id?>"> that when you click, I need you to bring a data from the database, but I don’t know how to pull this data to the screen. My code is codeigniter, and when you click on <a href> he calls this class:

$(document).on('click','.justificar', function(){
var t = $(this);
$.SmartMessageBox({
title : "<?= $this->lang->line("con_inflaud_jus_msg_title"); ?>",
content : "<?= $this->lang->line("view_relat_iten_title_jus"); ?>",
buttons : "[<?= $this->lang->line("con_inflaud_jus_msg_btn_ok"); ?>]",

}, function(ButtonPress, Value) {
if(ButtonPress == "<?= $this->lang->line("con_inflaud_jus_msg_btn_ok"); ?>"){
return 0;
}
});
});

How do I bring the data from the database

  • wouldn’t it be better to use php to get it from the bank?

  • 1

    What you’re looking for is an ajax call. You need to send a request to the server and treat it in codeigniter to bring your data and then vc recovers the return from ajax and displays the content

  • @Rafaelacioly and how I can call a controller function in javascript without using the window?

2 answers

1

What you’re looking for is an ajax call. You need to send a request to the server and treat it in codeigniter to bring your data and then vc recovers the return from ajax and displays the content.

$( document ).ready(function () {
  // define o clique do botão
  $("#button").click(function () {
    // faz uma requisição ajax do tipo $_GET
    $.get("/teste/tempo", function (tempo) {
      // atualiza o campo input#tempo com o tempo
      $("#tempo").val(tempo);
    });
  });
});

on the controller side you create an action tempo and defines the return

public function tempo()
{
    echo time();
} 

and so humanity walks...

---- Edit ----

Tip: it would be interesting to separate php from javascript for the sake of separation of responsibility, let each one control their own space.

0

I already got it another way. I created a class for the button and in the Javascript file I put the following code:

$(document).on('click', '.just', function (e) {
    logged();
    e.preventDefault();
    $.getJSON("Controller/funcao/" + $(this).attr("data-value"), function (j) {
        $('#jus_just').html(j);
        $('#myModal').modal('show');
    });
});

and in this Controller I called the table and the field that stores the required field.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.