A
A
Artem2020-05-20 13:11:06
PHP
Artem, 2020-05-20 13:11:06

How to choose the right shape?

There are 3 forms under their 'id' . When you click on one of the forms, the value is entered into the database and retrieved back into the php variable. Then 'echo' is displayed. If without a loop, then the value of the selected form will be shown only for the first form, despite the fact that I choose either 'id=2' or 'id=3' . If the loop then the values ​​will go in order as specified in the database.
The question is. How to set the value in the desired form. If it's 'id=3" then 'echo' will be in the form 3 .

JQUERY
$(".rating-input").on("click", function () {
    var id = $(this).attr("star-attr");// значение каждого инпута по отдельности
    var val = $(this).val();
    $.ajax({
        type: "POST",
        url: "./PHP REQUIRE/addtostars.php",
        data: "id=" + val + "&id1=" + id, // Отправка сразу нескольких значений через ajax
        dataType: "html",
        cache: false,
        success: function (data) { 
            console.log(data);
            if (data >= '0'){
                alert ('вы голосовали');
            } else {
                alert ('ваш голос учтен');
            }
        }
    });
});

PHP
$ra1 = $row['rating']; 
    $checked = intval($ra1); 
    $count = 5; 
    $inputs = '';
    for($i = 1; $i <= $count; $i++) { 
    $checked_attr = $i == $checked ? ' checked' : '';
    $inputs .= '<input  class="rating-input" id="mod' . $i . '' . $row["products_id"] . '" star-attr="' . $row["products_id"] . '" name="radio-rating" type="radio" value="' . $i . '"' . $checked_attr . '>';
    $inputs .= '<label class="rating-star" for="mod' . $i . '' . $row["products_id"] . '" ></label>';
    };

    // вывод
    echo '
    <div  class="order_right">  
     <div  class="wrap_stars">
      <form class="form-rating" method="POST"> // заблокировать форму на которую нажали
      <fieldset class="fieldset">
      <legend></legend>
      <div class="rating-group">

       ' .$inputs . '

      
       </div>
      </fieldset>
        <div class="count_block">
        <!-- Статистика -->
        <ul class="count" >' . $row["rating"] . '</ul>
        </div>
       </form>
      </div>
    </div>

5ec5091f2cb60171029016.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
AUser0, 2020-05-20
@AUser0

So you do not betray the form id in the AJAX code.
And they didn’t show what SQL query you use to read the data in order to show it to the user.
And in the output of the form there is no logic "form number 3 - here we pass the number to it."

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question