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
here we go, i found one of the solution for this question:
html file:
it should be like this in javascript, especially this variable liValue:
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:
The solution is creating new variable
var info
with data in ajax call (without it like in previous verision the data is not loaded):And humourvalue.php file:
Now i have data in database.
I’m not sure if i don’t need to set third variable which can be
init
.