0

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...

6
  • Try with json_encode. Commented Sep 1, 2013 at 7:09
  • <a class='gn-icon ".mysql_real_escape_string($r[0])."'> should be urlencode Commented Sep 1, 2013 at 7:10
  • .html('<? your_code?>'). QUOTES Commented Sep 1, 2013 at 7:12
  • 1
    And use implode instead of foreach while printing. Commented Sep 1, 2013 at 7:14
  • I got an error Unexpected token ILLEGAL while using .html('<?php $b=mainMenu($q); foreach ($b as $value) { echo "$value"; ?>') and .html('<?php $b=mainMenu($q); foreach ($b as $value) { echo $value; ?>') Commented Sep 1, 2013 at 7:16

2 Answers 2

1
<?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>
6
  • $("#sidemenu").html("<?php $b=mainMenu($q); $c=implode('',$b); echo $c; ?>"); It doesnt Work Commented Sep 1, 2013 at 7:26
  • 1
    Oh, Javascript does not allow newlines in string literals, so you must clean them or generate one-line HTML Commented Sep 1, 2013 at 7:34
  • Okay yeah got it this way $('#sidemenu').html(<?php $b=mainMenu($q); $c = implode('',$b); echo mysql_real_escape_string("$c"); ?>); Commented Sep 1, 2013 at 7:37
  • 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 Commented Sep 1, 2013 at 7:40
  • urlencode is completely wrong here, it will be good for <a href="<?...?>"> Commented Sep 1, 2013 at 7:41
0

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
  • 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 Commented Sep 1, 2013 at 7:41
  • 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!! Commented Sep 1, 2013 at 7:44
  • 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 Commented Sep 1, 2013 at 7:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.