0

I'm working in PHP and I have this button, than when clicked, should print something.

I've (unsuccessfully) tried the following:

<?php
echo '<button type="submit" id="enviarRecibos" class="btn btn-primary float-end">Imprimir Lista</button>'; 

?>
<script>
if(document.getElementById('enviarRecibos').clicked == true){
    <?php
    echo $listaDatos;
    ?>
}​;​
</script>

What am I doing wrong?

Note: $listaDatos is a variable that's already in the page with some content in JSON format.

Rosamunda
  • 14,620
  • 10
  • 40
  • 70
  • 1
    PHP is run on the server, JS on the client. `echo $listaDatos;` just prints out content, nothing JS could do anything with – brombeer Sep 26 '21 at 19:22
  • also your event handler is wrong. You can find out how to handle a "click" event in 1000 places online already...heres just one of them: https://www.google.com/amp/s/www.freecodecamp.org/news/javascript-addeventlistener-example-code/amp/ – ADyson Sep 26 '21 at 19:22
  • Does this answer your question? [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – CBroe Sep 27 '21 at 08:09

1 Answers1

1

I'm not sure i've well understand what you exactly want, but anyway your javascript script need improvement :

<?php
echo '<button type="submit" id="enviarRecibos" class="btn btn-primary float-end">Imprimir Lista</button>'; 

?>
<script>
document.getElementById('enviarRecibos').addEventListener('click', function (){
    document.getElementById('enviarRecibos').insertAdjacentHTML('afterend', '<?php echo $listaDatos;?>') ;
}) ;
</script>

First the best way to track click on an element in javascript is with addEventListener. Next, you want load content, ok, but you need to define the position / DOM element where the content need to be loaded. You can use "insertAdjacentHTML", an usefull function putting HTML content next to an existing element (here your button). Take care "<?php echo $listaDatos;?>" doesn't contain a quote ' and return a valid HTML string.

AS @brombeer just told you, PHP is a server side language while Javascript is client side. PHP can only help you prepare the raw file code.

Camille
  • 847
  • 1
  • 7
  • 19