How to fix cant retrieve data with jquery - php

i should tell you my code is actually work, but a litle bit of annoying problem i cant solve by my self.
The problem is i only can retrieve the first record(id) from my data grid but i cant retrieve the second or other record id
example :
when i click my edit button of my first record of my data then i want to send the id to my jquery and show it in console and use it for selecting data form database (this is work)
but when i click edit of my second record of my data it wont send data to my Jquery and show it in console
This is my Grid code
<?php foreach($data['gakubu_data'] as $gakubu):?>
<li class="list-group-item d-flex justify-content-between align-items-center"><?= $gakubu['code_gakubu'];?> | <?= $gakubu['gakubu'];?>
編集
</li>
<?php endforeach;?>
</div>
</div>
this is my Jquery Script
$(function(){
$('#showUpdateModal').on('click',function(){
$('#formModalLabel').html('データ編集');
$('.modal-footer button[type=submit]').html('データ編集');
const id = $(this).data('code');
console.log(id);
});
});

Your loop creates multiple elements with the same id (id="showUpdateModal"). Id's must be unique within a document, so when you select it with: $('#showUpdateModal'), it will always return the first element with that id (since there shouldn't be any more elements with the same id).
You can solve it by using a class instead:
編集
(I removed the id and added showUpdateModal as a class instead)
Now you can register the event on the class instead:
$('.showUpdateModal').on('click', function() {
// Your current code
});

Related

Pass loop data to jquery ajax model

I have a loop of data from my Lesson table.
When I click Specific lessons link it opens a Model.
I want to pass specific Lesson_id to Videos form but jquery repeats the first Index of data(first lesson's id).
I have used both an Id and Class but the results are the same.
here is my code.
<a class="btn btn-info add_video" data-id="<?php $lesson->id ?>" data-toggle="modal" data-target="#myModal">Add Video</a>
And this is Jquery Function
$(document).ready(function(){
$('#add_video_form').on('submit', function(event){
event.preventDefault();
var link = $('.add_video').attr('data-id');
alert(link);
});
});
Here, you are getting value of link with class name. May it get duplicate or used in you html structure more than one time.
I suggest you to check the length of add_video class with $('.add_video').length;
If It is returning more that 1, you have got the error. It needs to return 1. In above case. Also, you can try to check length with $('#add_video_form .add_video').length;
If it returns 1, then you can get the same value with var link = $('#add_video_form .add_video').data('id'); or you can use Id as well to retrieve the specific element.

onmouseover getting the id of a list item which refers to a mysql table & populate a new list with the table rows

So I want to make a dynamic submenu which directly gets data from mysql table
I have this js to get the current list item id...
$(document).ready(function() {
$('.courseTitle').mouseover(function() {
var id=this.id;
});
});
then I have one of the parent menu list item..
<li class="courseTitle" id="100"><a href="../courses/100/">Course100</li>
I want to get the id of the parent list item without page refresh. the submenu populating script is something like-
<ul id="chaptList">
<?php $crsID=$_REQUEST['course'];
require('../common/dbcon/courseCon.php');
$chapt_count=mysql_query("select * from course_$crsID order by chapter_id");
MY Js snippet is getting the li element id nicely.. but I need to pass the id value to my php snippet somehow whenever mouseover occurs
How abt this:
var id
$(document).ready(function() {
$('.courseTitle').mouseover(function() {
id = $(this).attr('id');
});
});
I decided to go for the old school method.. used simple arrays.. as I guess retreiving data from database on runtime every now n then is not that much of a good option

To perform ajax functioning with jquery

Simply, I have an <a> tag having values(ID) which will be posted to the same page in clicking,
I wanted to load data into the table based on the ID provided by the post method. On the other hand I am having a clock which is rested again and again when fired a post method.
I simply wanted to do the same via jQuery.
In short I wanted to Implement ajax using jQuery
Also I am using database MySQL, with PHP scripting
Any sort help will be appreciated.
<a class="D" href="?ID=<?php echo $rows[0]; ?>" onclick="">Question<?php echo $QNo; ?></a>
I wanted the same above and get ID for searching relevant data against the ID.
I might be wrong here, but you basically want to query a PHP page based on an ID and display relevant content in a div?
To make an AJAX call, setup a PHP page that will query your database and in turn return HTML data which you can then display in a DOM element.
A very simple example of this would be:
HTML
Load table 1
Load table 2
<div id="contentToPopulate"></div>
jQuery:
$('a.linkToLoadTable').click(function(){
var pageId = $(this).data('tableId')
$.ajax({
url: 'loadTable.php?id='+pageId
}).done(function(data) {
$('#contentToPopulate').html(data)
});
})

Pass the information of tags to the php and display in another page( and can also update in another page)?

I am working on a tag system:
1.you can select some tags from a list and display them in a tag container (the tag can be selected only once and the sum is limited to 10), and different tag has different colors.
2.you can delete some selected tags in the tag container
3.pass the information to the php and store in the database.
4. display the tags in another page and you can update the selected tag list in this page.
For now the first two steps has finished by javascript but I am quite confused how I can pass the selected information to the php and the database (the content and colors) so they can be displayed and updated in another page.Anyone can give me some suggestions? Thanks.
The link to the jsfiddle is http://jsfiddle.net/V9Euk/1015/
Here is the html:
<ul>
<li data-val="300"><span class="label label-morning">Morning</span></li>
<li data-val="301"><span class="label label-afternoon">Afternoon</span></li>
<li data-val="302"><span class="label label-evening">Evening</span></li>
</ul>
<div class="tagHandler">
<ul class="tagHandlerContainer" id="tag_handler">
</ul>
</div>
here is the javascript:
$(function(){
var tags = [];
function add_tag(that){
var tag = $(that).text();
if($.inArray(tag, tags)>=0|| tags.length >= 10) return;
tags.push(tag);
var singleValues = $(that).find('span').clone();
singleValues[0].innerHTML += "&times";
$("#tag_handler").append(singleValues);/*display the selected tags in the tag_handler with &times style*/
}
$("li").click(function(){
add_tag(this);
});/*add tags to the tag_container when click the li*/
$('#tag_handler').on('click', 'span', function(){
var tag = $(this).text();
var index = $.inArray(tag, tags);
tags.splice(index,1);
$(this).remove();
});/*remove the tag when click this tag in the tag_container*/
});
First of all the jsfiddle link doesn't work for me.
Now, the only way is to use http methods like POST/GET to pass the data from client to server. The implementation depends of what you like the most or better a friendly and easy to use interface, so my suggestions are:
You can create (dynamically or not) a form (with hidden fields for example) and update their values with JS and pass the data through a submit button which is an easy implementation.
An another implementation is to use Ajax if you take care of user selection and create the data structure dynamically.
In both cases, you should validate the correctness of the submitted data with php. Never trust the user or the "supposed" JavaScript restrictions.

load revelant data when clicking on link using jQuery

Hey, I'm trying to make an Address book for my site and I was thinking of having two panes. The one on the left to have Contact names, and the one on the right to have the contact details.
I can get the Contact pane loaded up using a while loop to cycle through my query, but then I was thinking that I really had no Idea how to load the details of that contact when the user clicks on a specific contact name.
How would I be able to approach this problem?
If you have something like:
<div id="contactNamesPane">
<ul>
<li>Doe, John</li>
</ul>
</div>
<div id="contactDetailsPane">
</div>
$('contactNamesPane a.names').click(
var thisContactId = $(this).attr('id');
$('contactDetailsPane')
.load('http://path/to/script.php?contactId=' + thisContactId + ' #' + thisContactId);
);
This has the assumptions that you have a php script to generate the contact details (located at http://path/to/script.php and this script can take a GET variable in order to show the particular individual.
It also assumes that this data will be placed inside an element of an id equal to the contact's name.
A vaguely coherent demo's posted on my server at: http://davidrhysthomas.co.uk/so/loadDemo.html
When you create the list on the left add the id of the contact to the rel
<ul id="contacts">
<li>Name Here</li>
...
</ul>
Then with jquery you can use the rel attribute and peform a ajax request to a php page that returns the info of the contact with that id
$("#contact a").click(function() {
var id = $(this).attr("rel");
$.get("url_of_php_page", { id: id },
function(data){
// do something with the data
}
);
});
use jquery.data instead

Categories