0

I have some loop using data from SQlite db and then i show all that data using bootstrap ul/li and data from database.

Code is something like this (without opening and closing php tags):

$results = $db->query("SELECT * FROM igre WHERE cat = 'slot'");
<div class="container-fluid">
<div class="row">
while($row=$results->fetchArray(SQLITE3_ASSOC)){
$game= $row['kod'];

<div class="gallery_product col-xs-6 col-sm-4 col-md-2 col-lg-2">
<span onclick="spremi()">

<i class="fas fa-star fav-icon"></i></span><a href="#" class="tranzicija zacrni"><img src="sample.jpeg"></a>

</div>

 } 

</div>
    </div>

I defined variable like $game= $row['kod']; and i it works fine in this loop, but the problem is when i try to use this variable value in jquery function that i use for storing data.

The function i use for storing data onclick (spremi) only get's value from last record, not from record i clicked on:

<script>
function spremi(){
    $korisnik = "<?php echo $uid;?>";
    $kod = "<?php echo $game;?>";
    $.ajax({
        url: "prihvati.php",
        data: {korisnik : $korisnik, igra : $kod},
        type: "GET",
        dataType: "json",
        //on success
        success: function(data){
        },
        error: function(){
        }
    });
}

I need this function to work fine for every record, for example if the game is "ABC" to read that from $game variable, or second game is "TRZ" to save that, and so on...

1
  • Your script is outside the php loop so of course by the time you get to the script $game holds the value from last iteration. You should pass the value of game as parameter with spremi(); Commented Feb 26, 2019 at 22:06

2 Answers 2

2

Pass game variable to javascript function:

...
<span onclick="spremi('<?=$game?>')">
...

And then use it in spremi function.

function spremi(game){
    $.ajax({
        url: "prihvati.php",
        data: {korisnik : "<?=$uid?>", igra : game},
        type: "GET",
        dataType: "json",
        //on success
        success: function(data){
        },
        error: function(){
        }
    });
}
Sign up to request clarification or add additional context in comments.

Comments

1

The problem is that your Javascript code is outside your while loop. So you are just writing the last value which those variables had when the loop ended.

You can fix it by letting your JavaScript function accept those values as input parameters, and making the call to the function (which is created within the loop) pass the relevant values in:

PHP / HTML

$results = $db->query("SELECT * FROM igre WHERE cat = 'slot'");

<div class="container-fluid">
<div class="row">

while($row=$results->fetchArray(SQLITE3_ASSOC)) {
  $game = $row['kod'];

  <div class="gallery_product col-xs-6 col-sm-4 col-md-2 col-lg-2">
  <span onclick="spremi('<?php echo $uid;?>', '<?php echo $game;?>')">

  <i class="fas fa-star fav-icon"></i></span><a href="#" class="tranzicija zacrni"><img src="sample.jpeg"></a>

</div>
} 
</div>
</div>

JavaScript:

function spremi(korisnik, kod) {
   $.ajax({
        url: "prihvati.php",
        data: { korisnik : korisnik, igra : kod},
        type: "GET",
        dataType: "json",
        //on success
        success: function(data){
        },
        error: function(){
        }
    });
}

N.B. In your original PHP code you did not include any reference to $uid so I have assumed it is populated somewhere before I have used it above.

1 Comment

Thank you for your response, thats it, it only need '' in <span onclick="spremi('<?php echo $uid;?>', '<?php echo $game;?>')">.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.