Skip to content Skip to sidebar Skip to footer

Calling Jquery Function In Php (or Modifying Javascript So Php Runs The Remaining Code)

I was having difficulty calling the necessary jQuery functions in php so I added them to the javascript, but the method I’m familiar with (the success function) prevents the php

Solution 1:

It's important to understand that javascript is a client-side language(meaning it runs in the user's browser) and php is a server-side language(meaning that it runs on your server). In order to get javascript and php to interact with each other, you're going to need to use AJAX.

since you're already using jQuery, I would suggest you check out their AJAX api. Essentially, every time you want to call a php function from within your javascript code, you're going to have something along the lines of this:

$.ajax({
  type: "POST", /*usually POST, but it can also be GET and some other HTTP requests in certain browsers*/url: "some.php", /*the url of the php which processes your data*/data: { name: "John", location: "Boston" } /*the data you want to pass to the server.  It will be contained in the $_POST array because of the 'type: "POST"' line above*/
}).done(function( msg ) {
  alert( "Data Saved: " + msg ); /*'msg' is what server send back to you while the contents of the function are what you do once the server finishes.  You can change the variable name, msg, as well as the function contents to suit your needs */
});

Post a Comment for "Calling Jquery Function In Php (or Modifying Javascript So Php Runs The Remaining Code)"