I'm making a music player using php and javascript. I list the files like this:
<?php
if (isset($_GET["action"])) {
$action = htmlspecialchars($_GET["action"]);
if ($action == "listen") {
function listFolderFiles($dir) {
$ffs = scandir($dir);
echo '<ol>';
foreach($ffs as $ff){
if($ff != '.' && $ff != '..'){
echo '<li>'. $ff . '';
if (is_dir($dir . '/' . $ff)) listFolderFiles($dir . '/' . $ff);
echo '</li>';
}
}
echo '</ol>';
}
listFolderFiles("music");
}
} else {
echo '> listen';
}
?>
And I change the song like this:
<script>
function changesong(url) {
$("#audioplayer").attr("src", url);
$("#audioplayer").trigger('play');
}
</script>
The problem is that songs with quotes in them won't play (for example Don't Stop Me Now). Is there an easy way to fix this?
You should use addslashes(), like this:
echo '<li>'. $ff . '';
You can escape quotes for javascript function in HTML code like this:
Note that you probably need to escape double quotes as well, since they can interfere with HTML tag.
$link = $dir . '/' . $ff;
$link = str_replace("'", "'", $link);
$link = str_replace('"', """, $link);
echo '<li>'. $ff . '';
Related
i want to run javascript in PHP but this code isn't not working at all for me.
if(!mysqli_fetch_assoc($result))
{
echo '<script type="text/javascript">',
'document.getElementById("console1").innerHTML += "Query did not work";',
'</script>' ;
}
Replace those , with . to chain a string.
if(!mysqli_fetch_assoc($result))
{
echo '<script type="text/javascript">' .
'document.getElementById("console1").innerHTML += "Query did not work";' .
'</script>';
}
You can't use " in php, this worked for me!
echo "<script>
document.getElementById('console1').innerHTML += 'Query did not work';
</script>";
You can Try the Heredoc Notation:
<?php
echo<<<HTML
.
.
<script type="text/javascript">
document.getElementById("console1").innerHTML += "Query did not work";
</script>
HTML;
//[*Note that echo<<<HTML and HTML; has to be on the very left side without blank spaces]
since it is done with ajax try something similar to this:
<?php
if(!mysqli_fetch_assoc($result)) {
http_response_code(500);
die(['error' => 'Query did not work!']);
}
?>
and in your frontend code something like:
<script>
$.get('your/query/path?query=...', function() {
console.log('executed');
}).fail(function(result) {
$('#console1').append(result.error);
});
</script>
I'm using PHP to call a Js function with values generated by PHP.
$fp = fopen($_FILES['file']['tmp_name'], 'rb');
while(($line = fgets($fp)) !== false)
{
$split = explode(":", $line);
echo '
<script type="text/javascript">
var a = updateHashes("' . $split[0] . '", "' . $split[1] . '");
console.log(a);
</script>';
}
But my code adds some line breaks to the code, which cause errors as you can see in the following screenshot:
What could I do to fix this?
You can try to use the php trim() function on each line, that should solve.
$split = explode(":", trim($line));
Add trim()
echo '
<script type="text/javascript">
var a = updateHashes("' . trim($split[0]) . '", "' . trim($split[1]) . '");
console.log(a);
</script>';
in the below php statement I want to assign ID to EmployeeName.I will use the ID tag to search an element in the echoed list by name.Where am I making the mistake ?
<?php while( $toprow4 = sqlsrv_fetch_array( $stmt4) ) {
echo "<div class='parent-div'><span class='rank'>" . $toprow4['rank'] . "</span><span class='name'>" id = ' . $toprow4['EmployeeName'] .' "</span><span class='points'>" . $toprow4['pointsRewarded'] . "</span></div>";
} ?>
Add the id to the div element as an attribute and not as textContent
echo "<div class='parent-div' id='" . $toprow4['EmployeeName'] . "'><span class='rank'>" . $toprow4['rank'] . "</span><span class='name'>" . $toprow4['EmployeeName'] . "</span><span class='points'>" . $toprow4['pointsRewarded'] . "</span></div>";
and try and make that name a valid ID by removing spaces or replace them by -.
You need to check your quotes. If you aren't already, I'd strongly recommend using an editor with code highlighting.
<?php
while ($toprow4 = sqlsrv_fetch_array( $stmt4)) {
$rank = $toprow4['rank'];
$id = $toprow4['EmployeeName'];
$points = $toprow4['pointsRewarded'];
echo "<div class='parent-div'><span class='rank'>$rank</span>";
echo "<span class='name' id='$id'></span><span class='points'>$points<span></div>";
}
?>
Seems like you have a big problem escaping your output string correctly. Perhaps you should to try write it clean by using templates and sprintf() like in this sample.
i did not test the logic of the while statement you have given
<?php
$template = '<div class="parent-div">';
$template .= '<span class="rank">%s</span>';
$template .= '<span class="name">id = %s</span>';
$template .= '<span class="points">%s</span>';
$template .= '</div>';
while($toprow = sqlsrv_fetch_array($stmt4) {
echo sprintf(
$template,
$toprow['rank'],
$toprow['EmployeeName'],
$toprow['pointsRewarded']
)
}
?>
Your code is not clean, so you failed on making correct quotes and single-quotes.
I get the error message syntax error missing ; before statement at "var galleryarray=new Array();" . "\n"; here is the php code
function returnimages($dirname=".") {
$pattern="\.(jpg|jpeg|png|gif|bmp)$";
$files = array();$curimage=0;
if($handle = opendir($dirname)) {
while(false !== ($file = readdir($handle))){
if(eregi($pattern, $file)){
echo 'galleryarray[' . $curimage .']=["' . $file . '"];' . "\n";
$curimage++;
}
}
closedir($handle);
}
return($files);
}
echo "var galleryarray=new Array();" . "\n";
returnimages();
and here is the javascript:
var galleryarray=new Array();
var curimg=0
function rotateimages(){
document.getElementById("slideshow").setAttribute("src", "slideshow_images/"+galleryarray[curimg])
curimg=(curimg<galleryarray.length-1)? curimg+1 : 0
}
window.onload=function(){
setInterval("rotateimages()", 2500)
}
i just don't see my mistake any help with this problem would be appreciated
klein
Replace your below line:
echo "var galleryarray=new Array();" . "\n";
with the following line:
echo "<script>var galleryarray=new Array();</script>";
You are adding a JS code without script tag so you need to add this tag.
EDITED:
echo 'galleryarray[' . $curimage .']=["' . $file . '"];' . "\n";
You also have error in the above line replace it with the below line:
echo '<script>galleryarray["' . $curimage .'"]=["' . $file . '"];</script>';
I want to know how to write a pice of PHP code into JavaScript/Ajax.
This is my PHP code:
if ($folder = opendir('data/Tasklist/')) {
while (false !== ($file = readdir($folder))) {
if ($file != '.' && $file != ".."){
$data=file_get_contents("data/Tasklist/".$file);
$poc=explode(";",$data);
echo '<li class="taskli">
<button id="'. $file . '" class="Del"> Delete </button>
'. $poc[0] . " " . $poc[1] . '<div class="hidinfo">' . $poc[2] . '</div></li>';
}
}
closedir($handle);
}
And i want to write : id="'. $file . '" inside this code:
$.post( "data/remove.php",{HERE})
Since you're storing the $file variable in the <button> id, you can grab it from there:
$('.Del').click(function(){
var file = $(this).attr('id');
$.post( "data/remove.php",{id:file});
return false;
});