Insert record into bank by clicking div x

Asked

Viewed 100 times

3

How can I enter a record to the database by clicking 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

  • Edit with the code you already made

  • I only have the function to insert in the bank.

  • I created an imput with an onclick, but then the ajax function I couldn’t do

  • You need to do javascript, detect the click on the div and trigger ajax to update the values with php.

  • I’ve got it all figured out.

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 file update.php:

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

if(filter_input(INPUT_POST, 'status')){
$status = filter_input(INPUT_POST, 'status');
atualizar($status);
}

0

For that you’d have to use ajax, using pure javascript or else jquery, and attach an event scavenger to that div. However, I don’t see much point in doing this with a div, and even more so in this way.

In this example I will use jquery because I don’t 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 event click to avoid complications, and it’s also a little awkward to process requests whenever the mouse passes over a div, which will probably happen all the time. I commented on the upcoming events.

We in requisitions with the ajax you can always choose between $.post, $.get, or even $.ajax.

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

To the html you’d have something like that:

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

And this to dispose 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 script php 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 exchange of values of this same variable would be, for the case of status be different with each request. For a query where you have the treatment of session variables you would put the query SQL, being 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}'");

Browser other questions tagged

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