0

What is equivalent of ScriptManager.RegisterStartUpScript() of Asp.Net in Php? I want to call some 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 :)

3 Answers 3

2

you can't. php is parsed on the server. js is parsed on the client.

Sign up to request clarification or add additional context in comments.

3 Comments

Yeah, offcourse js is parsed on client. But in Asp.Net it is supported. We can dynamically inject javascript in the page and can call function from server side which gets triggered on client side.
however if the process is triggered by an ajax call and you are wanting to for instance...user clicks a button and it sends a value to your script and your script updates a db table or something...and you want to have a js popup saying it was successful, you could perform the query and output in text the response for the ajax call to receive, and make the popup happen client-side, based on the response you send back.
^^ well you are asking about php, not Asp.Net. php can't do that, except for how I said above. Or I guess you could echo out the js code and flush the output buffer (ob_flush) immediately after, that might work...
1

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);

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.