0

I have a home.php page in which I load posts with an Ajax function from load_posts.php This is the important code of load_posts.php:

...
$userid = $_POST['userid'];
$sql = mysqli_query($conn, "SELECT * FROM storie WHERE userid IN (SELECT following FROM follow WHERE follower='$userid')");
while($row = mysqli_fetch_assoc($sql)){
$idsto=$row['storia_id'];
...
?>
<div class="storia" id="storia_<?php echo $idsto;?>">
...
<div class="star-rating">
      <input id="star-5" type="radio" name="rating" value="5" class="stellette" />
      <label for="star-5" title="5 stars">
        <i class="active fa fa-star fa-sm" aria-hidden="true"></i>
      </label>
      <input id="star-4" type="radio" name="rating" value="4" class="stellette"/>
      <label for="star-4" title="4 stars">
        <i class="active fa fa-star fa-sm" aria-hidden="true"></i>
      </label>
      <input id="star-3" type="radio" name="rating" value="3" class="stellette"/>
      <label for="star-3" title="3 stars">
        <i class="active fa fa-star fa-sm" aria-hidden="true"></i>
      </label>
      <input id="star-2" type="radio" name="rating" value="2" class="stellette"/>
      <label for="star-2" title="2 stars">
        <i class="active fa fa-star fa-sm" aria-hidden="true"></i>
      </label>
      <input id="star-1" type="radio" name="rating" value="1" class="stellette"/>
      <label for="star-1" title="1 star">
        <i class="active fa fa-star fa-sm" aria-hidden="true"></i>
      </label>
    </div>
</div>
<?php
}
?>

I'd like to have two jQuery variables, one with the value of the $idsto rating, and the other with the rating checked value from 1 to 5.

When I have only one single rating in a page, I simply do this:

$('input[name=rating]').change(function(){
var voto = $( 'input[name=rating]:checked' ).val();

But now I don't know how to get the specific rating of the id $idsto. I tried writing this, but with no results:

$("input[name^='rating']").change(function(){
...
name="rating_<?php echo $idsto;?>"

Can anyone help me?

Tommaso
  • 99
  • 1
  • 9
  • You need to set checked of radio button which matches this `$idsto` ? Please elaborate more . – Swati Sep 27 '20 at 07:06
  • I'd like to have two jQuery variables, one with the value of the $idsto rating id, and the other with the rating checked value from 1 to 5. Thank you for the answer – Tommaso Sep 27 '20 at 07:09
  • How about putting that `$idsto` value in some hidden field and access same inside your change event of jquery ? – Swati Sep 27 '20 at 07:18
  • Thank you for your help, I think it is a good solution, but the problem is that I'm not able to access the change eventi, it doesn't work (I tried with a simple alert and it isn't shown) – Tommaso Sep 27 '20 at 07:41
  • yes because these radios are dynamically created so use `$(document).on('change', 'input[name=rating]', function(){ //your jquery code ..})` .Check [this](https://stackoverflow.com/questions/203198/event-binding-on-dynamically-created-elements) post as well for more info – Swati Sep 27 '20 at 07:44
  • **Warning:** You are wide open to [SQL Injections](https://stackoverflow.com/a/60496/1839439) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/5741187) – Dharman Sep 27 '20 at 12:26
  • It doesn't work... this is the code: $(document).on('change', 'input[name=rating]', function(){ alert("hoho"); }); – Tommaso Sep 27 '20 at 15:48

0 Answers0