Join the Stack Overflow Community
Stack Overflow is a community of 6.8 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

php file

$querySelectWordFilter = "SELECT * FROM badwordfilter";
      $stmtSelectWordFilter = $conn->prepare($querySelectWordFilter);
      $stmtSelectWordFilter->execute();
      while($rowSelectWordFilter = $stmtSelectWordFilter->fetch()){
         $Array[] = $rowSelectWordFilter["filterWord"];
      }

    foreach($Array as $val){
         echo $val;
    }

Javascript file

<script>
 var filter = ['ass', 'evil','ugly'];
</script>

Question : Firstly,I select all the value from database and store it into array.But how can i pass the PHP array variable into JavaScript filter variable?

share|improve this question
4  
Just use var filter = <?php echo json_encode($Array); ?>; – Saty Mar 22 '16 at 6:07
1  
thank you..work :) – Ch Hong Mar 22 '16 at 6:08
up vote 8 down vote accepted

No need of foreach loop just create your array

while($rowSelectWordFilter = $stmtSelectWordFilter->fetch()){
         $Array[] = $rowSelectWordFilter["filterWord"];
      }

And in JavaScript use json_encode as

<script>
var filter = <?php echo json_encode($Array); ?>;
</script>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.