【问题标题】:How to save ul li variables to database如何将ul li变量保存到数据库
【发布时间】:2021-02-27 02:58:27
【问题描述】:

我有一个这样的例子...从这里 (http://jsfiddle.net/tpgun6Lz/5/) 我有使用 ul li 的下拉菜单(带图像)(在我的例子中会有几个不同的菜单)。 这是一个代码 - 我可以将 id 添加到 <ul>(但你会看到 jquery 使用不使用 id 的类),并将数据值添加到 <li> 以解决问题:

<ul class="prod-gram" id="menu">
    <li class="init" data-value="1"><img src="http://smartangielski.j.pl/img/hej.png" /> Country 1</li>
    <li data-value="2"><img src="http://smartangielski.j.pl/img/hej.png" /> Country 1</li>
    <li data-value="3"><img src="http://smartangielski.j.pl/img/hej.png" /> Country 2</li>
</ul>
<ul class="prod-gram" id="menu2">
    <li class="init" data-value="1"><img src="http://smartangielski.j.pl/img/hej.png" /> Country 1</li>
    <li data-value="2"><img src="http://smartangielski.j.pl/img/hej.png" /> Country 1</li>
    <li data-value="3"><img src="http://smartangielski.j.pl/img/hej.png" /> Country 2</li>
</ul>

和javascript:

<script>
    $(document).ready(function() {
        $(document).on("click", "ul.prod-gram .init", function() {
            $(this).parent().find('li:not(.init)').toggle();
        });
        var allOptions = $("ul.prod-gram").children('li:not(.init)');
        $("ul.prod-gram").on("click", "li:not(.init)", function() {
            allOptions.removeClass('selected');
            $(this).addClass('selected');
            $(this).parent().children('.init').html($(this).html()); // this line colide with ajax
            $(this).parent().find('li:not(.init)').toggle();
        });
    });
</script>

我想使用jQuery 和ajax 在PHP 中保存值ul(id) 和值li(数据值)。我准备了一些代码,但我不知道如何设置它们(我不确定变量设置和 ajax):

<script>
    $('#menu').click(function(){
        var liValue = $(this).closest('li').attr('data-value');
        var ulId = $(this).closest('ul').attr('id');
        jQuery.ajax({
            type: "POST",
            url: 'humourvalue.php',
            data:{ul: ulId, li: liValue},
            success: function(data) {
                $("#menu").html(data); // i tried to change datatype but then i cant send them to php
            }
        });
    });
</script>

和文件 humourvalue.php

<?php
    require_once 'includes/config.php';

    if(isset($_SESSION['user_id'])) {
        if ($userID === null) {
            $userID = $_SESSION['user_id'];
        }

        $userID = intval($userID);
        $ulId = $db->$_POST['ul'])));
        $liValue = $db->$_POST['li'])));
        $result = $db->query("UPDATE `users` SELECT `opts`='".($ulId)."' WHERE id=".($userID));
    }

?>

我不确定这个主题是否有助于从数据库中获取数据:how to get data to html <ul> <li> </li> </ul> list from mysql database with php

我需要首先关注如何在数据库中保存变量。请帮帮我,这让我很抓狂。

------------------更新------------ 现在我明白了 - javascript 代码与 ajax 调用相冲突,因为我们有

$(this).parent().children('.init').html($(this).html());

在 javascript 和 ajax 中我们有

success: function(data) {
         $("#menu").html(data);

谁能知道如何解决它?

【问题讨论】:

  • 我需要首先关注如何将变量保存在数据库中。 你这样做!为此,网上有一本 MySQL 用户手册和数以千计的教程,可以帮助您将 UDPATES 与 SELECTS 分开。 SO 不是教程网站
  • 您的UPDATE 不正确,并且对 SQL 注入开放。尚不清楚 JS 是否能做到这一点,或者问题出在 JS 上。
  • 您好,您需要获取被点击的li 的值吗?还是全部 lis ?
  • 斯瓦蒂,如果这是问题 - 是的,我需要获取被点击的 li 的值,所有的 lis

标签: javascript php html jquery ajax


【解决方案1】:

我们开始了,我找到了解决这个问题的方法之一:

html 文件:

<ul class="prod-gram" id="menu">
    <li class="init" data-value="value 1"><img src='https://raw.githubusercontent.com/stevenrskelton/flag-icon/master/png/16/country-4x3/ph.png' /> Country 1</li>
    <li data-value="value 1"><img src='https://raw.githubusercontent.com/stevenrskelton/flag-icon/master/png/16/country-4x3/ph.png' /> Country 1</li>
    <li data-value="value 2"><img src='https://raw.githubusercontent.com/stevenrskelton/flag-icon/master/png/16/country-4x3/cr.png' /> Country 2</li>
    <li data-value="value 3"><img src='https://raw.githubusercontent.com/stevenrskelton/flag-icon/master/png/16/country-4x3/nc.png' /> Country 3</li>
    <li data-value="value 4"><img src='https://raw.githubusercontent.com/stevenrskelton/flag-icon/master/png/16/country-4x3/se.png' /> Country 4</li>
  </ul>

在javascript中应该是这样的,尤其是这个变量liValue:

<script>
$(document).ready(function() {
  $(document).on("click", "ul.prod-gram .init", function() {
    $(this).parent().find('li:not(.init)').toggle();
  });
  var allOptions = $("ul.prod-gram").children('li:not(.init)');
  $("ul.prod-gram").on("click", "li:not(.init)", function() {
    allOptions.removeClass('selected');
    $(this).addClass('selected');
    $(this).parent().children('.init').html($(this).html());
    $(this).parent().find('li:not(.init)').toggle();
  });
});

$("#menu").click(function() {
  var liValue = $("ul.prod-gram").find(".selected").data("value");
  var ulId = $(this).closest('ul').attr('id');
  alert(liValue);
});
</script>

这是一个小提琴:http://jsfiddle.net/23oumxaf/24/ 现在我只是在将数据发送到数据库时遇到问题,我不知道这有什么问题:

  $liValue = $db->$_POST['li'];
  $ulId = $db->$_POST['ul'];
  $result = $db->query("UPDATE `users` SET `li`='".$liValue."'  WHERE id=".($userID));

【讨论】:

    【解决方案2】:

    解决方案是在 ajax 调用中使用数据创建新变量 var info(没有它,就像以前的版本一样,数据不会加载):

    <script>
        $(document).ready(function() {
            $(document).on("click", "ul.prod-gram .init", function() {
                $(this).parent().find("li:not(.init)").toggle();
            });
            var allOptions = $("ul.prod-gram").children("li:not(.init)");
            $("ul.prod-gram").on("click", "li:not(.init)", function() {
                allOptions.removeClass("selected");
                $(this).addClass("selected");
                $(this).parent().children(".init").html($(this).html());
                $(this).parent().find("li:not(.init)").toggle();
            });
        });
    
    $("#menu").click(function(){
        var liValue = $("ul.prod-gram").find(".selected").data("value");
        var ulId = $(this).closest("ul").attr("id");
        var info = {                                  // here is the modification
            "li":liValue,
            "ul":ulId
        };
        console.log(info);
    
        $.ajax({
            data: info,
            url: "humourvalue.php",
            type: "POST",
            method: "POST",
            success: function(result){
                console.log("finished");
                console.log(result);
            },
            error: function(result){
                console.log("error");
                console.log(result);
            }
        });
    });
    </script>
    

    还有 humourvalue.php 文件:

    <?php
    
    header('Access-Control-Allow-Methods: GET, POST');
    header("Access-Control-Allow-Headers: X-Requested-With");
    
    require_once 'includes/config.php';
    
    if(isset($_SESSION['user_id'])){
        if ($userID === null) {
            $userID = $_SESSION['user_id'];
        }
    
        $userID = intval($userID);
        $result = $db->query("UPDATE `users` SET `li`='".$_POST['li']."', `ul`='".$_POST['ul']."' WHERE id=".($userID));
        echo JSON_ENCODE($result);
    }
    
    ?>
    

    现在我在数据库中有数据。 我不确定我是否不需要设置第三个变量,它可以是init

    【讨论】:

    • 解决方案对我很重要,我可以编辑和删除的问题
    猜你喜欢
    • 2015-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-03
    • 2020-03-22
    • 1970-01-01
    相关资源
    最近更新 更多