I am trying to connect a database with php and display result dynamically using javascript. Here's What i am trying to do -
<?php
function mainMenu($q){
$res=array();;
$q->setFetchMode(PDO::FETCH_NUM);
while($r = $q->fetch()){
array_push($res, "
<li>
<a class='gn-icon ".mysql_real_escape_string($r[0])."'>".mysql_real_escape_string($r[1])."
</a>
</li>");
}
return $res;
} ?>
Now here is the html , which definitely works
<ul id="sidemenu" class="gn-menu">
<?php
$a=mainMenu($q);
foreach ($a as $value) {
echo $value;
}
?>
</ul>
but when i try this -
<script>
$('#sidemenu').html(<?php
$b=mainMenu($q);
foreach ($b as $value) {
echo "$value";
}
?>);
</script>
It doesnt work the, i just see blank space in my source and nothing is printed in the list, can anyone tell me where i am going wrong...
asked Sep 1, 2013 at 7:08
Harshit Laddha
2,1248 gold badges36 silver badges70 bronze badges
2 Answers 2
<?php
function mainMenu($q) {
$res=array();
$q->setFetchMode(PDO::FETCH_NUM);
while( $r = $q->fetch() ) {
array_push($res, "<li><a class='gn-icon ".mysql_real_escape_string($r[0])."'>".mysql_real_escape_string($r[1])."</a></li>");
}
return $res;
}
?>
<script>
$('#sidemenu').html("<?=implode('',mainMenu($q))?>");
</script>
answered Sep 1, 2013 at 7:15
mas.morozov
2,7461 gold badge25 silver badges22 bronze badges
Sign up to request clarification or add additional context in comments.
6 Comments
Harshit Laddha
$("#sidemenu").html("<?php $b=mainMenu($q); $c=implode('',$b); echo $c; ?>"); It doesnt Work
mas.morozov
Oh, Javascript does not allow newlines in string literals, so you must clean them or generate one-line HTML
Harshit Laddha
Okay yeah got it this way $('#sidemenu').html(<?php $b=mainMenu($q); $c = implode('',$b); echo mysql_real_escape_string("$c"); ?>);
Harshit Laddha
Yeah, urlencode, doesnt work here i tried and it gave me some weird encoded codes in my script tag when i viewed it in firebug
mas.morozov
urlencode is completely wrong here, it will be good for <a href="<?...?>"> |
you need to escape the single quote in your output using "Backslash",
<a class=\'gn-icon ".mysql_real_escape_string($r[0])."\'>".mysql_real_escape_string($r[1])." </a>
and you need use the .html like this,
.html('<?php $b=mainMenu($q); foreach ($b as $value) { echo $value;} ?>')
3 Comments
Harshit Laddha
I think i did the same think but it wont work, since the output is html with new line characters and quotes so i had to use mysql_real_escape_string() function to make it work
rAjA
Oh right..So you have tried adding the "Blackslash" like this <a class=\'gn-icon ? Because I just ran your code with this change in my server and it worked!!
Harshit Laddha
It might because from your sql end nothing will be returned , but for me the problem is with the sql result , which has the tags so had to use the function to escape the output from the sql
default
json_encode.<a class='gn-icon ".mysql_real_escape_string($r[0])."'>should beurlencode.html('<? your_code?>'). QUOTESimplodeinstead offoreachwhile printing.