Insert record in bank by clicking div x

3

How can I insert a record into the database when I click on a div x

I tried to use ajax but could not do the function

I have the function

$status = "on";
db::Query("UPDATE bdc SET status='$status'");

I wanted to insert this function by clicking on a div div X

    
asked by anonymous 03.11.2015 / 17:56

2 answers

0

Your link:

 <a href="#" id="atualiza">Atualizar</a>

And your Javascript (assuming you use JQuery)

$.ajax({
  method: "POST",
  url: "atualizar.php",
  data: { status: "on"}
})
  .done(function( msg ) {
    alert( "Atualizado!" );
  });

In your update.php file:

function atualizar($status){
db::Query("UPDATE bdc SET status='$status'");
}

if(filter_input(INPUT_POST, 'status')){
$status = filter_input(INPUT_POST, 'status');
atualizar($status);
}
    
03.11.2015 / 23:06
0

For this you would use ajax , using pure javascript or jquery , and add an event sink to that div. However, I do not see much use in doing this with a div , and even more so.

In this example I will use jquery because I do not have the best conditions at my disposal.

$(document).ready(function(){
    // mouseover, mouseout, leave
    $("#click").on('click', function(){
        var rando = 1 + Math.floor(Math.random() * 100); // gerar numeros entre 1 e 100
        $.post('upd.php',{status: rando}, function(i){
            $("#demo").html('Retorno: ' + i);
        });
    });
  });

This is the part of jquery , I used the click event to avoid complications, and it is also a bit inconvenient to process requests whenever the mouse passes over a div, which will probably always be happening. I commented the event renters.

In requests with ajax you can always choose between $ .post, $ .get, or even $ .ajax .

  • $ .post and $ .get (without much configuration);
  • $ .ajax (more configuration options, both for get and post ).

For html you would have something like this:

<div id="demo"></div>
<div id="click"></div>

And this to arrange the divs one above the other:

<style type="text/css">
  div {display:block;}
  #click {width:100%; height:20px; background-color:#204a87; content:"OI";}
</style>

And finally, for the php script you could do something like this:

session_start();

$data = isset($_POST['status']) && !empty($_POST['status']) ? (int) $_POST['status'] : "indefinido\n";

// Restantes verificações para $data...

if(isset($_SESSION['status']) && !empty($_SESSION['status'])){
    echo $_SESSION['status'];
} else {
    $_SESSION['status'] = $data;
    echo $_SESSION['status'];
}

session_destroy();

In this example, I used sessions to illustrate how the values of this variable would change, in case the status values differ from each request. For a query where you have the session variables you would put the SQL query, like this:

$status = isset($_POST['status']) && !empty($_POST['status']) ? (int) $_POST['status'] : "indefinido";

// Restantes verificações para $status...

db::Query("UPDATE bdc SET status='{$status}'");
    
04.11.2015 / 02:41