两个参数的 JSON 传递


json delivery of two parameters

我正在使用ajax和php构建一个星级评定系统。当用户点击时,不仅需要发送"星级"值,还需要发送被评级项目的标识符。如何同时发送两者?我目前正在尝试回显 id#。同时,我尝试使用 PHP 实时显示 SQL 表中的当前评级。我应该以另一种方式执行此操作吗?

带单选按钮的表单

<div class="rating">
<form name="star" class="rating" method="post" action="getStarno(this.value, <?php echo $id; ?>)"
<legend><?php if(is_null($avgrate)){echo "Rate me!";} else {echo round($avgrate, 2);} ?> / 3</legend>
 <fieldset>
   <input type="radio"  name="Starno" value="1" onclick="this.form.submit()"/>
   <input type="radio"  name="Starno" value="2" onclick="this.form.submit()"/>
   <input type="radio"  name="Starno" value="3" onclick="this.form.submit()"/>
 </fieldset>
</form>
</div>

.js

    function getStarno(int, $id)
     {
     if (window.XMLHttpRequest)
       {// code for IE7+, Firefox, Chrome, Opera, Safari
       xmlhttp=new XMLHttpRequest();
       }
     else
       {// code for IE6, IE5
       xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
       }
     xmlhttp.onreadystatechange=function()
       {
       if (xmlhttp.readyState==4 && xmlhttp.status==200)
         {
         document.getElementById("rating").innerHTML=xmlhttp.responseText;
         }
       }
     xmlhttp.open("GET","../rating.php?Starno="+int,true);
     xmlhttp.send();
     }

.PHP

    <?php
    isset($_GET['id']);
    isset($_GET['starno']);
    $con=mysqli_connect ("","","","");
    mysqli_query($con,"INSERT INTO ratings (storyidr, rank, entry_date) 
            VALUES ('$_GET[id]','$_POST[starno]',now())");
    mysqli_close($con);
    ?>

您可以在表单中添加隐藏的输入字段,该字段将包含 ID 作为值。