play sound clip when php/mysql if statement is true? - php

hi i have a message alert box that appears when a user gets a new message, what i want to do is add sound to this box when it pops up, i am using a php if statement to check when a user gets a new message and i have tried adding sound by doing the following but its not working please can someone show me how to do this. thanks.
<?php
$check_new_chats = check_new_chats();
while ($chat = mysql_fetch_array($check_new_chats))
if (isset($_SESSION['user_id'])) {
if ($chat['to_user_id'] == $_SESSION['user_id']){
echo( "<embed name='sound_file' src='/assets/music/sound_file.mp3' loop='true' hidden='true' autostart='true'/>"); ?>

<?php
$check_new_chats = check_new_chats();
while ($chat = mysql_fetch_array($check_new_chats))
if (isset($_SESSION['user_id'])) {
if($chat['to_user_id'] == $_SESSION['user_id']){
echo '<script type="text/javascript">play_sound();</script>';
}
}
?>
Here's the Javascript function:
<script type="text/javascript">
function play_sound() {
var audioElement = document.createElement('audio');
audioElement.setAttribute('src', '/assets/music/sound_file.mp3');
audioElement.setAttribute('autoplay', 'autoplay');
audioElement.load();
audioElement.play();
}
</script>

According to the question:
hi i have a message alert box that appears when a user gets a new
message, what i want to do is add sound to this box when it pops up
You already have some sort of javascript function that displays an alert box when needed. Use the info from this answer to play a sound with it.
<audio id="soundHandle" style="display: none;"></audio>
<script>
soundHandle = document.getElementById('soundHandle');
soundHandle.src = '/assets/music/sound_file.mp3';
</script>
//With your message alert function....
alert("Message box");
soundHandle.play();

Related

ajax php validation php code not working

<?php
if(isset($_POST["username"]) && $_POST["username"] != "")
{
$username= $_POST['username'];
if (strlen($username) < 4) {
echo '4 - 15 characters please';
}
if (is_numeric($username[0])) {
echo 'First character must be a letter';
}
}
?>
php code not working: please help me validation using java script or ajax
e<script type="text/javascript" language="javascript">
function callme()
{
var showme = document.getElementById("show");
var user = document.getElementById("uname").value;
//for check new browser show ajax from
if(user!=="")
{
showme.innerHTML=' loading.....';
var hr = new XMLHttpRequest()
{
hr.open("post","index.php",true);
hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
hr.readystatechange=function()
{
if(hr.readystate== 4 && hr.status==200) {
showme.innerHTML= hr.responseText;
}
}
var v="username="+ user;
hr.send(v);
}
}
}
</script>
<body>
<span>username:</span>
<input type="text" name="uname" id="uname" onBlur="callme();"/>
<div id="show"></div>
</body>
all code working fine only php code not working please help me
when we enter some text in textbox only loading.....
any type of validation are not showing ...
I think your problem is here hr.readystatechange. What you need is hr.onreadystatechange
Maybe this is not the solution but you can echo or print_r or in a file what you receive from the browser in php and see if $_POST['username'] is coming with this name and no with uname.
print_r($_POST);
Your code is very old, now you can use jquery to make things easy with javascript and for php there are lots of frameworks out there that can do your php easy. Tags like onclick into html are deprecated or very near to be.
Is better if you expect an string
$_POST["username"] !== ""
Try to store the message in one variable and at the end of the php function, return it, is better to have only one exit than multiple along the function.
Next time try to put the question and the code in order, for readability and understanding.
Thanks, and sorry for my english... XD

How to make my simple alert window work

i'm currentrly coding in php and when i click a button the message should me Would you like to delete this entry?
This is where I'm currently at
<button onClick="alert('Sure to delete entry?')">Remove Entry</button>
And sure it works. But when I press "x" to close the alert window it still deletes the entry.
How can you make a popup window that has the "Cancel" opition?
I would really appreciate the help! :)
if (confirm("Your question")) {
// do things if OK
}
Use confirm
Click here for examples
You can do this using the following JavaScript code:
if (confirm('Sure to delete entry?') == true) {
//write your code here to delete
}
The HTML
<a href="delete.php?delete=<?php echo $row['guestbook_id'];?>"
onclick="return areYouSure()">Remove Entry</a>
And the JS
function areYouSure()
{
var con = window.confirm("Are You Sure?");
if(con)
{
return true;
}
else
{
return false;
}
}

hide and show div when page loads based on result generated from php

I am doing API call to box.net.
I wanted to show data from the response into a table.
But sometimes response is empty.
that time i want to show user some message.
So my idea is to show div containing table when i get some response and disable div containing message. and vice versa.
I an new to jquery. Didn't find suitable examples.
I tried something from those examples,but it was not successful.
here is
if (sizeof($folder_entries) == 0)
{
echo '<script>';
echo '$("#message").show()';
echo '$("#content").hide()';
echo '</script>';
}
else
{
echo '<script>';
echo '$("#message").hide()';
echo '$("#content").show()';
echo '</script>';
}
How can i get that?
Thanks in Advance.
seems like you need straightforward javascript or jquery to show and hide the div
<script>
if (some condition..... )
{
document.getElementById("test").style.display = '';
}
else
{
document.getElementById("test").style.display = 'none';
}
</script>
<div name="test" id="test">
jQuery:
$(document).ready(function() {
$('#hideh1').click(function(){
$('div.showhide,h1').hide();
});
$('#showh1').click(function(){
$('div.showhide,h1').show();
});
$('#toggleh1').click(function(){
$('div.showhide,h1').toggle();
});
});

show and hide div with jquery when if not true

i have one php function which verify admin login like that
if($vAdmin==1)
{
$_SESSION['admin']="admin";
header("location:index.php");
}
else
{
$errMsg="Invalid Username or Password.";
}
This work perfect but i want to show the div which show the error message, so i defined the div like this and its hidden div in style.
<div class="notification">Invalid username or password. (Type anything)</div>
i have changed the above function to
if($vAdmin==1)
{
$_SESSION['admin']="admin";
header("location:index.php");
}
else
{?>
$('div.notification').fadeIn();
setTimeout(function() {
$('div.notification').fadeOut('slow');
}, 3000);
<?}
?>
my basic purpose is when its not verified just show the div and hide it.
but its not working?
Thanks
Change the else part of your code to the following:
else
{
echo <<<EOD
<script type="text/javascript">
$(document).load(function() {
$('div.notification').fadeIn();
setTimeout(function() {
$('div.notification').fadeOut('slow');
}, 3000);
});
</script>
EOD;
}
I'm also assuming you're including the jQuery library somewhere prior to where this script tag is added. If you're not, make sure you do.
You need to output the javascript within script tags.

display a javascript message before proceeding

I have this code that is to fetch a particular item in the database after the user presses a button. If the item is found I would like to display a confirm message via javascript and I don't know how to show it.
After obtaining the item from the database
if(null!=mysqli_fetch_array($res)))
{
echo ""; // how can I call the javascript code here ?
}
and the following confirm box
<script type="text/javascript">
function onConfirm()
{
return confirm("Display this item ?");
}
</script>
Use jquery and ajax to get the value from the database:
<script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
$.get("ajax.php", function(data){
if(data != "0"){
if(confirm("Display this item ?"))
{
//do something
}
}
});
ajax.php:
//other db code here
if(null!=mysqli_fetch_array($res)))
{
echo "1";
}
else{
echo "0";
}
There are number of ways you can solve this. A good way is to:
Send an ajax request to a page
The page does the mysql stuff and returns(echo in case of PHP) the message
On the callback function use simple way such as alert() to show the message.

Categories