dpjr86761 2015-03-15 08:14
浏览 36
已采纳

使用php调用javascript函数

This is my php code

if(isset($_POST['submit'])){
    $post_string=$_POST;
    $enc_string=json_encode($post_string);
    print("<script>encfunction('$enc_string');</script>");
    }

this is my javascript code

<script>
function encfunction(data)
{
alert("data");  
alert("hello");
alert("I am an alert box!");
} 
</script>

I want to get the result in that function because i have to sent json response using ajax but it shows:Uncaught ReferenceError: encfunction is not defined.

I referred following links but it doesn't give the desired result: Javascript Uncaught Reference error Function is not defined

Uncaught ReferenceError: function is not defined with onclick

Uncaught ReferenceError: function is not defined jQuery

Can any one help me.

  • 写回答

1条回答 默认 最新

  • drhozgt6007 2015-03-15 08:19
    关注

    Functions have to be defined before they are called. Make sure your first block of code containing the call to the encfunction appears in your html document after it has been declared (in your second block of code). A better option is to define your function within the head element of the document.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?