skip to Main Content

i have such an example… From here (http://jsfiddle.net/tpgun6Lz/5/) i have dropdown menu(with images) using ul li (in my example there will be few different menu).
Here is a code – i can add the id to <ul>(but as you will see jquery using class not using id), and data-value to <li> for the purpose of solve problem:

<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>

and 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>

I want to save value ul(id), and value li (data-value) in PHP using jQuery and ajax. I prepare some code but I don’t know how to setup them(I’m not sure about variables setup and 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>

and file 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));
    }

?>

I’m not sure if this topic will be helpful to get data from database: how to get data to html <ul> <li> </li> </ul> list from mysql database with php

I need to focus first how to save variables in database. Please help me with this, course it makes me crazy.

——————UPDATE————
Now i undertand – the javascript code colide with ajax call, couse we have

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

in javascript and in ajax we have

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

Can anyone have some idea how to fix it?

2

Answers


  1. Chosen as BEST ANSWER

    here we go, i found one of the solution for this question:

    html file:

    <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>
    

    it should be like this in javascript, especially this variable 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>
    

    Here is a fiddle: http://jsfiddle.net/23oumxaf/24/ Now i just have problem with sending data into database, i dont know what is wrong with this:

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

  2. The solution is creating new variable var info with data in ajax call (without it like in previous verision the data is not loaded):

    <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>
    

    And humourvalue.php file:

    <?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);
    }
    
    ?>
    

    Now i have data in database.
    I’m not sure if i don’t need to set third variable which can be init.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search