如何从输入类型收音机获取值并将其插入到使用PHP的数据库中?

时间:2018-08-27 15:01:05

标签: javascript php ajax

我是Web开发的新手,所以我有这个问题。我正在尝试使用以下输入来创建星级评分系统:

<fieldset class="rating">
    <input type="radio" id="star5" name="rating" value="5" /><label class = "full" for="star5" title="Awesome - 5 stars"></label>
    <input type="radio" id="star4" name="rating" value="4" /><label class = "full" for="star4" title="Pretty good - 4 stars"></label>
    <input type="radio" id="star3" name="rating" value="3" /><label class = "full" for="star3" title="Meh - 3 stars"></label>
    <input type="radio" id="star2" name="rating" value="2" /><label class = "full" for="star2" title="Kinda bad - 2 stars"></label>
    <input type="radio" id="star1" name="rating" value="1" /><label class = "full" for="star1" title="Sucks big time - 1 star"></label>
</fieldset>

我找到了用于星星的代码,所以现在我想在用户选择所需星数时将其插入数据库。我希望它没有提交按钮,所以我想我必须使用Javascript和Ajax。如何从此输入中取值(数字从1到5)并将其存储在数据库中?因此,当他单击时,例如星级4,它将获取其值并将其插入数据库。如果您有一些好的链接可以帮助我解决这个问题,我将非常感激。

1 个答案:

答案 0 :(得分:0)

这是一个php,html和jquery,可以完成您正在尝试的工作,也许这是一个不错的开始。

<?php
 if ((($_SERVER["REQUEST_METHOD"] == "POST") && (isset($_SERVER["HTTP_REFERER"]) && strpos(urldecode($_SERVER["HTTP_REFERER"]), urldecode($_SERVER["SERVER_NAME"].$_SERVER["PHP_SELF"])) > 0) && isset($_POST))) {
    // echo the values from the select facet page
    echo "You have selected " . $_POST['rating']. "<br";

    //do your db actions here
    }
 ?>

<!doctype html>
 <html>
  <head>
  <meta charset="utf-8">
  <title>Untitled Document</title>
 </head>

<body>
 <form  method="post" name="form1" id="form1">
  <p>
   <fieldset class="rating">
    <input type="radio" id="star5" name="rating" value="5" /><label class = "full" for="star5" title="Awesome - 5 stars"></label>
    <input type="radio" id="star4" name="rating" value="4" /><label class = "full" for="star4" title="Pretty good - 4 stars"></label>
    <input type="radio" id="star3" name="rating" value="3" /><label class = "full" for="star3" title="Meh - 3 stars"></label>
    <input type="radio" id="star2" name="rating" value="2" /><label class = "full" for="star2" title="Kinda bad - 2 stars"></label>
    <input type="radio" id="star1" name="rating" value="1" /><label class = "full" for="star1" title="Sucks big time - 1 star"></label>
   </fieldset>
  </form>
  <script src='http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js'></script>
  <script>
   $(document).ready(function() {
    $('input[name=rating]').change(function(){
     $('form').submit();
    });
   });
  </script>
 </body>
</html>

我从https://forums.adobe.com/thread/1832611那里获得了这一点,更改了几行代码。