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;
});
});
Related
I am taking two different values from select options and sending it using AJAX, but PHP is not responding to the request.
This is my jQuery code:
$(document).ready(function() {
$("#updateStatus").change(function() {
var opt = $("#updateStatus").val();
$("#updateStatus1").change(function() {
var sta = $("#updateStatus1").val();
$.ajax({
url: 'updatecode.php',
type: 'POST',
data: "option=" + opt + "&status=" + sta,
dataType: 'json',
success: function(data) {
alert(data + "hello");
}
});
});
});
});
This is my PHP code:
$id = $_POST['opt'];
$status = $_POST['sta'];
$query = "UPDATE projectstable SET projectStatus='".$status."'WHERE id='".$id."'";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
if (!$row) {
echo json_encode("fail");
} else {
echo json_encode("sucess");
}
Replace your existing ajax function like this
$(document).ready(function() {
var opt = '';
var sta = '';
$("#updateStatus").change(function() {
opt = $("#updateStatus").val();
});
$("#updateStatus1").change(function() {
sta = $("#updateStatus1").val();
callAjax(opt,sta);
});
});
function callAjax(opt,sta) {
$.ajax({
url: 'updatecode.php',
type: 'POST',
data: "opt=" + opt + "&sta=" + sta,
dataType: 'json',
success: function(data) {
alert(data + "hello");
}
});
}
You are passing the json body as a query string, and you are declaring to use dataType: "json"
$.ajax({
url: 'updatecode.php',
type: 'POST',
data: "option=" + opt + "&status=" + sta,
dataType: 'json',
success: function(data) {
alert(data + "hello");
}
});
instead that you should do it like this:
$.ajax({
url: 'updatecode.php',
type: 'POST',
data: {"option": opt, "status": sta},
dataType: 'json',
success: function(data) {
alert(data + "hello");
}
});
You are sending request under data using "option=" and "&status=" but you are reading these values in PHP through $_POST['opt'] and $_POST['stat'] which should actually be $_POST['option'] and $_POST['status'] respectively.
Also you need to change the data request to JSON format like data: {option:opt,status:sta} for your dataType is JSON
Your code should be like this
$(document).ready(function () {
$("#updateStatus,#updateStatus1").change(function () {
var opt = $("#updateStatus").val();
var sta = $("#updateStatus1").val();
$.ajax({
url: 'updatecode.php',
type: 'POST',
data: {option:opt,status:sta},
dataType: 'json',
success: function (data) {
alert(data + "hello");
}
});
});
})
and php code should like this
$id = $_POST['option'];
$status = $_POST['status'];
$query = "UPDATE projectstable SET projectStatus='".$status."'WHERE id='".$id."'";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
if (!$row) {
echo json_encode("fail");
} else {
echo json_encode("sucess");
}
Thanks every one looks like i just had to change some code in ajax this is my new code which is working fine
This is my PHP code I got rid of if(!$row)
mysql_select_db("dts_db",$con);
$id=$_POST['opt'];
$status=$_POST['sta'];
$query="UPDATE projectstable SET projectStatus='".$status."'WHERE id='".$id."'";
$result=mysql_query($query);if(!$result){ die("My sql query result ".mysql_error());}
else {
echo json_encode("success");
}
and this is my ajax
$.ajax({
url: 'updatecode.php',
type: 'POST',
data: {opt:opt,sta:sta},
dataType: 'json',
success: function(data) {
alert(data + "hello");
}
});
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 have ajax script that print comment
I would like to add loader while the server query works
what do I need to add to the "success" in order to see LOADER in my html page?
function printComments (obj) {
var element = $(obj);
var contactID = element.attr("contactID");
var type = element.attr("id");
var info = 'contactID=' + contactID + "&type=" + type + "&catID=" + catID;
$("#loader").html('<img src="images/loading.gif" align="absmiddle">');
// alert(info);
$.ajax({
type: "POST",
url: "ajax/followPrint.php",
data: info,
success: function(msg){
if (type == "followsTab")
$("#follows").html(msg);
if (type == "commentsTab")
$("#commentsContent").html(msg);
}
});
return false;
}
You can just play with hide() and show() like,
function printComments (obj) {
var element = $(obj);
var contactID = element.attr("contactID");
var type = element.attr("id");
var info = 'contactID=' + contactID + "&type=" + type + "&catID=" + catID;
$("#loader").show(); // displaying loader here
$.ajax({
type: "POST",
url: "ajax/followPrint.php",
data: info,
success: function(msg){
if (type == "followsTab")
$("#follows").html(msg);
if (type == "commentsTab")
$("#commentsContent").html(msg);
$("#loader").hide(); // hiding loader here
}
});
//return false;
}
example
HTML
<a href="#" id="verification" >test</a>
<img src="example_preloader.gif" id="ajaxPreloader" style="display:none" />
JS
$('#verification').click(function() {
$('#ajaxPreloader').toggle();
$.ajax({
type : "POST",
url : "example url",
data : {'exampleData': ''},
success : function(msg) {
$('#ajaxPreloader').text('');
location.reload();
},
error: function(error) {
$('#ajaxPreloader').text('');
}
});
});
Use beforeSend to show the loader and then in succes function hide that loader
$.ajax({
type: "POST",
url: "ajax/followPrint.php",
data: info,
beforeSend:function(){
$("#loader").show();
},
success: function(msg){
$("#loader").hide();
if (type == "followsTab")
$("#follows").html(msg);
if (type == "commentsTab")
$("#commentsContent").html(msg);
}
});
Try Below code:
$.ajax({
type: "POST",
url: "ajax/followPrint.php",
data: info,
**beforeSend: function() {
$("#loader").html("<img src="images/loading.gif" align="absmiddle">");
},**
success: function(msg){
$('#loader').hide();
if (type == "followsTab")
$("#follows").html(msg);
if (type == "commentsTab")
$("#commentsContent").html(msg);
}
});
added this line of code extra:
beforeSend: function() {
$("#loader").html("<img src="images/loading.gif" align="absmiddle">");
},
function printComments (obj) {
var element = $(obj);
var contactID = element.attr("contactID");
var type = element.attr("id");
var info = 'contactID=' + contactID + "&type=" + type + "&catID=" + catID;
$("#follows").html('<img src="images/loading.gif" align="absmiddle">');
$("#commentsContent").html('<img src="images/loading.gif" align="absmiddle">');
//loader will hide replaced by output while the server query works
$.ajax({
type: "POST",
url: "ajax/followPrint.php",
data: info,
success: function(msg){
if (type == "followsTab")
$("#follows").html(msg);
if (type == "commentsTab")
$("#commentsContent").html(msg);
}
});
return false;
}
I am getting undefined from an ajax call while the variables I pass through exist for sure.
This is my ajax function:
function setMessages(roomId, username, message){
$.ajax({
type: "get",
url: "http://www.sinansamet.nl/chatdistract/ajax/setMessages.php",
data: { roomId:roomId, username:username, message:message },
success: function(html) {
strReturn = html;
}
});
}
My PHP:
<?php
$roomdId = $_GET['roomId'];
$username = $_GET['username'];
$message = $_GET['message'];
echo $username;
?>
Calling ajax:
$(document).on("click", "#messageSubmit", function(){
var username = window.localStorage["username"];
var message = $("#message").val();
alert(setMessages(roomId, username, message));
alert(roomId + username + message);
});
The first alert returns undefined. No matter which variable I use. The second alert returns all 3 variables like they are supposed to be. So they do exist for sure. I am using phonegap for this but it does the same on desktop browser.
The function does not return a value, and even if it did, ajax is async, so you'll have to wait until the data is returned before you can use it :
function setMessages(roomId, username, message){
return $.ajax({
type: "get",
url: "http://www.sinansamet.nl/chatdistract/ajax/setMessages.php",
data: { roomId:roomId, username:username, message:message }
});
}
$(document).on("click", "#messageSubmit", function(){
var username = window.localStorage["username"],
message = $("#message").val();
setMessages(roomId, username, message).done(function(data) {
alert(data);
});
});
try:
function setMessages(roomId, username, message){
var strReturn = '';
$.ajax({
type: "get",
url: "http://www.sinansamet.nl/chatdistract/ajax/setMessages.php",
data: { roomId:roomId, username:username, message:message },
success: function(html) {
strReturn = html;
},
async: false
});
return strReturn;
}
Try the following
function setMessages(roomId, username, message){
return $.ajax({
type: "get",
url: "http://www.sinansamet.nl/chatdistract/ajax/setMessages.php",
data: { roomId:roomId, username:username, message:message },
success: function(html) {
strReturn = html;
}
});
}
<?php
$roomdId = $_GET['roomId'];
$username = $_GET['username'];
$message = $_GET['message'];
echo $username;
?>
$(document).on("click", "#messageSubmit", function(){
var username = window.localStorage["username"];
var message = $("#message").val();
setMessages(roomId, username, message).done(function (){
alert(strReturn);
alert(roomId + username + message);
});
});
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;
}
});
});
});