简体   繁体   中英

How do i call javascript function from php function?

What is equivalent of ScriptManager.RegisterStartUpScript() of Asp.Net in Php? I want to call some Javascript function from my php function ie 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM