How to launch PHP file from Javascript without window.open - php

I'm using Javascript and I need to call a PHP file to execute some stuff, but I'd like not to use window.open("filename.php"); because it usually opens a new tab or new window.
Sometime a PHP file need a few seconds to finish its job and I don't like to see an open tab or open window while it's working.
Thanks

AJAX is the solution, and with jQuery it's so simple.
Just add this line on your head section (to include jquery):
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$.ajax({
type: "GET",
url: "yourPage.php"
}).done(function( data) {
alert( "Request is done" );
});
</script>

You can use ajax or dynamically create iframe.
If you use extjs library - here is example ajax request

Use Ajax! Here's a solution using JavaScript's jQuery library:
$.post('your_file.php', {parameter : some_value}, function(response){
// now you can use `response` came from your_file.php after execution
});
$.post(), $.get() are shorthand methods for $.ajax() of jQuery.
or simply use .load(),
('#test_div').load('your_file.php'); //load data from server into `#test_div`
will do job, if you want to just execute something on server-side.

Related

Making HTTP requests on button click

Need to make a HTTP request on click of a button on a webpage! , please guide me with an Example that whether using AJAX + PHP is the way out or Javascript will be able to do the same alone?
The easiest way is to bind onclick to the button
`onclick='location.href="mynewpage.html"`
AJAX is actually part of JavaScript.
For simplicity, you might want to use a library such as jQuery.
Put this in the <head> section to load jQuery...
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
And to execute an AJAX request, include this too...
<script type="text/javascript">
$('#id-of-your-button').onClick(function() {
$.ajax({
url: "backend.php"
}).success(function(data) {
// do something here
});
}
</script>
For more details on this function, refer to jQuery.ajax() here.

Display HTML while PHP is processing

I have a PHP script that takes around 1 minute 20 seconds to load as it fetches data from other websites. However I need some way to show the user that the page is loading. Something like 'This page is loading and may take up to 2 minutes' with a .gif loading images.
I have tried jquery:
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<script>
// Put an animated GIF image insight of content
$("#content").empty().html('<img src="http://www.website.com/images/loading.gif" />');
// Make AJAX call
$("#content").load("http://website.com/page.php")
</script>
<div id="content">
Content to load here!
</div>
</body>
</html>
But the page is blank (don't know if i'm implementing something wrong).
Is there any other way of doing so?
I have looked at other questions but can't find anything that matches what I need, that I can get to work.
Thanks,
Jack.
You won't get a response from the server, until the script has finished loading. Therefor you have to 'prepend' the page with another page, which shows the loading part. That page should call the script you want to execute, through for example an AJAX call.
You can use an AJAX call with beforeSend and success events:
$.ajax({
url: "http://website.com/page.php", type: "POST",
beforeSend: function() {
$("#content").html('<img src="images/loading.gif" />');
},
success: function(data) {
$("#content").html(data);
}
})
you got the script tags wrong. you need to close the script with the src call, and open a new one for the actual code
Also, you need to load the DOM before using JQuery to fetch objects. You can use $(document).ready() or place your script after the DOM element.
Where is your jquery loaded in the file? If its loaded at the top, you should wait for the dom to load before executing its JS.
http://api.jquery.com/ready/

Ajax call to a method that prints javascript

Ok,
I have a some javascript code in the database
Table: jsSnippets
Field: snippet
Type: Text
<SCRIPT SRC="https://svc.com/somestuff.js"></SCRIPT>
<script>
var fubar = 'stuf'
send_some_stuf_to_svc(fubar) // sends some data to a service :)
</script>
So i have N number of this JS snippets
will that code work if a server side method was called via Ajax call, for example:
$.ajax({
type: 'GET',
url: path + '/doTheJSStuff/',
)};
where the doTheJSStuff is a method that echo/prints the JS code
Your script must return javascript code (without html tags).
Call eval(text) after receiving text.
That will work, so long as the contents of the <script> tag are being passed into javascript's eval() function. If you're using a framework such as jQuery, its built-in $.ajax() method eval's tags automatically.

Execute PHP without leaving page

I have a form - textarea (named su) and submit button.
When the form is submitted, I need to
run a PHP script without refreshing/leaving page
"echo" or somehow print a return on the screen
I'm pretty sure this works via some kind of ajax request thing. but I have no idea how.
Like I said I'm uneducated with ajax or java. A quick example would be wonderful.
Simple , that is what is called AJAX. The solution is more of Javascript, than PHP.
Using Jquery, you can do it with a simple function call:
<script src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#ID_Of_Your_Button').change(function(){
$.ajax({
type: "GET",
url: "send.php",
data: "query="+document.form.textarea.value,
success: function(msg){
document.getElementById("Div_Where_you_want_the_response").innerHTML = msg }
})
});
});
</script>
Does the trick.
If your just now getting into ajax, PHP, and JQuery I would highly suggest using firefox and installing Firebug. Learn to use it and it will tell you all sorts of great things. There is not enough space to tell you everything it does, but it is ,at this point, one of my best debugging tools. Learn it, Love it and good luck.
It is usually best to use a Javascript library for doing AJAX.
See jQuery.get() for one way to send a request to a PHP web page using the jQuery library. You can have your PHP page output Javascript to be executed, or output plain text or data.

How can I add a simple counter function in javascript jquery?

I'm using Shadowbox a jquery overlay and I wanted to count how many people are actually using the overlay. Thus, I would need a function that would write a counter to a file or sending a query through a php api...
has to be a a php url api because I cant use php on the server where the overlay is.
So I need help with executing a javascript function on the overlay click, tips on how to make a counter query through GET method.
Thanks
<script type="text/javascript">
Shadowbox.init({
handleOversize: "resize",
overlayOpacity: 0.9
});
When you bind your click handler to open the shadownbox, add a binding for an ajax call, such as this:
$.ajax({
type: "GET",
url: "stats.js",
data: "name=urlOrNameOfItem"
});
Replace urlOrNameOfItem with something meaningful so you can track what has been clicked. I assume you know in php how to handle a query string.
See JQuery docs: http://api.jquery.com/jQuery.ajax/
Before you display your Shadowbox throw an Ajax query to a php script which would save the current request in a db (including $_SERVER info for better analysis).
This PHP script can fetch the current count of views for that image from the Db and update it accordingly.
I'm guessing the shadowbox function is called as a onclick event on your image so just add the Ajax call something like this:
$.ajax({
url: 'path-to-counter-script.php?i='+image-identifier,
success: function() {
//display shadowbox
}
});

Categories