Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you use php in a javascript function [duplicate]

<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

like image 689
fosho Avatar asked Dec 12 '11 09:12

fosho


People also ask

Can you put PHP in a JavaScript function?

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.

Can you define a function twice in JavaScript?

functions are data in memory stack, so when you define another function with the same name, it overrides the previous one. Show activity on this post. Well obviously you're not meant to define the same function twice. However, when you do, the latter definition is the only 1 that applies.

Can we use PHP in jquery?

If you are trying to generate final javascript or jquery code using PHP then this is okay. Show activity on this post. Show activity on this post. PHP is executed on the server, and then the javascript will be executed on the client.


2 Answers

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/

like image 89
Andreas Eriksson Avatar answered Sep 22 '22 22:09

Andreas Eriksson


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.

like image 21
Vigrond Avatar answered Sep 25 '22 22:09

Vigrond