Can someone help with some javascript?

Started by rcane, October 24, 2022, 01:25:48 PM

Previous topic - Next topic

rcane

I have a simple text field that turns green if you type in a value that exists in the themes.`value` table.  It's red until you've typed the proper 'text'.

I duplicated the script on a new page, but this time I'm using a simple table with a single column, and I can't get the same responses. So, I'm thinking the array is bad.

I've checked the new table's array results in php to is if it's is_object() and it says it is.  And a var_dump shows the same format (though table 2 only has a few test entries versus 2000 from the themes query.

This is the var_dump of the themes table results (just the first 3 copied)

array1 is this: 

array(1845) { [0]=> object(stdClass)#38 (1) { ["value"]=> string(7) "u302352" } [1]=> object(stdClass)#40 (1) { ["value"]=> string(7) "U360576" } [2]=> object(stdClass)#41 (1) { ["value"]=> string(7) "U021061" } [3]=> object(stdClass)#42 (1) { ["value"]=> string(7) "u126207" }
This is the var_dump of array2 (mine)

array(2) { [0]=> object(stdClass)#1885 (1) { ["serial"]=> string(7) "u444333" } [1]=> object(stdClass)#1886 (1) { ["serial"]=> string(7) "U999999" } }
this is the javascript:

<script type="text/javascript">
    const idk = document.getElementById('idk');
        idk.addEventListener('input', checkNumber);
        idk.addEventListener('focus', checkNumber);
        idk.addEventListener('blur', checkNumber);

    function checkNumber() {
       
         const theArray = <?php echo json_encode($array2); ?>;
         const test = theArray.map(item => item.value.toLowerCase());
   
         if (test.includes(idk.value)) {
          idk.style.background = "green"   
         } else if (theArray.find(item => item.value === idk.value) !== undefined) {
             idk.style.background = "green";
             
         }else {
             idk.style.background = "red";
         }
       
          }
         
</script>


I've tested the second array with: 

$is = in_array('U999999', array_column($second, 'serial'));
echo $is;

And it returns a 1, so it's seeing it. 

rcane

yeah, I'm good.

Delete this quickly to avoid any further embarrassment. :)

Advertisement: