-2

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

3
  • try to put it in a variable and echo that variable Commented Jul 30, 2021 at 18:57
  • pls do not do this.. Commented Jul 30, 2021 at 18:59
  • I thought this seemed familiar... stackoverflow.com/questions/68591535/… Commented Jul 30, 2021 at 19:23

2 Answers 2

2

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;
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

i want hide this js from client whos trying to view page source thats why m trying to do php in js code
That is impossible. Your PHP will still print to the HTML page and be visible. You cannot execute arbitrary, invisible JavaScript on a user's machine; that would be a security nightmare.
0

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/

4 Comments

i want hide this js from client whos trying to view page source thats why m trying to do php in js code
It will still show up in view page source even if you echo it using PHP. @Akki
then how to hide this js file from client user
You can't hide it because JS is client-side unlike PHP which is server-side. If you really wanted to, you can minify it to make it harder to read or understand. javascript-minifier.com