I have multiple drop down box, it is a result on a loop.
foreach($Transs as $Trans){
<select data-id="'.$Trans->coursetitle.'" name = "Schedule" id = "Schedule" class = "form-control" '.$disabled.'>
<option></option>
</select>
}
Everytime i want to click a select box, it will populate a data from php and display as option in the clicked box.
Here my ajax
<script>
$(document).on("click", "#Schedule", function () {
var subid = $(this).data('id');
var dataString = 'id=' + subid;
$.ajax({
type: "POST",
url: "../class/formschedule.class.php",
data: dataString,
cache: false,
dataType: 'json',
success: function (data) {
var len = data.length;
$("#Schedule").empty();
for( var i = 0; i<len; i++){
var id = data[i]['id'];
var name = data[i]['name'];
$("#Schedule").append("<option value='"+id+"'>"+name+"</option>");
}
}
});
});
</script>
But what happens is that only the first drop down box is populated not what i clicked.
HTML Page must contain unique id. You are giving same id to multiple select that's why only first id event done. Change your id to class then perform action
foreach($Transs as $Trans){
echo '<select data-id="'.$Trans->coursetitle.'" name = "Schedule" class = "form-control Schedule" '.$disabled.'>
<option></option>
</select>';
}
Ajax:
<script>
$(document).on("click", ".Schedule", function () {
var subid = $(this).data('id');
var dataString = 'id=' + subid;
var attr = $(this);
$.ajax({
type: "POST",
url: "../class/formschedule.class.php",
data: dataString,
cache: false,
dataType: 'json',
success: function (data) {
var len = data.length;
$(attr).empty();
for( var i = 0; i<len; i++){
var id = data[i]['id'];
var name = data[i]['name'];
$(attr).append("<option value='"+id+"'>"+name+"</option>");
}
}
});
});
</script>
Related
I have a modal button inside the table for each row which is an edit button and inside the modal, there is the autocomplete query from the database.
Issue: The (jQuery) auto-complete works only for the first row of the table. It doesn't work for the next row and so on.
This is my script:
$(document).ready(function() {
$(document).on('keydown', '.second', function() {
var id = this.id;
var splitid = id.split('_');
var index = splitid[2];
$('#' + id).autocomplete({
source: function(request, response) {
$.ajax({
url: "getDetails.php",
type: 'post',
dataType: "json",
data: {
search: request.term,
request: 1
},
success: function(data) {
response(data);
}
});
},
select: function(event, ui) {
$(this).val(ui.item.label); // display the selected text
var userid = ui.item.value; // selected id to input
$.ajax({
url: 'getDetails.php',
type: 'post',
data: { userid: userid, request: 2 },
dataType: 'json',
success: function(response) {
var id = response[0]['id'];
var biddernumber = response[0]['biddernumber'];
var fb_id = response[0]['fb_id'];
document.getElementById('fb_id_' + index).value = fb_id;
document.getElementById('biddernumber_' + index).value = biddernumber;
}
});
return false;
}
});
});
});
Everything works fine, the only thing I need is to make it work on all rows.
I want to post the content of div(text) to a php page with ajax, i have already this script which post all the input fields of the three forms
.on('actionclicked.fu.wizard' , function(e, info){
if(info.step == 3 ) {
var dataString = $("#form1, #form2, #form3").serialize();
var mydiv = $('#mydiv').html();
$.ajax({
data: dataString + mydiv,
type: 'POST',
url: 'confirmation.php',
cache: false,
success: function(data) {
message : 'you request was submitted'
}
});
}
})
but i need to post also the text of the div in the php page i use
$mydiv =$_REQUEST['mydiv'];
I can echo all the other fields but nothing return for $mydiv.
Are you looking for something like this?
<div id="myDiv">
<h1>This is a page</h1>
</div>
$("#post").click(function(){
var htmlData = $("#myDiv").html();
$.post('postPage.php', {'html': htmlData },function(response){
//
});
`.on('actionclicked.fu.wizard' , function(e, info){
if(info.step == 3 ) {
var a = $("#form1").serialize();
var b = $("#form2").serialize();
var c = $("#form3").serialize();
var d = $(#mydiv).text();
data = {};
data['astring'] = a;
data['bstring'] = b;
data['cstring'] = c;
data['ddivtext'] = d;
$.ajax({
data: data,
type: 'POST',
dataType: "json",
url: 'confirmation.php',
cache: false,
success: function(data) {
message : 'you request was submitted'
}
});
}
})
I am trying to get the count values of different function to sum up and then show the values.What my end goal is to get the notification count for when user like someone post when someone accepted your request and when someone commented on the post i need to get the notification for all three in the same notification badge so need to add the count from all the functions and give the last result to the badge.let me show you what i am trying to do
1st function
function recieve_accept_notification()
{
var id = $('.id_data').attr('value');
// var Recievers_id = $('.id_data').attr('value');
// var senders_id = $('.senders_div').attr('senders_id');
jQuery.ajax({
type: 'POST',
url: '<?php echo base_url('user/user_recieve_return_requests'); ?>',
data: {
id: id,
},
dataType:'json',
success: function(data) {
// alert(data);
if(data=="")
{}
else{
var ParsedObject = JSON.stringify(data);
var count='0';
var recievers_values = $.parseJSON(ParsedObject);
$.each(recievers_values, function(key, data) {
count++;
var uname = data.uname;
var senders_id = data.friend_id;
var Recievers_id = data.user_id;
var friends_request_image=data.image;
{
$('.recieve_accept_notification').append(' <strong>' + uname + ' ' + '</strong>accepted your friend Request <div style="padding-left: 11em;"></center></div>');
}
});
$('#notification_count').val(count); //this sends the no of counted values to the html input
}
} });
}
function when_post_like_notification()
{
var id = $('.id_data').attr('value');
jQuery.ajax({
type: 'POST',
url: '<?php echo base_url('user/user_get_posts_id'); ?>',
data: {
id: id,
},
dataType:'json',
success: function(data) {
var ParsedObject = JSON.stringify(data);
var recievers_values = $.parseJSON(ParsedObject);
$.each(recievers_values, function(key, data) {
// alert(recievers_values);
var Recievers_id = data.id;
// alert(Recievers_id);
jQuery.ajax({
type: 'POST',
url: '<?php echo base_url('user/user_liked_post_notification'); ?>',
data: {
id: Recievers_id,
},
dataType:'json',
success: function(data) {
// alert(data);
if(data=="")
{}
else{
var ParsedObject = JSON.stringify(data);
var recievers_values = $.parseJSON(ParsedObject);
var count='0';
$.each(recievers_values, function(key, data) {
count++;
var uname = data.uname;
var Recievers_id = data.user_id;
var friends_request_image=data.image;
$('.recieve_accept_notification').append(' <strong>' + uname + ' ' + '</strong>liked your post.<div style="padding-left: 11em;"></center></div>');
});
var notification_count= $('#notification_count').val();
var new_count= parseInt(notification_count)+ parseInt(count) ;
this adds the previous value from the notification and shows the result in for next .
$('#notification_count').val(new_count);
}
}
});
});
}
});
}
function when_someone_commented_onpost()
{
// alert();
var id = $('.id_data').attr('value');
jQuery.ajax({
type: 'POST',
url: '<?php echo base_url('user/user_get_posts_id'); ?>',
data: {
id: id,
},
dataType:'json',
success: function(data) {
console.log(data);
if(data="")
{}
else{
var ParsedObject = JSON.stringify(data);
var recievers_values = $.parseJSON(ParsedObject);
$.each(recievers_values, function(key, data) {
var Recievers_id = data.id;
jQuery.ajax({
type: 'POST',
url: '<?php echo base_url('user/user_get_all_comments_by_someone'); ?>',
data: {
id: Recievers_id,
},
dataType:'json',
success: function(data) {
var ParsedObject = JSON.stringify(data);
var recievers_values = $.parseJSON(ParsedObject);
var count= '0';
$.each(recievers_values, function(key, data) {
count++;
var uname = data.uname;
var Recievers_id = data.user_id;
var friends_request_image=data.image;
$('.recieve_accept_notification').append(' <strong>' + uname + ' ' + '</strong>commented on your post.<div style="padding-left: 11em;"></center></div>');
});
var notification_count= $('#notification_count').val();
var new_count=parseInt(notification_count)+ parseInt(count) ;
$('#notification_count').val(new_count);
}
});
});}
}
});
}
function add_all_counts()
{
value=$('#notification_count').val();
// alert(value);
this is the final function that shows the value to the span and give us the notification
$('#notification_count').val(value);
$('#accept_request_count').html('<span class="badge" >'+value+'</span>');
}
this is the html area that show the result
<span style="font-size: 1.3em; z-index: 9999999;" class="fa fa-globe" id="accept_request_count" ></span> <input type="hidden" name="notification_count" id="notification_count" value="0">
let me show you some images that would explain better
I am unable to display data in the set of DIVs from MySQL database.
The data is showing only in last DIV, below is my code:
$(document).ready(function (){
var n = 9;
for(var i=0;i<n;i++){
var div = document.createElement('div');
div.className = "d5";
div.id=i+1;
document.getElementById('container').appendChild(div);
$.ajax({
url: 'myapi.php', //the script to call to get data
data: "", //you can insert url argumnets here to pass to api.php for example "id=5&parent=6"
dataType: 'json', //data format
success: function(data) //on recieve of reply
{
var Name = data[2]; //get id
//var vname = data[1]; //get name
$('#'+div.id).html(""+Name);
}
});
}
});
Add one more property when you make ajax request, async:false. Hope you get your result
$.ajax({
url: 'myapi.php', //the script to call to get data
data: "",
async:false , //you can insert url argumnets here to pass to api.php for example "id=5&parent=6"
dataType: 'json', //data format
success: function(data) //on recieve of reply
{
var Name = data[2]; //get id
//var vname = data[1]; //get name
$('#'+div.id).html(""+Name);
}
});
set ajax async:false
$(document).ready(function (){
var n = 9;
for(var i=0;i<n;i++){
var div = document.createElement('div');
div.className = "d5";
div.id=i+1;
document.getElementById('container').appendChild(div);
$.ajax({
async: false,
url: 'myapi.php', //the script to call to get data
data: "", //you can insert url argumnets here to pass to api.php for example "id=5&parent=6"
dataType: 'json', //data format
success: function(data) //on recieve of reply
{
var Name = data[2]; //get id
//var vname = data[1]; //get name
$('#'+div.id).html(""+Name);
}
});
}
});
You can close your Ajax call in an anonymous function:
(function(div.id){
$.ajax({
url: 'myapi.php',
data: "",
dataType: 'json',
success: function(data)
{
var Name = data[2];
//var vname = data[1];
$('#'+div.id).html(""+Name);
}
});
})(div.id);
Because the Ajax call is async, the value of div.id has changed before the Ajax call returns Reference
If myapi.php always returns the same data and you want to display it in multiple divs
$(document).ready(function() {
$.ajax({
url: 'myapi.php', //the script to call to get data
data: "", //you can insert url argumnets here to pass to api.php for example "id=5&parent=6"
dataType: 'json', //data format
success: function(data) //on recieve of reply
{
var n = 9;
for (var i = 0; i < n; i++) {
var div = document.createElement('div');
div.className = "d5";
div.id = i + 1;
document.getElementById('container').appendChild(div);
var Name = data[2]; //get id
//var vname = data[1]; //get name
$('#' + div.id).html("" + Name);
}
}
});
});
If myapi.php returns different data for each request we might have multiple solutions
One of them is just setting the async attribute to false
The other one is passing the ID of the div to the server and return it with the response
$(document).ready(function() {
var n = 9;
for (var i = 0; i < n; i++) {
var div = document.createElement('div');
div.className = "d5";
div.id = i + 1;
document.getElementById('container').appendChild(div);
$.ajax({
url: 'myapi.php?id=' + div.id, //the script to call to get data
data: "", //you can insert url argumnets here to pass to api.php for example "id=5&parent=6"
dataType: 'json', //data format
success: function(data) //on recieve of reply
{
var Name = data[2]; //get id
//var vname = data[1]; //get name
var divId = data[3];
$('#' + divId).html("" + Name);
}
});
}
});
The problem with you is that the div.id value is updated before you receive the response and the latest div get filled because the loop will be faster than the request
One more solution can be defining a function and pass the container id to it
function loadContentInto(divId) {
$.ajax({
url: 'myapi.php', //the script to call to get data
data: "",
async: false, //you can insert url argumnets here to pass to api.php for example "id=5&parent=6"
dataType: 'json', //data format
success: function(data) //on recieve of reply
{
var Name = data[2]; //get id
//var vname = data[1]; //get name
$('#' + divId).html("" + Name);
}
});
}
and call this method from your loop
for (var i = 0; i < n; i++) {
var div = document.createElement('div');
div.className = "d5";
div.id = i + 1;
document.getElementById('container').appendChild(div);
loadContentInto(div.id);
}
I hope that can help :)
I have a program that sends a user id through AJAX and to a php program that searches a table for all records that match that user id. The problem is that it's only returning one row and I need for it to return all the rows that match. Any ideas on how to do this?
MySQL statement in php:
$id=$_POST["id"];
$sql = mysql_query("SELECT * FROM crime_map WHERE user_id = '$id'");
while($row = mysql_fetch_assoc($sql))
$output[]=$row;
echo json_encode($output);
JS code:
function submitform() {
var id = document.getElementById('id').value;
var datastr = 'id=' + id;
alert(datastr);
$.ajax({
type: "POST",
url: 'api.php',
data: datastr,
dataType: 'json',
success: function(data){
var user_id = data[0];
alert(user_id);
}
})
}
function submitform() {
var id = document.getElementById('id').value;
var datastr = 'id=' + id;
//alert(datastr);
$.ajax({
type: "POST",
url: 'api.php',
data: datastr,
dataType: 'json',
success: function(data){
//var user_id = data[0];
//alert(user_id);
if(data.length > 0)
{
for(i=0; i<data.length; i++)
{
alert("User: " + data[i].user_id);
// here you have the user_id and any other fields from the table e.g. lat/long
}
}
}
})
}
Have a look at the Google Maps Polyline reference for how to plot the points:
http://code.google.com/apis/maps/documentation/javascript/reference.html#Polyline