How to add javascript into php? [duplicate]

Solution for How to add javascript into php? [duplicate]
is Given Below:

Here is my js code

<script>
function myform() {
    var n = document.getElementById("name").value;

    if (n == "") {
        document.getElementById("username").innerHTML = "Please enter your name";
        return false;
    }
    if (!isNaN(n)) {
        document.getElementById("username").innerHTML = "Please enter valid name";
        return false;
    }
}
</script>

How to write this js code into php

m trying all code in echo but not working plz help

after php echo using

    echo'<script>
function myform() {
    var n = document.getElementById("name").value;

    if (n == "") {
        document.getElementById("username").innerHTML = "Please enter your name";
        return false;
    }
    if (!isNaN(n)) {
        document.getElementById("username").innerHTML = "Please enter valid name";
        return false;
    }
}
</script>';

thankyou

You have the right idea, you can just echo a <script> tag with JavaScript inside it, but you have to be careful about quotation marks (which works with your example, you have single-quoted the HTML and then used double-quotes inside the JavaScript). If your script is not working, it would be because you don’t have the <?php frontmatter that every PHP script needs:

<?php
echo '<script>
function myform() {
    var n = document.getElementById("name").value;

    if (n == "") {
        document.getElementById("username").innerHTML = "Please enter your name";
        return false;
    }
    if (!isNaN(n)) {
        document.getElementById("username").innerHTML = "Please enter valid name";
        return false;
    }
}
</script>';

This is functional but I would strongly recommend splitting the JavaScript off into a separate JS file and then referencing that file’s location via the src attribute of the script tag. That will be far more maintainable in the future (and has the added benefit of working around quotation problems). So like

<?php
echo '<script src="script.js"></script>'

and in script.js:

function myform() {
    var n = document.getElementById("name").value;

    if (n == "") {
        document.getElementById("username").innerHTML = "Please enter your name";
        return false;
    }
    if (!isNaN(n)) {
        document.getElementById("username").innerHTML = "Please enter valid name";
        return false;
    }
}

Your JavaScript and PHP is correct. However, you need something to trigger the JavaScript function. In your case, for example you can add a button that triggers the JavaScript upon clicking.

<button onclick="myform();">Run</button>

Here is a working sample of your code:

HTML

<input type="text" id="name" value=""/>
<button onclick="myform();">
Run
</button>
Username: <span id="username">click run!</span>

JS

function myform() {
    var n = document.getElementById("name").value;

    if (n == "") {
        document.getElementById("username").innerHTML = "Please enter your name";
        return false;
}
    if (!isNaN(n)) {
        document.getElementById("username").innerHTML = "Please enter valid name";
        return false;
    }
}

JSFiddle: https://jsfiddle.net/g1j0wxmk/