I’m having problems outputting php to javascript when i only output one line javascript works when i put my for loop in javascript breaks no errors just no output i use firebug and i see that output is bugged for instance:
“‘ANS” . ‘+’ . “‘ANS”. ‘+’ . “‘ANS” If i output text like this from php it works. When i output a single anchor like so:
'"' . '<a href="#">Text</a>' . '"';
However when i put this in a for loop it breaks i tried many options:
function step1() {
modalbox.show(new Element("div").insert(
new Element("p", { "align": "justify" }).insert(
<?php $i = 0; ?>
<?php foreach ($items as $category => $itemsattr): $i++; ?>
<?php if($i == 27): ?>
<?= "'" . '<a class="category" href="#"> '. $category . '</a>' . "'" ?>
<?php endif; ?>
<?= "'" . '<a class="category" href="#"> '. $category . '</a>' . "'+" ?>
<?php endforeach; ?>
)
), {
"title" : "Step 1/3",
"width" : 800,
"options" : [{
"label" : "Next »",
"onClick" : step2
}]
});
};
If i output only one like this it works:
<?= "'" . '<a class="category" href="#">Text</a>' . "'" ?>
However when i put that in a loop and add ‘+ at the end of each one except last one it breaks.
My for loop outputs this when checked with Firebug:
'<a class="category" href="#"> Assault Ship</a>'+ '<a class="category" href="#"> Battlecruiser</a>'
From what i know this should be valid for javascript is there another way more secure for outputing php past javascript to avoid problems like this ?
Instead of this:
What if you try something like this?
You also may have better luck with this; it’s more scalable than what you had:
Note the different placement of the + as well as the first conditional.
Like others have said, this general approach may not be the best, but this might make what you have work better…