getting random values from different function when counting their values - php

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

Related

Fetching data from database using PHP and AJAX

I want to fetch data from database using PHP and Ajax, which is first encoded into JSON.
But data is not printed properly on the screen. It shows elements of four rows in single line separated by comma.
$(document).ready(function() {
$(function()
{
$.ajax({
url: 'demo2.php',
data: "",
dataType: 'json',
success: function(data)
{
var name = data[0];
var email = data[1];
var msg = data[2];
var date1 = data[3];
$('#output').html("<div id='container'>" + name + " " + email + " " + msg + " " + date1 + "</div><br>");
}
});
});
});
Try this:
$(document).ready(function() {
$(function()
{
$.ajax({
url: 'demo2.php',
data: "",
dataType: 'json',
success: function(data)
{
console.log(JSON.stringify(data));
var obj = JSON.parse(data);
// Iterate object:
my_text=''
$.each(obj, function(index, value) {
console.log(value);
my_text += value
});
// var obj = JSON.parse('{ "name":"John", "email":"email#domain.com", "msg":"Hello"}');
$('#output').html("<div id='container'>" + my_text + "</div><br>");
}
});
});
});
Fiddle:
https://jsfiddle.net/fks3j500/

Posting text of div with ajax

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

jQuery stopped working with PHP

I have written some code with jQuery Session, GET etc. What happens is when you click a Text Link it should come up with "Success"
But when there's no Data it works, but when I put in the data and fix it, the code stops working completely.
$(function() {
$(".open").click(function() {
var status = "Open";
var ticketid = "<?php echo $_GET['id']; ?>";
var username = "<?php echo $_SESSION['MM_Username']; ?>";
var dataString = 'status=' + status + 'id=' + ticketid + 'username=' + username;
if(status==='' || ticketid==='' || username==='' || dataString==='') {
$('.success').fadeOut(200).hide();
$('.error').fadeOut(200).show();
} else {
$.ajax({
type: "POST",
url: "core/ticketData.php",
data: dataString,
success: function(result){
$('.success').fadeIn(200).show();
$('.error').fadeOut(200).hide();
}
});
}
return false;
});
});
This is from my View Source
$(function() {
$(".open").click(function() {
var status = "Open";
var dataString = 'status='+status;
if(status==='') {
$('.success').fadeOut(200).hide();
$('.error').fadeOut(200).show();
} else {
$.ajax({
type: "POST",
url: "core/ticketData.php?id=772&username=NoMansLand",
data: dataString,
success: function(result){
$('.success').fadeIn(200).show();
$('.error').fadeOut(200).hide();
}
});
}
return false;
});
});
but when the PHP is empty it works completely, but when it's there it stops. I have tried Echo & print.
Any ideas?
UPDATED
Ok I have worked on this, The alerts work when you: Click it, Check Variables, Go through else, but when you hit $.ajax({ it wont alert.
<script>
$(function() {
$(".open").click(function() {
var status = "Open";
var ticketid = "<?php echo $_GET['id']; ?>";
var username = "<?php echo $_SESSION['MM_Username']; ?>";
var dataString = 'status=' + status + '&id=' + ticketid + '&username=' + username;
if(status==='' || ticketid==='' || username==='' || dataString==='') {
$('.success').fadeOut(200).hide();
$('.error').fadeOut(200).show();
} else {
dataString = {
'status' = status,
'id' = ticketid
'username' = username
};
$.ajax({
type: "POST",
url: "core/ticketData.php",
data: dataString,
success: function(result){
$('.success').fadeIn(200).show();
$('.error').fadeOut(200).hide();
}
});
}
return false;
});
});
UPDATED
Removed the site, issue has been resolved.
You aren't concatenating correctly, you are missing &s in the data, and that's why it fails:
dataString = 'status=' + status + '&id=' + ticketid + '&username=' + username;
//---------------------------------^-------------------^
Change the way you declare
if(status==='' || ticketid==='' || username==='' || dataString==='') {
$('.success').fadeOut(200).hide();
$('.error').fadeOut(200).show();
} else {
// declare it inside the else
// this is more optimised
dataString = {
'status' = status,
'id' = ticketid
'username' = username
};
$.ajax({
type: "POST",
url: "core/ticketData.php",
data: dataString,
success: function(result){
$('.success').fadeIn(200).show();
$('.error').fadeOut(200).hide();
}
});
}your `dataString`
You can implement this in the following way with data representation in json format:
$(function() {
$(".open").click(function() {
var status = "Open";
var ticketid = "<?php echo $_GET['id']; ?>";
var username = "<?php echo $_SESSION['MM_Username']; ?>";
if(status==='' || ticketid==='' || username==='' || dataString==='') {
$('.success').fadeOut(200).hide();
$('.error').fadeOut(200).show();
} else {
$.ajax({
type: "POST",
url: "core/ticketData.php",
data: {'status': status, 'id':ticketid, 'username':username},
success: function(result){
$('.success').fadeIn(200).show();
$('.error').fadeOut(200).hide();
}
});
}
return false;
});
});

Print json from mysql

Hi all I have a site developed in codeigniter.
In some function I have to retrieve data from mysql and print the result in javascript because is an ajax call.
Thi is my php function to retrieve data:
public function getCityByNameOnly($name) {
$query = $this->db->query('SELECT * FROM city WHERE name LIKE "%'.$name.'%" ');
$city = array();
foreach ($query->result() as $row)
array_push($city, $row);
return json_encode($city);
}
}
And this is my ajax call:
$('#ricerca-ajax').keyup(function(){
var val = $(this).val();
if (val.length>2){
var site_url_city ="<?php echo(site_url('/city/get_city_by_text')); ?>";
$.ajax({
url: site_url_city,
type: "POST",
data: {search: val},
dataType: "text",
success: function(data) {
console.log(data);
for(var i=0;i<data.length;i++)
{
$('#msgid').append(data[i].name_en + '<br> ');
}
}
});
}
})
I append the result into a div but is always undefined.
This is the console.log of my created json:
{"id":"125","name_it":"Lèsina (Hvar)","name_en":"Hvar","nation_id":"23","region_id":"0","active":"1"},{"id":"127","name_it":"Trogir (Traù)","name_en":"Trogir","nation_id":"23","region_id":"0","active":"1"},{"id":"1088","name_it":"Città del Capo","name_en":"Cape Town","nation_id":"101","region_id":"0","active":"1"}]
How to print this json into my success function in javascript?
Thanks
change the datatype:"json" in the ajax will solve the issue
$('#ricerca-ajax').keyup(function(){
var val = $(this).val();
if (val.length>2){
var site_url_city ="<?php echo(site_url('/city/get_city_by_text')); ?>";
$.ajax({
url: site_url_city,
type: "POST",
data: {search: val},
dataType: "json",
success: function(data) {
console.log(data);
for(var i=0;i<data.length;i++)
{
$('#msgid').append(data[i].name + '<br> ');
}
}
});
}
})

Using AJAX form submit to submit and retrieve data from MySQL

Basically, I'm trying to make it so that when a post is submitted to my site, it sends the post using AJAX so that they don't change page, and then if the AJAX post is successful, retrieve all the posts for said user from MySQL and write them onto the page.
My problem is that the browsers (Chrome, IE) are completely ignoring the AJAX request.
My form:
<div id="updatestatus">
<form action="" method="post" id="ps">
<textarea name="status" id="status"></textarea>
<input type="hidden" name="uid" id="uid" value="<?php echo $uid; ?>" />
<input type="submit" id="poststatus" name="poststatus" value="Share" />
</form>
</div>
My AJAX request:
$(function() {
$("#poststatus").click(function() {
var status = $("textarea#status").val();
if (status == "") {
return false;
}
var uid = $("input#uid").val();
var dataString = 'status='+ status + '&uid=' + uid;
$.ajax({
type: "POST",
url: "updatestatus.php",
data: dataString,
success: function() {
$.ajax({
url: 'ajax/query.php',
data: "uid=<?php echo $uid; ?>",
dataType: 'json',
success: function(data) {
var status = data[0];
var sid = data[1];
$('#mainprofile').html("<div id='statuses'><p>"+status+"</p></div>);
return false;
}
});
return false;
});
});
});
});
My ajax/query.php request
<?php
//connect stuff
$uid = strip_tags(stripslashes(htmlspecialchars(htmlentities(mysql_real_escape_string($_GET['uid'])))));
$result = mysql_query("SELECT * FROM mingle_status WHERE uid = '$uid' ORDER BY timestamp DESC"); //query
$array = mysql_fetch_row($result); //fetch result
echo json_encode($array);
?>
Thanks in advance for any help - Joe
In this section of JS code
$.ajax({
type: "POST",
url: "updatestatus.php",
data: dataString,
success: function() {
$.ajax({
url: 'ajax/query.php',
data: "uid=<?php echo $uid; ?>",
dataType: 'json',
success: function(data) {
var status = data[0];
var sid = data[1];
$('#mainprofile').html("<div id='statuses'><p>"+status+"</p></div>);
return false;
}
});
return false;
});
});
You need to remove the ending bracket after the curly brace which follows the last return false, such as...
$.ajax({
type: "POST",
url: "updatestatus.php",
data: dataString,
success: function() {
$.ajax({
url: 'ajax/query.php',
data: "uid=<?php echo $uid; ?>",
dataType: 'json',
success: function(data) {
var status = data[0];
var sid = data[1];
$('#mainprofile').html("<div id='statuses'><p>"+status+"</p></div>");
return false;
}
});
return false;
};
});
Try this
$(function() {
$("#poststatus").click(function() {
var status = $.trim($("#status").val());
if (status == "") {
return false;
}
var uid = $("#uid").val();
var dataString = 'status='+ status + '&uid=' + uid;
$.ajax({
type: "POST",
url: "updatestatus.php",
data: dataString,
success: function() {
$.ajax({
url: 'ajax/query.php',
data: "uid="+<?php echo $uid; ?>,
dataType: 'json',
success: function(data) {
var status = data[0];
var sid = data[1];
$('#mainprofile').html("<div id='statuses'><p>"+status+"</p></div>);
return false;
}
});
return false;
}
});
});
});

Categories