Change the value of the field dynamically [closed]

-2

Hello, I would like help with my code, I wanted it when the user clicked the field with the magnifying glass:

<a class="blue" href="#">
    <i class="ace-icon fa fa-search-plus bigger-130"></i>
</a>

The value of the status field: <?php $status = $dados['status'];

Dynamically change the bank from "N" to "S". Initially the value saved for all publications is "N" but I want it to change to "S" when the user clicks. I'm doing this which is similar to whatsapp; Clicking is displayed, but it's an application in PHP, a website.

Looking for I saw that javascript and ajax would already solve this, but I do not know how (I also saw that onClick = function () would already resolve); I've set up an if to retrieve the values afterwards with refresh.

<?php $status = $dados['status'];
        if  ($status == "N"){
            echo "<i class='ace-icon fa fa-circle red'></i>";}
        else if($status == "S"){
            echo "<i class='ace-icon fa fa-circle green'></i>"; 
} ?>
    
asked by anonymous 14.02.2017 / 17:14

1 answer

2

I would do this using jQuery. I did a fiddle that exemplifies how you should implement this functionality.

Basically you get the click event from the element and make an AJAX call to the URL that will make the change in the database, in which case you will need a simple PHP code to do this. Here's an example of what it would look like, you'd have to adapt to your case.

<?php
if (isset($_POST['status']) && isset($_POST['id'])) {
    $status = $_POST['status'];
    $id = $_POST['id'];

    // Atualiza no banco de dados
    $servername = 'localhost';
    $dbport = 123;
    $database = 'nome_db';
    $username = 'root';
    $password = 'senha123';

    $db = new PDO("mysql:host=$servername;port=$dbport;dbname=$database", $username, $password);
    $stmt = $db->prepare("UPDATE mensagem SET status='?' WHERE id='?'");
    $data = array($status, $id);

    if ($stmt->execute($data))
        echo '1'; // sucesso
    else
        echo '0'; // falha
} else {
    echo '0'; // falha
}
    
15.02.2017 / 14:07