I am Trying to escape these darn quotes and stuff in JS. I'm trying to use json_encode. My head hurts from looking at quotes, help???
$list = '';
// some loop here
$message = 'centeredPopup(this.href,"myWindow","500","300","yes")';
$jscode = 'json_encode('.$message.');return false';
$list .= '<p>
<a href="http://www.example.com/something.php?id=' . $id . '" onclick="'
.htmlspecialchars($jscode) . '" >' . $name . '</a></p><br>';
If you want to call json_encode there, you need to do this.
$jscode = json_encode($message).';return false';
You don't even need json_encode for this. You can put this there:
$message = 'centeredPopup(this.href,"myWindow","500","300","yes")';
$jscode = $message.';return false';
$list .= '<p> <a href="http://www.example.com/something.php?id=' . $id . '" onclick="' .htmlspecialchars($jscode) . '" >' . $name . '</a></p><br>';
Try:
$message = 'centeredPopup(this.href,"myWindow","500","300","yes")';
$jscode = json_encode($message);
Otherwise everything else should be OK.
Related
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'm making a program that shows several descriptions taken from a database (MySQL):
echo "<input type=\"submit\" id=\"Boton" . $i . "\" value=\"Mostrar Descripcion\""
. " onclick=cambiarBoton(" . $i . ", \"" . $row["descripcion"] . "\") />";
echo "<div class=\"entrada-descripcion\" id=\"Descripcion" . $i . "\"> </div>";
where $row["descripcion"] is the access to the description and $i is an identifier because i'm using loops. That code generates a button that i must press to show the description. The javascript function "cambiarBoton" is the one that do that changes:
function cambiarBoton(i, d){
if (document.getElementById("Boton"+i).value == "Mostrar Descripcion"){
document.getElementById("Boton"+i).value = "Ocultar Descripcion";
document.getElementById("Descripcion"+i).innerHTML = d;
}else if(document.getElementById("Boton"+i).value == "Ocultar Descripcion"){
document.getElementById("Boton"+i).value = "Mostrar Descripcion";
document.getElementById("Descripcion"+i).innerHTML = "";
}
}
Ok, but there is a problem in the program and this is that i can't pass the description to the function in javascript and this doesn't works. How can i do this?
(I must do this without using AJAX)
I answer to the question I think you're asking is:
<?php echo "<script>var data = ".$row["description"].";</script>"; ?>
The problem was in the call of the function "cambiarBoton" in the code PHP,
it should have been this:
echo "<input type=\"submit\" id=\"Boton" . $i . "\" value=\"Mostrar Descripcion\""
. " onclick=\"cambiarBoton(" . $i . ", '" . $row["descripcion"] . "')\" />";
And then it works.
I have tried creating a list within a container in PHP and I am struggling because it's my first time doing a list in PHP. I want the list within the container to look like an example I found on http://getbootstrap.com/components/#list-group this example is under "List Group" then it's the "Linked Items" example.
My code:
// List
echo_lines(array(
// All cases
"<ul>",
"<div class='row'>",
"<div class='col-md-6'>",
"<div class='panel panel-default'>",
"<div class='panel-heading clearfix'>",
"<h4 style='margin:0; margin-top:5px; padding:0;'>All Projects</h4>",
"</div>",
"<div class='panel-body'>",
// Content
"<div class='list-group'>"
"<a href='#' class='list-group-item'>"Orange"</a>"
"<a href='#' class='list-group-item'>"Pizza"</a>"
"<a href='#' class='list-group-item'>"Beef"</a>"
"<a href='#' class='list-group-item'>"Chicken"</a>"
"</ul>",
The code you gave us is way to messy. You don't close most of the html tags, and I'm not sure what you're trying to achieve
Here is the example from bootstrap documentation that you linked, more or less mixed with your code.
I'm still not sure if "Orange", "Beef", "Pizza" and "Chicken" are just string or you want to use variables (since you escaped them on your code)
$str = "";
$str .= '<div class="list-group">';
$str .= ' Orange';
$str .= ' Beef';
$str .= ' Pizza';
$str .= ' Chicken';
$str .= ' ' . $variable . ''; // this is an example, if you want to use variable
$str .= '</div>';
echo $str;
If you want to use " or ' into a string, you have to escape it with a backslash \ depending on how you write your string.
Example :
$str = "";
$str .= 'Here you can use "double quotes" without escaping<br />';
$str .= "There you will need to escape \"double quotes\"<br />";
$str .= 'There you will need to escape \'simple quotes\'<br />';
$str .= "There, escaping 'simple quotes' is unnecessary";
More info at PHP: Strings manual
In PHP you must concatenate variables or constants to your string with the . operator, and you forgot to keep > within the quotation ("):
"<a href='#' class='list-group-item'>". Orange. "</a>"
"<a href='#' class='list-group-item'>". Pizza. "</a>"
"<a href='#' class='list-group-item'>". Beef. "</a>"
"<a href='#' class='list-group-item'>". Chicken. "</a>"
Also, why are you sometimes using commas and sometimes not?
Use php array :
<?php
$arr_foods = array('Orange', 'Pizza','Beef', 'Chicken');
$str_list_group = '';
foreach($arr_foods as $str_food)
{
//Concatenation
$str_list_group =. "<a href='#' class='list-group-item'>". $str_food. "</a>\n";
}
print $str_list_group;
?>
I would like to know how to pass multiple arguments to the the function with onmouseover event handler in HTML.
Here is the following code in PHP ,
<a href='Page2.php?user=" . $userid . "' target='_blank' onMouseOver=\"writetxt('" . $info "')\" onMouseOut=\"writetxt(0)\">"
I would like to pass 2 arguments to writetxt() function something like this :
writetxt($arg1, $arg2)
<a href='Page2.php?user=" . $userid . "' target='_blank' onMouseOver=\"writetxt('" . $info . "," . $img . "')\" onMouseOut=\"writetxt(0)\">"
Thanks
It should be like...
writetxt('" . $info . "', '" . $img . "');
^ ^
use like this
echo "<a href=\"Page2.php?user=" . $userid ."\" target=\"_blank\" onMouseOver=\"writetxt('" . $info . "','" . $img . "')\" onMouseOut=\"writetxt(0)\">" ;