0

How to create var javascript via php ?

<?PHP
include("connect.php");
$get_data = mysqli_query($db_mysqli,"SELECT * FROM bad_word");
while($resilt_row = mysqli_fetch_array($get_data))
{ 
    $bad_words = $bad_words."".$resilt_row [word].",";
}
//echo $bad_words;
?>

<script>
var bad_words = ["<?PHP echo $bad_words; ?>"];
alert(bad_words);
</script>

I want to get var javascript like this var bad_words = ["fuck", "ass"]; When alert it's get only blank result.

How can i do that ?

2
  • 1
    first of all, instantiate the $bad_words variable before the while block Commented Dec 13, 2016 at 15:43
  • Surely $resilt_row [word] should be $resilt_row['word']? Commented Dec 13, 2016 at 15:44

4 Answers 4

1
<?php

include("connect.php");
$query = mysqli_query($db_mysqli,"SELECT * FROM bad_word");
$badWords = [];

while($row = mysqli_fetch_array($query))
{ 
    $badWords[] = $row['word'];
}

js

<script>
var bad_words = <?= json_encode($badWords); ?>;
alert(bad_words[0]);
console.log(bad_words);
</script>

I don't like gluing the strings if it's an array then pass it as an array with json_encode() function. Also in views it's better to use short syntax with <?= ?> tag

Sign up to request clarification or add additional context in comments.

Comments

0

The string you're making is not the one you want.
You're not adding quotes.

Do this instead, using an array is better:

$get_data = mysqli_query($db_mysqli,"SELECT * FROM bad_word");
$bad_words = array();
while($resilt_row = mysqli_fetch_array($get_data))
{ 
    $bad_words[] = "'" . $resilt_row[word]. "'";
}

And then, down in your js:

var bad_words = ["<?php echo implode(",", $bad_words) ?>"];

or (for short)

var bad_words = ["<?= implode(",", $bad_words) ?>"];

2 Comments

i get var bad_words = ["fuck,ass"]; not var bad_words = ["fuck", "ass"]; ?
Weird, you should have been getting var bad_words = ['first', 'second']
0

If you want alert them as a string you can do it like this:

<script>
    var bad_words = ["<?php echo implode('","',$bad_words); ?>"];
    alert(bad_words);
</script>

Otherwise, you can print them as an array:

<script>
    var bad_words = <?php echo json_encode($bad_words) ?>;
    console.log(bad_words);
</script>

1 Comment

Only for the case that you got the answer-check. This part is not ok ',' it must be '","'
0
<?PHP
include("connect.php");
$get_data = mysqli_query($db_mysqli,"SELECT * FROM bad_word");
while($resilt_row = mysqli_fetch_array($get_data))
{ 
 $bad_words[] = $resilt_row['word'];
}
//echo $bad_words;
?>

<script>
var bad_words = ["<?php print implode('","',$bad_words); ?>"];
alert(bad_words);
</script>

5 Comments

Do you mean: implode ?
Yes, mean implode :)
i get var bad_words = [""]; not var bad_words = ["fuck", "ass"]; ?
@weduti sepoyuei Sorry, just add an echo
@weduti sepoyuei But only as note: Did you now how many way exists to write something like F**k? It wil be an heavy task to prevent all bad words. And also showing the blockwords in the HTML code will help little crack-heads to get around your blocklist. Lets say i type: F.uck or F!uck .... neverending....

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.