0

I have page with data from database:

<?php
$host = 'localhost';
$user = 'root';
$pass = '';
$base = 'zar';
try {

$db = new PDO('mysql:host='.$host.';dbname='.$base.';charset=utf8', $user, $pass, array(PDO::ATTR_EMULATE_PREPARES => false,PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));

$name = 'name';
$pin = 'pin';
$ip = 'ip';
$id = 'id';

$statement = $db->query('SELECT id, ip, name, pin FROM devic');

foreach($statement as $wiersz)
{

?>
<div id = "<?php print($wiersz['id'])?>"
<img class= "obraz"  src="css/bulb_off.png" alt="...">
 <div class="relayBlock"><span class="relayTitle"><?php print($wiersz['name']) ?> </span>
    <button class="btn btn-block btn-lg btn-primary " value="on">Wł</button>
    <button class="btn btn-block btn-lg btn-danger " value="off">Wył</button>
</div>
</div>
<?php
}   
$statement->closeCursor();

} catch(PDOException $err) {
    exit('error: '.$err->getMessage());
}
?>

JS:

<script type="text/javascript">
        $(document).ready(function () {
            $('.btn').click(function() {
                var val = $(this).val();            
                $.ajax({
                    url: "try.php",
                    type: "POST",
                    data: {'myVar': val},                         
success : function(data) {
        if (data == 1) {

            $('.obraz').attr({src : "css/bulb_on.png"})
        }
        else{
            $('.obraz').attr({src : "css/bulb_off.png"})
    }
}
      });
            });
        });
    </script>

Now, if I click any button, the pictures for all the rows in the database change. I want to do so that after clicking a button for a specific row from the database, the picture changed only for that line. I know that this is because every picture has the same class for the picture, but can I do it in a different way?

5
  • use a WHERE clause to get specific data from a said row. Commented Dec 19, 2017 at 16:32
  • 1
    If you want to update only a specific record then you'd need to identify that specific record in the WHERE clause in your UPDATE statement. Commented Dec 19, 2017 at 16:35
  • No, I wan't to update data on database, I only want to update data on my html page. Commented Dec 19, 2017 at 16:40
  • if you don't want to update the record, what is the purpose of the call to "try.php"? Commented Dec 19, 2017 at 16:50
  • try.php send request for other server and return 1 or 0. But it isn't important at this moment. Commented Dec 19, 2017 at 16:54

2 Answers 2

1
var idParent = getvalueId from the div parent
if (data == 1) {
     $('#' + idParent +' .obraz').attr({src : "css/bulb_on.png"})
} else{
     $('#' + idParent +' .obraz').attr({src : "css/bulb_off.png"})
}

jquery, selector for class within id

Sign up to request clarification or add additional context in comments.

1 Comment

"var idParent = getvalueId from the div parent" should be in function "function(data)"?
0

I add changed code, it works: JS:

<script type="text/javascript">
        $(document).ready(function () {
            $('.btn').click(function() {
                var val = $(this).val();
                var idParent = $(this).parent().attr("id");
                $.ajax({
                    url: "try.php",
                    type: "POST",
                    data: {'myVar': val},

success : function(data) {
        if (data == 1) {            
            $('#' + idParent +' .obraz').attr({src : "css/bulb_on.png"})
        }
        else{
            $('#' + idParent +' .obraz').attr({src : "css/bulb_off.png"})
    }
}
      });
            });
        });
    </script>

Thanks @Ricard Espinàs Llovet for suggestion.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.