dpjr86761
dpjr86761
2015-03-15 08:14
浏览 31

使用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
    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.

    点赞 评论

相关推荐