I need to play a php variable in javascript

0

I have a code where I need to put a variable inside a javascript that is the ID of the person I'm pulling, but I can not find the solution. Where am I going wrong?

<?php echo "
                <tr>
                <td>".$row['nome']."</td>
                <td>".$row['telefone']."</td>
                <td>".$idade."</td>
                <td>".$row['cidade']."</td>
                <td>".$row['cargo_pretendido']."</td>

                <td><button onclick='mostrar()' class='w3-button w3-black'>INFOk123</button><td>
                </tr>

                <div id='id\"$row['id']\"' class='w3-modal'>
                    <div class='w3-modal-content'>
                        <header class='w3-container w3-teal'>
                            <span onclick='sumir()' class='w3-button w3-display-topright'>&times;</span>
                        </header>
                        <p>Teste</p>
                    </div>
                </div>

          ";
    }
    echo "<br>";

}


?>

<script language="javascript" type="text/javascript">


    function mostrar() { 
       document.getElementById('id"<?php echo $row['id']; ?>"').style.display='block'; 
    }

    function sumir() { 
       document.getElementById('id"<?php echo $row['id']; ?>"').style.display='none'; 
    }

</script>
    
asked by anonymous 21.07.2018 / 14:13

1 answer

0

Since you are receiving the ID dynamically, it would be ideal to receive the ID in the functions:

//Receber o ID nas funções
<script language="javascript" type="text/javascript">

function mostrar(id) { 
   document.getElementById(id).style.display='block'; 
}

function sumir(id) { 
   document.getElementById(id).style.display='none'; 
}

</script>

And pass the ID as parameter when generating html with php

<td><button onclick=\"mostrar('id".$row['id']."')\" class='w3-button w3-black'>INFOk123</button><td>
            </tr>

            <div id='id".$row['id']."' class='w3-modal'>
                <div class='w3-modal-content'>
                    <header class='w3-container w3-teal'>
                        <span onclick=\"sumir('id".$row['id']."')\" class='w3-button w3-display-topright'>&times;</span>
                    </header>
                    <p>Teste</p>
                </div>
            </div>
    
21.07.2018 / 15:05