Akash
Akash

Reputation: 49

Calling javascript function using php

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.

Upvotes: 0

Views: 547

Answers (1)

sg-
sg-

Reputation: 2176

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.

Upvotes: 3

Related Questions