How do i call javascript function from php function?
What is equivalent of ScriptManager.RegisterStartUpScript()
of Asp.Net in Php? I want to call so开发者_JAVA百科me Javascript function from my php function i.e after some event is fired (may be adding record to database) i want to call javascript function. The event is fired using Ajax of JQuery.
Thanks in advance :)
you can't. php is parsed on the server. js is parsed on the client.
If the event is fired using JQuery, you can use the Success parameter of the Ajax options to specify the function to call.
For example:
var options = {
url: 'urlForAJAXCall.php',
success: function(){
//Call your function here
},
error: function() {
alert('failure');
}};
$.ajax(options);
Probably what you want is to run the javascript or jQuery in the success event of the ajax request.
Just remember that "success" simply means the http request completed (the browser talked to the server), it doesn't mean the .php or database query worked.
In addition, you can return something from php, possible in a json array to give you a result which javascript can then use for example: check to see if a username already exists in the database, and give an error if it does, otherwise add the username as a new user.
精彩评论