Change the field value dynamically

Asked

Viewed 162 times

-2

Hello, I would like help with my code, I wanted when the user clicked on 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'];

Change, dynamically in the bank from "N" to "S". Initially the value saved for all posts 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 as viewed, but it’s a PHP application, a website.

Looking for javascript and ajax would already solve this, but I don’t know how (I also saw that onClick=Function() would already solve); I built an if to recover the values later 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>"; 
} ?>
  • Gives a search on the site on ajax, has enough and assembles a test, there reformulates with a punctual doubt, so is very broad...

  • very broad ...

1 answer

2


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

Basically you take the element click event 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. To follow an example of what it would be like, you would 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
}

Browser other questions tagged

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