0

I have to show the contact num from database when user click on the button.. but when I use php in javaScript it doesn't work for me... here is HTML

<button name="showNo" id="phone1" onclick="myFunction()"><span>Show Phone No</span></button>
<p id="show">******</p>

here the JavaScript

<script type="text/javascript">
function myFunction(){
    var hsh = document.getElementById("show");
    if ( hsh.value === "******" )
        hsh.value = "<?php echo $contact; ?>";
    else
        hsh.value = "Open Curtain";
}
</script>

is there any other method to do this,,?

3
  • 1
    What does the rendered Javascript look like? What do you mean, "doesn't work"? What's the error message? Commented Apr 15, 2019 at 4:43
  • There is no error shown.. But it do nothing when I click on the button.. Commented Apr 15, 2019 at 4:47
  • 1
    @catcon - How would removing the quotes change anything? If $contact is a string, the quotes are required. Commented Apr 15, 2019 at 4:49

1 Answer 1

2

Try this, p tag does not have value so you need to use innerHTML to get data from it and set it

var hsh = document.getElementById("show").innerHTML;
if(hsh === "******"){
    document.getElementById('show').innerHTML = "<?php echo $contact; ?>";
}else{
    document.getElementById('show').innerHTML = "<?php echo 'Open Curtain'; ?>";
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.