简体   繁体   English

如何查询数据库中最后选择的单选按钮?

[英]How to query database for last radio button selected?

I need to know how to query the database for the last radio button selected by user so when they refresh the page or login on a different session, they will see their selection.我需要知道如何查询数据库以获取用户选择的最后一个单选按钮,以便当他们刷新页面或登录不同的 session 时,他们将看到他们的选择。 I'm using jquery.min.js (1.7.1) for the show/hide functionality.我正在使用 jquery.min.js (1.7.1) 来显示/隐藏功能。

Here is the HTML:这是 HTML:

    <div class="wrap">
    <div class="box">
      <div align=center id="catNav">
<label> &nbsp; <input type="radio" name="group_name" id="list-one-button" value="01" checked="checked" /></label>
<label> &nbsp; <input type="radio" name="group_name" id="list-two-button" value="02" /></label>
<label> &nbsp; <input type="radio" name="group_name" id="list-three-button" value="03" /></label>
<label> &nbsp; <input type="radio" name="group_name" id="list-four-button" value="04" /></label>
<label> &nbsp; <input type="radio" name="group_name" id="list-five-button" value="05" /></label>
<label> &nbsp; <input type="radio" name="group_name" id="list-six-button" value="06" /></label>
<label> &nbsp; <input type="radio" name="group_name" id="list-seven-button" value="07" /></label>
      </div>

      <ul class="cat-list" id="list-one">
<li><a href="#"><img src="img1.jpg"></a></li>
      </ul>

      <ul class="cat-list" id="list-two">
<li><a href="#"><img src="img2.jpg"></a></li>
      </ul>

      <ul class="cat-list" id="list-three">
<li><a href="#"><img src="img3.jpg"></a></li>
      </ul>

      <ul class="cat-list" id="list-four">
<li><a href="#"><img src="img4.jpg"></a></li>
      </ul>

      <ul class="cat-list" id="list-five">
<li><a href="#"><img src="img5.jpg"></a></li>
      </ul>

      <ul class="cat-list" id="list-six">
<li><a href="#"><img src="img6.jpg"></a></li>
      </ul>

      <ul class="cat-list" id="list-seven">
<li><a href="#"><img src="img3.jpg"></a></li>
      </ul>         
    </div>
</div>

and here is the Javascript:这是 Javascript:

$(function(){
$("#list-two").hide();  
$("#list-three").hide();    
$("#list-four").hide();
$("#list-five").hide();
$("#list-six").hide();
$("#list-seven").hide();

$("#list-one-button").click(function(){
    $(".cat-list").slideUp(600);
    $("#list-one").slideDown(600);
});

$("#list-two-button").click(function(){
    $(".cat-list").slideUp(600);
    $("#list-two").slideDown(600);
}); 

$("#list-three-button").click(function(){
    $(".cat-list").slideUp(600);
    $("#list-three").slideDown(600);
}); 

$("#list-four-button").click(function(){
    $(".cat-list").slideUp(600);
    $("#list-four").slideDown(600);
});

$("#list-five-button").click(function(){
    $(".cat-list").slideUp(600);
    $("#list-five").slideDown(600);
});

$("#list-six-button").click(function(){
    $(".cat-list").slideUp(600);
    $("#list-six").slideDown(600);
});

$("#list-seven-button").click(function(){
    $(".cat-list").slideUp(600);
    $("#list-seven").slideDown(600);
});

$("#catNav li a").click(function() {
    $("#catNav li").removeClass("activeCatButton");
    $(this).parent().addClass("activeCatButton");
});
});

You can achieve the desired effect by utilizing AJAX calls.您可以通过调用 AJAX 来达到预期的效果。

Here is the jQuery part:这是 jQuery 部分:

$('ul.cat-list').click(function() {
  var id = this.prop('id');
  $.ajax({
    type: "POST",
    url: "saveSelection.php",
    data: "id=" + id,
  }).done(function( msg ) {
    // this is just for debugging purposes
    // console.log("Data Saved: " + msg);
  });
});

In PHP you can save the value.在 PHP 你可以保存这个值。 This could be the file saveSelection.php :这可能是文件saveSelection.php

<?php
  // save in session
  $_SESSION['selection'] = $_POST['id'];
  // or save in database, for example by storing the user's ip address or login id
  ...
?>

Then, if the user requests the page again, check if there is a presaved value, and render the radio button as selected.然后,如果用户再次请求该页面,检查是否有预存值,并将单选按钮呈现为选中状态。

By the way, you are using lists wrongly.顺便说一下,您错误地使用了列表。 Correct would be one ul tag with multiple li tags:正确的是一个ul标签带有多个li标签:

<ul class="cat-list" id="list">
    <li id="item-one"><a href="#"><img src="img1.jpg"></a></li>
    <li id="item-two"><a href="#"><img src="img2.jpg"></a></li>
    ...
</ul>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM