How can I use AJAX to run a PHP file when the page is loaded, I'm trying to create a small chrome extension and need to run my PHP file however nothing seems to be working I have this so far
var req = new XMLHttpRequest();
req.open(
"GET",
"my_php_file.php",
true);
req.send(null);
I solved with jquery...
(put at the bottom of your page)
<script type="text/javascript" src="./jquery-1.3.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#container-id-to-load-into').load('file-to-load.php);
});
</script>
I tried a couple solutions on my own that didn't work b/c I needed to do multiple calls on page load and finally went with the JQuery solution I could find.
Hope that helps...
Related
so, I have a script like this
<script>
$(document).ready(function() {
$('#lista').load('gifs/1.php').fadeTo( "slow" , 1);
});
</script>
<script src="arqs/main.js"></script>
<ul id="lista"></ul>
main.js:
$(document).ready(function() {
var DOMarr=$('#lista li').map(function(){return this;});
var clip = new ZeroClipboard(DOMarr);
})
the problem is, zeroclipboard is not "injecting" the flash file when I use .load(), if I use php include it works normally but when I switch to load it does that :(
does someone knows a workaround?
I'll need to use load to switch the list, and I'd like zeroclipboard to allow copying from the new list
.load is an asychronous call so .ready may be executed before. Make sure the list is loaded before doing the ZeroClipboard call.
I am trying to load the output of a PHP script into a using JavaScript and JQuery. The JavaScript function I am using uses the $.get function in JQuery to call a php script, which I want to display in another division. The script I wrote is:
<script type="text/javascript">
function on_load() {
$(document).ready(function() {
alert('here');
$.get("http://localhost/dbtest.php", function(data){
alert('here too too');
$("uname").html(data);
});
});
}
</script>
The PHP script (dbtest.php) uses a simple echo statement:
echo "hello, world!!!";
I am getting the first alert here, but not the second. What Can I be doing wrong here?
I suppose uname is a ID, in that case you should use:
$("#uname").html(data);
You can add this to your php for debugging:
error_reporting(E_ALL ^ E_NOTICE);
ini_set("display_errors", 1);
Try also to remove http:// from your ajax call and use relative path instead.
The guys below pointed out that the selector is wrong. Indeed that's a problem, but I think that the real issue is that you don't get the second alert. Probably your php file localhost/dbtest.php is not accessible. What happen if you open localhost/dbtest.php in a new tab?
I think the problem is the path to your dbtest.php file. You are saying you seecond alert will not be displayed so your request must be wrong.
Try to copy your page into the same folder like dbtest.php open the page in your browser with http://localhost/yourfile.php
If this does not display both alert-boxes try the same with an open developer console (Chrome/IE = F12) and look if there are errors.
You say you are trying to make ajax requests to your localhost from a Phonegap application. Phonegap prevents making ajax requests to other domains by default. You must add localhost to whitelist. Here is more detail: http://docs.phonegap.com/en/1.9.0/guide_whitelist_index.md.html
Here you have a working example:
<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<div id="uname"></div>
<script>
function on_load() {
$(document).ready(function() {
alert('here');
$.get("http://localhost/dbtest.php", function(data){
alert('here too too');
$("#uname").html(data);
});
});
}
on_load();
</script>
Beware of the same-origin-policy. The page loading dbtest.php must be from the same origin, unless you grant other origins by adding a header from dbtest.php.
Try add some error handling to your code to better see what´s happening.
<script type="text/javascript">
function on_load() {
$(document).ready(function() {
alert('here');
$.get("http://localhost/dbtest.php", function(data){
alert('here too too');
$("uname").html(data);
}).fail(function () {
alert("failed");
});
});
}
</script>
This would be a common question but what I'm looking for is:
My PHP Script does:
Read a remote page using cURL
Update every 20 Seconds
I want to auto update a div(Not whole page), (Which is populated using cURL) every 20 seconds.
I've read many solutions but that doesn't show updated data in source code (Crawl-able form).
Pls suggest me a solution how to update a div with cURL updated data, and that should populate/include in my page's source code.
Let me know if anything is unclear. sorry for bad english :(
Copy your cURL PHP codes into a new file named "reloader.php", in your main page, also put the source codes which reads the data(cURL stuff) in a div "id = to_be_reloaded", in your main page add these:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/
libs/jquery/1.8.2/jquery.min.js"></script>
<script type="text/javascript">
var auto_refresh = setInterval(
function ()
{
$('#to_be_reloaded').load('reloader.php').fadeIn("slow");
}, 20000); // refresh every 20000 milliseconds(20 seconds)
</script>
I know you need it for commenting system and
The workaround is use setInterval like this:
<script type="text/javascript">
setInterval(function(){
$.ajax({
url:'PUT YOUR URL',
success:function(data){
$('#comment').append($(data).fadeIn());
}
});
}, 20000);
</script>
Anything else ...
good luck
Forgive the simplistic question. I'd like to pass a variable from javascript to php and I understand this can be done with an xmlhttprequest object. So I built a test script to figure out how to do this.
I'm using XAMPP and I placed the following files, main.php and test.php into their proper location in the htdocs folder.
main.php tries to pass a variable to test.php using an xmlhttprequest. test.php simply echos the variable. I then try and display the echoed result in main.php.
When I load main.php in a browser and view source, I expect to see the variable echoed. Instead I see an error message that the variable that was supposed to be passed to test.php is undefined.
Any help would be most welcome.
<html>
<head>
<title>Main</title>
<script type="text/javascript" src="../scripts/jquery-1.7.1.min.js"></script>
<script language="javascript" type="text/javascript">
$(document).ready(function () {
xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET","test.php?var1=hello", true);
xmlhttp.send(null);
<?php include("test.php"); ?>
});
</script>
</head>
<body></body>
</html>
Here is the code in test.php which resides in the same folder as main.php
<?php
$name=$_GET['var1'];
echo $name;
?>
I believe you're running the request in asynchronous mode, so check for ready state change:
xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET","test.php?var1=hello", true);
xmlhttp.send(null);
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4) {
alert(xmlhttp.responseText)
}
I noticed that it looks like you're using jQuery (if it is Sizzle, ignore the rest of this),
so you don't need to use that! jQuery has built in Ajax methods that simplify it greatly!
$.ajax({
url:"test.php",
method:"get",
data:{
var1:"hello"
}
}).done(function(data){
alert(data);
});
Will create an XMLHttpRequest and send it.
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.