Play a sound when new comment is detected on AJAX refreshWhich one of these PHP and Mysql scripts is safer security wise?PHP alternating colorsHow do I show a user's credit based on their sessionphp time messed upRails detect if request was AJAXAJAX and playing a sound oncePHP code execution sequenceSql error in my php sourceManipulating retrieved data that is being echoed by an external PHP scriptPlaying sound when new message comes in

Will 700 more planes a day fly because of the Heathrow expansion?

What does 'made on' mean here?

Why are UK Bank Holidays on Mondays?

Would glacier 'trees' be plausible?

Did we get closer to another plane than we were supposed to, or was the pilot just protecting our delicate sensibilities?

Where are the "shires" in the UK?

Should I decline this job offer that requires relocating to an area with high cost of living?

Word for Food that's Gone 'Bad', but is Still Edible?

Nominativ or Akkusativ

Understanding trademark infringements in a world where many dictionary words are trademarks?

Frequency of specific viral sequence in .BAM or .fastq

How to write a 12-bar blues melody

exec command in bash loop

How I can I roll a number of non-digital dice to get a random number between 1 and 150?

Decoupling cap routing on a 4 layer PCB

Wrong answer from DSolve when solving a differential equation

Does it make sense for a function to return a rvalue reference

Can you Ready a Bard spell to release it after using Battle Magic?

Can I use a fetch land to shuffle my deck while the opponent has Ashiok, Dream Render in play?

What does this wavy downward arrow preceding a piano chord mean?

Point of the Dothraki's attack in GoT S8E3?

Emotional immaturity of comic-book version of superhero Shazam

Why do people keep telling me that I am a bad photographer?

US born but as a child of foreign diplomat



Play a sound when new comment is detected on AJAX refresh


Which one of these PHP and Mysql scripts is safer security wise?PHP alternating colorsHow do I show a user's credit based on their sessionphp time messed upRails detect if request was AJAXAJAX and playing a sound oncePHP code execution sequenceSql error in my php sourceManipulating retrieved data that is being echoed by an external PHP scriptPlaying sound when new message comes in






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I'm making a live chat system for my site. Any help with solving this issue is greatly appreciated =). I have a page ticket.php with a div #reply-holder where I fetch the comments for that ticket using AJAX:



<script type="text/javascript"> 
function loadlink()
$('#reply-holder').load('ajax.php',function ()
$(this).unwrap();
);


loadlink(); // This will run on page load
setInterval(function()
loadlink() // this will run after every 5 seconds
, 15000);
</script>


My ajax.php page where the comments are fetched from the MYSQL db and display is:



<?php
require_once 'inc/config.php';

session_start();
$ticketid = $_SESSION['ticketid'];
$username = $_SESSION['username'];

$getticketid = "SELECT * FROM orders where id = '".$ticketid."' ORDER BY id DESC LIMIT 1"
or die(mysql_error());

$result = $link->query($getticketid);

$row = $result->fetch_array(MYSQLI_NUM);
$active = $row[2];
$created_at = $row[3];
$message = $row[4];
$price = $row[5];

?>

<?php

$getcomments = "SELECT * FROM comments where orderid = ".$ticketid." ORDER BY id DESC"
or die(mysql_error());

$result2 = $link->query($getcomments);
while($row2 = mysqli_fetch_array($result2))
echo '<div class="reply">';
echo '<p>'.$row2[2].' replied:</p>';
echo '<h3>'.$row2[3].'</h3></div>';
$lastMessageIdWePlayedSoundFor = $row2[0] - 1;


?>


I've tried the following code, but the sound isn't working, no sound is played even if a new comment is inserted.. I first tried it on ajax.php but it would just play the sound after every refresh even if no new comment was inserted.



I'm pretty sure the code is alright, I just don't know exactly where to place it.



<?php 

$playsound = "SELECT COUNT(*) FROM comments where orderid = '".$ticketid."' ORDER BY id DESC LIMIT 1"
or die(mysql_error());

$result2 = $link->query($playsound);

$row2 = $result2->fetch_array(MYSQLI_NUM);
$lastMessageId = $row2[0];

if($lastMessageId!=$lastMessageIdWePlayedSoundFor)

$myAudioFile = "notification.mp3";
echo '<EMBED SRC="'.$myAudioFile.'" HIDDEN="TRUE" AUTOSTART="TRUE"></EMBED>';
$lastMessageIdWePlayedSoundFor = $lastMessageId;


?>









share|improve this question






















  • in function loadlink you should check whather there are new comments or not, then play the sound (by invoking a play method on embed elelement).

    – Jeff
    Mar 22 at 23:10












  • in php $lastMessageIdWePlayedSoundFor doesn't persist through the several calls you do (in setTimeout), because you don't save it in session or something similar. In this/your case php should not manage the UI logic.

    – Jeff
    Mar 22 at 23:12











  • @Jeff If i started a session for the $lastMessageIdWePlayedSoundFor would the current code work? I'm not sure how to edit the loadlink so if I can do it in php i'd prefer it.

    – Ralph
    Mar 22 at 23:16












  • you started a session, right, but you don't store any values into $_SESSION['somevaluename'] = $value;

    – Jeff
    Mar 22 at 23:34

















0















I'm making a live chat system for my site. Any help with solving this issue is greatly appreciated =). I have a page ticket.php with a div #reply-holder where I fetch the comments for that ticket using AJAX:



<script type="text/javascript"> 
function loadlink()
$('#reply-holder').load('ajax.php',function ()
$(this).unwrap();
);


loadlink(); // This will run on page load
setInterval(function()
loadlink() // this will run after every 5 seconds
, 15000);
</script>


My ajax.php page where the comments are fetched from the MYSQL db and display is:



<?php
require_once 'inc/config.php';

session_start();
$ticketid = $_SESSION['ticketid'];
$username = $_SESSION['username'];

$getticketid = "SELECT * FROM orders where id = '".$ticketid."' ORDER BY id DESC LIMIT 1"
or die(mysql_error());

$result = $link->query($getticketid);

$row = $result->fetch_array(MYSQLI_NUM);
$active = $row[2];
$created_at = $row[3];
$message = $row[4];
$price = $row[5];

?>

<?php

$getcomments = "SELECT * FROM comments where orderid = ".$ticketid." ORDER BY id DESC"
or die(mysql_error());

$result2 = $link->query($getcomments);
while($row2 = mysqli_fetch_array($result2))
echo '<div class="reply">';
echo '<p>'.$row2[2].' replied:</p>';
echo '<h3>'.$row2[3].'</h3></div>';
$lastMessageIdWePlayedSoundFor = $row2[0] - 1;


?>


I've tried the following code, but the sound isn't working, no sound is played even if a new comment is inserted.. I first tried it on ajax.php but it would just play the sound after every refresh even if no new comment was inserted.



I'm pretty sure the code is alright, I just don't know exactly where to place it.



<?php 

$playsound = "SELECT COUNT(*) FROM comments where orderid = '".$ticketid."' ORDER BY id DESC LIMIT 1"
or die(mysql_error());

$result2 = $link->query($playsound);

$row2 = $result2->fetch_array(MYSQLI_NUM);
$lastMessageId = $row2[0];

if($lastMessageId!=$lastMessageIdWePlayedSoundFor)

$myAudioFile = "notification.mp3";
echo '<EMBED SRC="'.$myAudioFile.'" HIDDEN="TRUE" AUTOSTART="TRUE"></EMBED>';
$lastMessageIdWePlayedSoundFor = $lastMessageId;


?>









share|improve this question






















  • in function loadlink you should check whather there are new comments or not, then play the sound (by invoking a play method on embed elelement).

    – Jeff
    Mar 22 at 23:10












  • in php $lastMessageIdWePlayedSoundFor doesn't persist through the several calls you do (in setTimeout), because you don't save it in session or something similar. In this/your case php should not manage the UI logic.

    – Jeff
    Mar 22 at 23:12











  • @Jeff If i started a session for the $lastMessageIdWePlayedSoundFor would the current code work? I'm not sure how to edit the loadlink so if I can do it in php i'd prefer it.

    – Ralph
    Mar 22 at 23:16












  • you started a session, right, but you don't store any values into $_SESSION['somevaluename'] = $value;

    – Jeff
    Mar 22 at 23:34













0












0








0








I'm making a live chat system for my site. Any help with solving this issue is greatly appreciated =). I have a page ticket.php with a div #reply-holder where I fetch the comments for that ticket using AJAX:



<script type="text/javascript"> 
function loadlink()
$('#reply-holder').load('ajax.php',function ()
$(this).unwrap();
);


loadlink(); // This will run on page load
setInterval(function()
loadlink() // this will run after every 5 seconds
, 15000);
</script>


My ajax.php page where the comments are fetched from the MYSQL db and display is:



<?php
require_once 'inc/config.php';

session_start();
$ticketid = $_SESSION['ticketid'];
$username = $_SESSION['username'];

$getticketid = "SELECT * FROM orders where id = '".$ticketid."' ORDER BY id DESC LIMIT 1"
or die(mysql_error());

$result = $link->query($getticketid);

$row = $result->fetch_array(MYSQLI_NUM);
$active = $row[2];
$created_at = $row[3];
$message = $row[4];
$price = $row[5];

?>

<?php

$getcomments = "SELECT * FROM comments where orderid = ".$ticketid." ORDER BY id DESC"
or die(mysql_error());

$result2 = $link->query($getcomments);
while($row2 = mysqli_fetch_array($result2))
echo '<div class="reply">';
echo '<p>'.$row2[2].' replied:</p>';
echo '<h3>'.$row2[3].'</h3></div>';
$lastMessageIdWePlayedSoundFor = $row2[0] - 1;


?>


I've tried the following code, but the sound isn't working, no sound is played even if a new comment is inserted.. I first tried it on ajax.php but it would just play the sound after every refresh even if no new comment was inserted.



I'm pretty sure the code is alright, I just don't know exactly where to place it.



<?php 

$playsound = "SELECT COUNT(*) FROM comments where orderid = '".$ticketid."' ORDER BY id DESC LIMIT 1"
or die(mysql_error());

$result2 = $link->query($playsound);

$row2 = $result2->fetch_array(MYSQLI_NUM);
$lastMessageId = $row2[0];

if($lastMessageId!=$lastMessageIdWePlayedSoundFor)

$myAudioFile = "notification.mp3";
echo '<EMBED SRC="'.$myAudioFile.'" HIDDEN="TRUE" AUTOSTART="TRUE"></EMBED>';
$lastMessageIdWePlayedSoundFor = $lastMessageId;


?>









share|improve this question














I'm making a live chat system for my site. Any help with solving this issue is greatly appreciated =). I have a page ticket.php with a div #reply-holder where I fetch the comments for that ticket using AJAX:



<script type="text/javascript"> 
function loadlink()
$('#reply-holder').load('ajax.php',function ()
$(this).unwrap();
);


loadlink(); // This will run on page load
setInterval(function()
loadlink() // this will run after every 5 seconds
, 15000);
</script>


My ajax.php page where the comments are fetched from the MYSQL db and display is:



<?php
require_once 'inc/config.php';

session_start();
$ticketid = $_SESSION['ticketid'];
$username = $_SESSION['username'];

$getticketid = "SELECT * FROM orders where id = '".$ticketid."' ORDER BY id DESC LIMIT 1"
or die(mysql_error());

$result = $link->query($getticketid);

$row = $result->fetch_array(MYSQLI_NUM);
$active = $row[2];
$created_at = $row[3];
$message = $row[4];
$price = $row[5];

?>

<?php

$getcomments = "SELECT * FROM comments where orderid = ".$ticketid." ORDER BY id DESC"
or die(mysql_error());

$result2 = $link->query($getcomments);
while($row2 = mysqli_fetch_array($result2))
echo '<div class="reply">';
echo '<p>'.$row2[2].' replied:</p>';
echo '<h3>'.$row2[3].'</h3></div>';
$lastMessageIdWePlayedSoundFor = $row2[0] - 1;


?>


I've tried the following code, but the sound isn't working, no sound is played even if a new comment is inserted.. I first tried it on ajax.php but it would just play the sound after every refresh even if no new comment was inserted.



I'm pretty sure the code is alright, I just don't know exactly where to place it.



<?php 

$playsound = "SELECT COUNT(*) FROM comments where orderid = '".$ticketid."' ORDER BY id DESC LIMIT 1"
or die(mysql_error());

$result2 = $link->query($playsound);

$row2 = $result2->fetch_array(MYSQLI_NUM);
$lastMessageId = $row2[0];

if($lastMessageId!=$lastMessageIdWePlayedSoundFor)

$myAudioFile = "notification.mp3";
echo '<EMBED SRC="'.$myAudioFile.'" HIDDEN="TRUE" AUTOSTART="TRUE"></EMBED>';
$lastMessageIdWePlayedSoundFor = $lastMessageId;


?>






php ajax






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 22 at 23:05









RalphRalph

227210




227210












  • in function loadlink you should check whather there are new comments or not, then play the sound (by invoking a play method on embed elelement).

    – Jeff
    Mar 22 at 23:10












  • in php $lastMessageIdWePlayedSoundFor doesn't persist through the several calls you do (in setTimeout), because you don't save it in session or something similar. In this/your case php should not manage the UI logic.

    – Jeff
    Mar 22 at 23:12











  • @Jeff If i started a session for the $lastMessageIdWePlayedSoundFor would the current code work? I'm not sure how to edit the loadlink so if I can do it in php i'd prefer it.

    – Ralph
    Mar 22 at 23:16












  • you started a session, right, but you don't store any values into $_SESSION['somevaluename'] = $value;

    – Jeff
    Mar 22 at 23:34

















  • in function loadlink you should check whather there are new comments or not, then play the sound (by invoking a play method on embed elelement).

    – Jeff
    Mar 22 at 23:10












  • in php $lastMessageIdWePlayedSoundFor doesn't persist through the several calls you do (in setTimeout), because you don't save it in session or something similar. In this/your case php should not manage the UI logic.

    – Jeff
    Mar 22 at 23:12











  • @Jeff If i started a session for the $lastMessageIdWePlayedSoundFor would the current code work? I'm not sure how to edit the loadlink so if I can do it in php i'd prefer it.

    – Ralph
    Mar 22 at 23:16












  • you started a session, right, but you don't store any values into $_SESSION['somevaluename'] = $value;

    – Jeff
    Mar 22 at 23:34
















in function loadlink you should check whather there are new comments or not, then play the sound (by invoking a play method on embed elelement).

– Jeff
Mar 22 at 23:10






in function loadlink you should check whather there are new comments or not, then play the sound (by invoking a play method on embed elelement).

– Jeff
Mar 22 at 23:10














in php $lastMessageIdWePlayedSoundFor doesn't persist through the several calls you do (in setTimeout), because you don't save it in session or something similar. In this/your case php should not manage the UI logic.

– Jeff
Mar 22 at 23:12





in php $lastMessageIdWePlayedSoundFor doesn't persist through the several calls you do (in setTimeout), because you don't save it in session or something similar. In this/your case php should not manage the UI logic.

– Jeff
Mar 22 at 23:12













@Jeff If i started a session for the $lastMessageIdWePlayedSoundFor would the current code work? I'm not sure how to edit the loadlink so if I can do it in php i'd prefer it.

– Ralph
Mar 22 at 23:16






@Jeff If i started a session for the $lastMessageIdWePlayedSoundFor would the current code work? I'm not sure how to edit the loadlink so if I can do it in php i'd prefer it.

– Ralph
Mar 22 at 23:16














you started a session, right, but you don't store any values into $_SESSION['somevaluename'] = $value;

– Jeff
Mar 22 at 23:34





you started a session, right, but you don't store any values into $_SESSION['somevaluename'] = $value;

– Jeff
Mar 22 at 23:34












0






active

oldest

votes












Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55308868%2fplay-a-sound-when-new-comment-is-detected-on-ajax-refresh%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55308868%2fplay-a-sound-when-new-comment-is-detected-on-ajax-refresh%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript