Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

hi im new to php and im wonder if there any way to use javascript inside php code then i want to know how to call that function in javascript inside php code , really im sorry for my bad english and i hope that make my problem clear

<?php
    ob_start();
    require 'connect.inc.php';

    session_start();
    $usrequest = $_REQUEST['usr'];
    $pwdrequest = $_REQUEST['pwd'];

    $id = 'msg';

    echo '<script type="text/javascript" language = "javascript">

        function send ()
        {
            document.getElementById('msg').innerHTML = "asdsd";
        }
    </script>';    
?>
share|improve this question
add comment

6 Answers

up vote 3 down vote accepted

1) If your question is "Can I echo javascript code using the php echo function?",

you have not escaped the string to be echoed. Change it as follows:

<?php
    ob_start();
    require 'connect.inc.php';

    session_start();
    $usrequest = $_REQUEST['usr'];
    $pwdrequest = $_REQUEST['pwd'];

    $id = 'msg';

    echo '<script type="text/javascript" language = "javascript">

        function send ()
        {
            document.getElementById(\'msg\').innerHTML = "asdsd";
        }
    </script>';    
?>

2) If your question is "Can I call a javascript function from php (from the server)?"

you cannot. PHP executes in the server and sends the resultant page to the client (browser). Then, in your browser, the javascript code is executed.

enter image description here

share|improve this answer
add comment

You can't.

PHP runs on the server side before the page is downloaded by the client. JavaScript runs on the client side after the page has been downloaded by the client.

Therefore it is impossible for PHP to call a JavaScript function. It is possible to write JavaScript with PHP as example your code shows but not to execute it.

share|improve this answer
    
please more explain of how can i call a php function from javascript –  Bassam Badr Jun 15 '13 at 12:50
    
@BassamBadr I'm saying it isn't possible. There is nothing more to explain. You can only write JavaScript with PHP not execute it. That is the browser's job. –  George Reith Jun 15 '13 at 12:51
add comment

What you're doing would work except that you're using single quotes inside single quotes. Change it to:

document.getElementById("msg").innerHTML = "asdsd";

It's a different matter, of course, get a return value back from JavaScript to the PHP script, as the PHP script has finished running before the JavaScript one starts.

share|improve this answer
add comment

Writing JavaScript in PHP is nothing but writing JavaScript for the DOM. You can write it in PHP. use within PHP or directly.

Example:

<?php
echo "
    <script type=\"text/javascript\">
        function greeting(name){
            alert(name);
        }
    </script>
";
?>
<script type="text/javascript">
greeting('srihari');
</script>

I hope that this will help you.

And don't use single quotes within single quotes,

share|improve this answer
add comment

You can simply call that function and this will run after PHP code will send to your browser but it will give you result as you want

You can use this

<?php
ob_start();
require 'connect.inc.php';

session_start();
$usrequest = $_REQUEST['usr'];
$pwdrequest = $_REQUEST['pwd'];

$id = 'msg';

echo '<script type="text/javascript" language = "javascript">

    function send ()
    {
        document.getElementById('msg').innerHTML = "asdsd";
    }
echo '<script type="text/javascript>send();</script>"'
</script>';    
?>
share|improve this answer
add comment

There exist a couple of attempts to make Google's V8 javascript engine available from within PHP, e.g. https://github.com/preillyme/v8js

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.