1

I wish to convert button link with js from html to php

i added click function when i wrote this code in php the onclick function not working

Html

<button onclick="document.getElementById('id01').style.display='block'" style="width:auto;">

</button>

Corephp

<?php 
    echo '<button onclick="document.getElementById("id01").style.display="block" " style="width:auto;">';
?>

 This php echo line not working that means that onclick function.
Kalai Shruthi
  • 21
  • 1
  • 4
  • `onclick="document.getElementById("id01").style ...` try counting the quote marks, you're going to need to escape some (or break out of PHP or use HEREDOC syntax) - I suspect if you view source you'll see the broken HTML in place. – CD001 May 14 '18 at 15:52
  • Although it is useful to know the difference between client-side and server-side, I don't think it is the good duplicate to use for this question. – Karl-André Gagnon May 14 '18 at 15:54
  • @Karl-AndréGagnon at risk of getting off topic can you suggest another? It is one I encounter frequently. oh wait, nvm, as in a 'bad close'. See my remarks. mea culpa. – ficuscr May 14 '18 at 15:55
  • 1
    @ficuscr This one may be better: https://stackoverflow.com/questions/2109583/whats-the-best-practice-to-set-html-attribute-via-php. I've tested the solution with `onclick` instead of `title` and it works. – Karl-André Gagnon May 14 '18 at 15:59
  • Or even the one you posted in your answer. – Karl-André Gagnon May 14 '18 at 16:00

1 Answers1

1

Ah, I see. It is an escaping issue. Sorry, missed the mark with my first comment (Saw it as a client side / server side disconnect) and think I hijacked your question.

<?php 
    echo '<button onclick="document.getElementById(\"id01\").style.display=\"block\" " style="width:auto;">';
?>

These may help:

Pass a PHP string to a JavaScript variable (and escape newlines)

How to escape string from PHP for javascript?

ficuscr
  • 6,975
  • 2
  • 32
  • 52