[英]echoing javascript inside of php, can't get quotes right
I want to have some javascript execute based on some php if statement, but I can't get the quotes around my link correct .. In console it shows that a ) is missing from the following line. 我想让一些基于php if语句的javascript执行,但是我无法正确获取链接的引号。在控制台中,它显示出以下行缺少a)。 I'm sure I messed up the quotes, it was correct when I just had it javascript, but now mixing with echo I can't get it. 我确定我搞砸了引号,当我刚使用javascript时,它是正确的,但是现在与echo混合了,我听不到了。 I'm using single quotes around the whole thing and doubles inside. 我在整个事情上使用单引号,并在里面加倍。 The rest is working, please take a look and help me fix this line. 其余的都在工作,请看看并帮助我解决此问题。 Thanks! 谢谢!
$(this).append("<a href=""index.php?patient=test&a="".""appointment_nums[count]""."">Schedule Appointment </a>");
if ($mode== 'view' && $action==''){
echo '<script >
$(document).ready(function(e){
$("body").click(function(event) {
redirect = $(event.target).context.getAttribute("href");
});
checkColumns();
});
function checkColumns(){
count=0;
appointment_nums = [];
$(".mgrid_table > tbody > tr").each(function() {
appointment_nums.push($(this).find("td").eq(3).find("label").html());
appointment_nums = appointment_nums.filter(function(n){ return n != undefined });
});
appointments = appointment_nums.length;
appendColumns();
}
function appendColumns(){
function ajax() {
return $.ajax({
type:"post",
url: "../testrequest.php",
data : {appointment_nums:appointment_nums},
dataType:"json",
});
};
ajax().done(function(result){
$("table:nth-of-type(2) > tbody > tr > td:nth-of-type(2)").each(function() {
if($(this).children().length < 1){
if (result[count] == false){
$(this).append("<a href=""index.php?patient=test&a="".""appointment_nums[count]""."">Schedule Appointment </a>");
}else{
$(this).append("<span>Waiting For Doctor to Schedule</span>");
}
}
count = count + 1 ;
});
});
}
</script>';
Either vary the quote types, or start escaping. 要么更改报价类型,要么开始转义。 eg with this raw (and broken) snippet: 例如,使用此原始(和损坏的)代码段:
.append("<a href="foo.php" onclick="someFunction("hi mom!");">");
you actually have code 3 levels deep. 您实际上有3级代码。 There's the original Javascript (layer 1): 有原始的Javascript(第1层):
.append("<a href="foo.php" onclick="someFunction("hi mom!");">");
^--------------layer 1--------------------------------^
Then there's the embedded HTML (layer 2): 然后是嵌入式HTML(第2层):
.append("<a href="foo.php" onclick="someFunction("hi mom!");">");
^-------^ ^----------layer 2---------^
And then there's the sub-embedded Javascript (layer 3): 然后是子嵌入的Javascript(第3层):
.append("<a href="foo.php" onclick="someFunction("hi mom!");">");
^-------^
IN every case, you have to use the quotes appropriate for the layer you're embedding within. 在每种情况下,都必须使用适合嵌入其中的图层的引号。 So to embed your layer 2 quotes within layer 1, use escapes: 因此,要将第2层引号嵌入第1层,请使用转义:
.append("<a href=\"foo.php\" onclick=\"someFunction(\"hi mom!\");\">");
So now you've got valid Javascript at layer 1. But the HTML itself is still broken, because once that snippet is embedded in the DOM, you end up with 因此,现在您在第1层有了有效的Javascript。但是HTML本身仍然坏了,因为一旦将该代码段嵌入到DOM中,您最终就会
<a href="foo.php" onclick="someFunction("hi mom!");">
^---start attribute
^---end attribute
^--start unterminated attribute
So now you have to worry about embedding layer 3 in layer 2. Since it's Javascript-in-html: 因此,现在您必须担心将第3层嵌入第2层。因为它是Javascript-in-html:
.append("<a href=\"foo.php\" onclick=\"someFunction("hi mom!");">\");
^--------^----------^ etc... layer 2-in-1 escapes
^^^^^--------^^^^^ layer 3-in-1 quotes
Basically - you always have to consider the context in which the code will be seen. 基本上-您始终必须考虑将在其中看到代码的上下文。 If you're embedding HTML in javascript, any quotes in HTML must also be valid Javascript. 如果要在javascript中嵌入HTML,则HTML中的所有引号也必须是有效的Javascript。 If you're embedding Javascsript in HTML, then any JS quotes must also produce valid HTML. 如果要在HTML中嵌入Javascsript,则任何JS引号也必须产生有效的HTML。 And when you're nesting languages 3 or 4 levels deep, you have to consider ALL of the layers. 而且,当您将语言嵌套3或4级深时,必须考虑所有层。 Every layer must itself be valid to all of the parent layers. 每一层本身必须对所有父层均有效。
使用单引号.. IE
$(this).append('<a href="index.php?patient=test&a=' . 'appointment_nums[count]' . '>Schedule Appointment </a>');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.