Calling PHP scripts from Javascript without leaving the current page

I'm having trouble calling PHP scripts from Javascript without leaving the current HTML page (if at all possible). I understand that you can use AJAX, although is it possible to use only Javascript?

Context: -

I want my page to perform a short animation using Javascript (using onclick), and then immediately call a PHP script to insert data into the MySQL database - all without leaving the page so that it does not block the animation.

The part of the animation that I can execute and paste the data into a database, etc., but how can I call a PHP script at the end of this animated Javascript function?

Any pointers, code snippets, etc. will be very grateful! ^ _ ^

Sorry if this question has been asked before.

+5
source share
7 answers

AJAX is asynchronous Javascript and XML, Its Javascript technology that allows you to send a request to the server (as your browser does when entering the URL), and have a response in the javascript string instead of displaying it on the page.

The problem in different browsers does not implement AJAX the same way, so I suggest using jQuery for abstraction.

do it with jQuery:

<script>
$.get("backend.php",{param:value},callbackFunction);
callbackFunction(data)
{
alert(data);
}

</script>
+9
source

I just had the same problem, so I came up with something similar. All you have to do is add the code you need and assign the do_the_script function to the onclick event.

<script type="text/javascript">
var myIntv;
function do_the_script() {
    // play animation ...
    var address='fancy_script.php';
    var tmp = new XMLHttpRequest();
    myIntv=setInterval(function(){
    tmp.addEventListener("load", doneHandler, false);
    tmp.open("POST", address);
    tmp.send(null);
    }, 1000);
}

function doneHandler(event) {
    // maybe do something when script is executed
    clearInterval(myIntv);
}
</script>

, , "" , 1 . , , , doneHandler . , , .

+2
+1

PHP - . JavaScript - .

, - . , (aka AJAX).

JavaScript, jQuery MooTools . , MooTools script :

var req = new Request({url: '/backend/doPHPInsert.php'});
req.send();

AJAX, , iFrame ( , ).

+1

, AJAX, Javascript?

XHR, ...

var request = 'mysql-insert.php',
    image = new Image();

image.onload = function() {
  // Success
}

image.onerror = function() {
  // Error
}

image.src = request;

, AJAX ( ).

AJAX. jQuery XHR.

0

unadulterated XMLHttpRequest: https://developer.mozilla.org/En/XMLHttpRequest/Using_XMLHttpRequest

( , - .)

: . http://msdn.microsoft.com/en-us/library/ms535874%28v=vs.85%29.aspx, IE, native window.XMLHttpRequest.

, - , XHR ( -) . - (jQuery, Dojo, MooTools, Prototype, Closure, YUI ..), XHR , , , XHR.

- , , .:)

0

jquery,

a) ad iframe, 0px, , href .

b) js-

 window.frames['iframename'].location.replace('http://....your . php');

php script, , , ...

0

All Articles