Execute JavaScript in PHP

How to call a JavaScript function from PHP?

As far as PHP is concerned (or really, a web server in general), an HTML page is nothing more complicated than a big string.

All the fancy work you can do with language like PHP - reading from databases and web services and all that - the ultimate end goal is the exact same basic principle: generate a string of HTML*.

Your big HTML string doesn't become anything more special than that until it's loaded by a web browser. Once a browser loads the page, then all the other magic happens - layout, box model stuff, DOM generation, and many other things, including JavaScript execution.

So, you don't "call JavaScript from PHP", you "include a JavaScript function call in your output".

There are many ways to do this, but here are a couple.

Using just PHP:

echo '<script type="text/javascript">',
'jsfunction();',
'</script>'
;

Escaping from php mode to direct output mode:

<?php
// some php stuff
?>
<script type="text/javascript">
jsFunction();
</script>

You don't need to return a function name or anything like that. First of all, stop writing AJAX requests by hand. You're only making it hard on yourself. Get jQuery or one of the other excellent frameworks out there.

Secondly, understand that you already are going to be executing javascript code once the response is received from the AJAX call.

Here's an example of what I think you're doing with jQuery's AJAX

$.get(
'wait.php',
{},
function(returnedData) {
document.getElementById("txt").innerHTML = returnedData;

// Ok, here's where you can call another function
someOtherFunctionYouWantToCall();

// But unless you really need to, you don't have to
// We're already in the middle of a function execution
// right here, so you might as well put your code here
},
'text'
);

function someOtherFunctionYouWantToCall() {
// stuff
}

Now, if you're dead-set on sending a function name from PHP back to the AJAX call, you can do that too.

$.get(
'wait.php',
{},
function(returnedData) {
// Assumes returnedData has a javascript function name
window[returnedData]();
},
'text'
);

* Or JSON or XML etc.

Execute javascript in PHP

To evaluate JavaScript code using PHP, have a look at the V8 JavaScript engine extension, which you may compile into your PHP binary:

  • http://php.net/manual/en/book.v8js.php

V8 is Google's open source JavaScript implementation.

calling javascript function from php

Your html is invalid. You're missing some tags.

And you need to call the function after it has been declared, like this

<html>
<head>
<title></title>

<script type="text/javascript">
function run(){
alert("hello world");
}

<?php
echo "run();";
?>
</script>

</head>
<body>
</body>
</html>

In this case you can place the run before the method declaration, but as soon as you wrap the method call inside another script tag, the script tag has to be after the method declaration.

Try yourself http://jsfiddle.net/qdwXv/

Executing javascript from within a php file

Think this is what you are looking for:
How to call a JavaScript function from PHP?

PHP file is server side yes, so is Javascript depending on what you are doing with it. PHP can absolutely execute a Javascript script within its main .php file. You just can not include anywhere in your program like before.

Below is an example to run a Javascript script that simply alerts the browser to the string "pong". Notice how the PHP statement is still executed.

We could be more helpful if you posted the full code and were very efficient and detailed explaining what you needed to be answered.

Copy and paste this code into a PHP file by itself, so you can test without any errors your full code might contain:
index.php

<?php
echo 'Ping' ?>
<script>alert('Pong');</script>

<?php
// PHP code goes inside here
?>
<script>
// Javacript code goes inside here
</script>

Call Javascript function and execute PHP at the same time?

You can just load the new page with PHP after you check the login. Unless you have some other reason you need to do it with JavaScript, I think it makes more sense to do it in PHP anyway, because you'll want to do different things depending on whether or not the login was successful.

if ($result->num_rows > 0) {
$_SESSION["email"] = $_POST['email'];
header('Location: new url');
exit;
}
else {
echo "Incorrect username or password. Please try again!";
}

Run JavaScript from PHP if statement

Try this:

if (condition){ 
my database code;
} else {
my database code;
echo '<script type="text/javascript">function();</script>'
}

What was happening was that in "<script type="text/javascript">function();</script>" there were double quotes that weren't being escaped. Using singlequotes fixes this.



Related Topics



Leave a reply



Submit