How can you use php in a javascript function

186,211

Solution 1

You can't run PHP code with Javascript. When the user recieves the page, the server will have evaluated and run all PHP code, and taken it out. So for example, this will work:

alert( <?php echo "\"Hello\""; ?> );

Because server will have evaluated it to this:

alert("Hello");

However, you can't perform any operations in PHP with it.

This:

function Inc()
{
<?php
$num = 2;
echo $num;
?>
}

Will simply have been evaluated to this:

function Inc()
{
    2
}

If you wan't to call a PHP script, you'll have to call a different page which returns a value from a set of parameters.

This, for example, will work:

script.php

$num = $_POST["num"];
echo $num * 2;

Javascript(jQuery) (on another page):

$.post('script.php', { num: 5 }, function(result) { 
   alert(result); 
});

This should alert 10.

Good luck!

Edit: Just incrementing a number on the page can be done easily in jQuery like this: http://jsfiddle.net/puVPc/

Solution 2

I think you're confusing server code with client code.

JavaScript runs on the client after it has received data from the server (like a webpage).

PHP runs on the server before it sends the data.

So there are two ways with interacting with JavaScript with php.

Like above, you can generate javascript with php in the same fashion you generate HTML with php.

Or you can use an AJAX request from javascript to interact with the server. The server can respond with data and the javascript can receive that and do something with it.

I'd recommend going back to the basics and studying how HTTP works in the server-client relationship. Then study the concept of server side languages and client side languages.

Then take a tutorial with ajax, and you will start getting the concept.

Good luck, google is your friend.

Solution 3

you use script in php..

<?php

        $num = 1;
        echo $num;

    echo '<input type="button"
           name="lol" 
           value="Click to increment"
           onclick="Inc()" />
    <br>
    <script>
        function Inc()
        {';
            $num = 2;
            echo $num;

        echo '}
    </script>';
?>

Solution 4

Simply, your question sounded wrong because the JavaScript variables need to be echoed.

<?php
        $num = 1;
        echo $num;
        echo "<input type='button' value='Click' onclick='readmore()' />";
   		echo "<script> function readmore() { document.write('";
        $num = 2;
        echo $num;
        echo "'); } </script>";
?>

Solution 5

There is a way to run php in client-side javascript (not talking about server-side js here). Don't know if this is a very good idea, but you can. As some people pointed out you have to keep in mind the php is evaluated on the server and then returned as static stuff to the browser who will then run the javascript with the added data from the server.

You have to tell the server to evaluate the js files as php files, if you are running an apache server you can do this with a htaccess-file like this:

<FilesMatch "\.js$">
SetHandler application/x-httpd-php
Header set Content-type "application/javascript"
</FilesMatch>

More info here:

Parse js/css as a PHP file using htaccess

http://css-tricks.com/snippets/htaccess/use-php-inside-javascript/

Edit: On http page request this trick makes files with js extension be parsed by the php compiler. All php parts in the js file will get executed and the js file with added server data will be handed to the browser.

However, the OP uses an html formated file (probably with php extension), no js file, so in this specific case there's no need for my suggestion.

The suggested js solutions are the way to go. If the variable needs to get stored on the server, use ajax to send it there.

Share:
186,211
fosho
Author by

fosho

Updated on January 29, 2020

Comments

  • fosho
    fosho over 4 years
    <html>
        <?php
            $num = 1;
            echo $num;
        ?>
        <input type="button"
               name="lol" 
               value="Click to increment"
               onclick="Inc()" />
        <br>
        <script>
            function Inc()
            {
            <?php
                $num = 2;
                echo $num;
            ?>
            }
        </script>
    </html>
    

    This is what i have so far, not working though, i think i need to use ajax or something but i have no idea what to do.

    Thanks