0

I have the following JQuery Function.

   <script>

    var forbiddenWords = ['Phone', 'Home', 'Address', 'Number', 'Postcode', 'email', 'call','n u m b e r'];
    $(function () {
      $('.msgbox').on('keyup', function(e) {
        forbiddenWords.forEach(function(val, index) {
          if (e.target.value.toUpperCase().indexOf(val.toUpperCase()) >= 0) {
              e.target.value = e.target.value.replace(new RegExp( "(" + val + ")" , 'gi' ), '');
          }
        });
      });
    });

    </script>

I want the words in fordiddenWords array should be dynamic and must come from MySQL database. I have written the following code but it is not working..!

     <script>

 var forbiddenWords = [<?php foreach($result as $res) { echo $res->rest_words; } ?>];
        $(function () {
          $('.msgbox').on('keyup', function(e) {
            forbiddenWords.forEach(function(val, index) {
              if (e.target.value.toUpperCase().indexOf(val.toUpperCase()) >= 0) {
                  e.target.value = e.target.value.replace(new RegExp( "(" + val + ")" , 'gi' ), '');
              }
            });
          });
        });

        </script>

The words appear like this..!

var forbiddenWords = [phonenumberaddress];

Please Help.

5
  • Check your console for error Commented Aug 2, 2016 at 13:18
  • can you console.log this variable forbiddenWords ? Commented Aug 2, 2016 at 13:19
  • $res->rest_words is array of words ? or a single string ? Commented Aug 2, 2016 at 13:20
  • No, you don't need Ajax for this Commented Aug 2, 2016 at 13:21
  • Use the | operator of regex to avoid the for loop : value.replace(/word1|word2|word3/gi,'') Commented Aug 2, 2016 at 13:23

3 Answers 3

1

You can simply use json_encode function for convert php array into JSON/Javascript object or Array

var forbiddenWords = [<?php foreach($result as $res) { echo $res->rest_words; } ?>];

Replace it with

<?php
$arr=array();
foreach($result as $res) { $arr[]=$res->rest_words; }
?>
var forbiddenWords = <?php echo json_encode($arr); ?>;

OR

var forbiddenWords = <?php echo json_encode(array_map(function($record){ return $record->rest_words; }, $result)); ?>;
Sign up to request clarification or add additional context in comments.

1 Comment

you can check my edits for what was missing stackoverflow.com/posts/38721777/revisions
0

Echoing like this directly in Javascript is not the correct way to pass information. It would be easier if you convert your $result array to json so it's more native for js. Try:

http://php.net/manual/en/function.json-encode.php

So it should look something like this:

var forbiddenWords = <?=json_encode($result)?>;

3 Comments

Will produce array of objects. $result needs to be mapped first to expected output structure
var forbiddenWords = [{"rest_word":"phone"},{"rest_word":"number"},{"rest_word":"address"}];
Yes, as @charlietfl mentioned and as I missed to write you should map the result. Try var result = forbiddenWords.map(function(a) {return a.rest_word;}); and then result should be mapped array
0

You're missing the commas and the quotes:

 <script>

 var forbiddenWordsString = <?php foreach($result as $res) { echo "'" . $res->rest_words . "',"; } ?>;
 var forbiddenWords = [forbiddenWords.substring(0, str.length - 1)]; // remove last comma
    $(function () {
      $('.msgbox').on('keyup', function(e) {
        forbiddenWords.forEach(function(val, index) {
          if (e.target.value.toUpperCase().indexOf(val.toUpperCase()) >= 0) {
              e.target.value = e.target.value.replace(new RegExp( "(" + val + ")" , 'gi' ), '');
          }
        });
      });
    });

</script>

1 Comment

syntax error: trailing comma after last array element

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.