I have this php script:
<?php
echo "Welcome!<br>";
sleep(3);
echo "Something else<br>";
sleep(3);
echo "Something else 2<br>";
?>
How can I do with jQuery to display results on real time or almost real time without waiting the whole script to finish, for example checking every seconds for new results until the script stop loading?
What if I have a php loop like this one:
<?php
while($x<10) {
echo "Hi<br>";
$x++;
}
?>
Can I do the same on this case?
This is my actual jQuery, but it wait until php script is complete:
<script>
$(document).ready(function() {
$('#submit').click(function() {
event.preventDefault();
$.ajax({
type: 'POST',
url: 'index.php',
data: { url : $('#url').val() },
success: function(data) {
$("#div").append(data);
}
});
});
});
</script>
You could make 3 different PHP scripts and write an AJAX call for each one
If I understand you right, you should check out this comment at the php docs:
http://php.net/manual/de/function.flush.php#76039
This will send content to the browser before finishing the script.
Related
How call and run Ajax every X minutes without the page being open (client-side), running in the background on the server side.
My Code in Header.php:
(function($) {
function updateGo() {
$(window).load(function() {
$.ajax({
url: '/test.com/template/test/script.php',
dataType: 'html',
cache: false,
success: function(data) {
if (data) {
$('body').append(data);
}
}
});
})
}
updateGo();
setTimeout(updateGo, 60 * 1000);
})(jQuery);
In script.php there are some functions and codes jquery inside and it works perfectly, but only works when I enter the page (client-side), in case I am trying to make the functions and codes of this script always run without having to enter the page.
If you want to Call and Run Ajax every X minutes jQuery you can try the function setInterval doc like:
(function($) {
function updateGo() {
$(window).load(function() {
$.ajax({
url: '/test.com/template/test/script.php',
dataType: 'html',
cache: false,
success: function(data) {
if (data) {
$('body').append(data);
}
}
});
})
}
updateGo();
// for this example we take 3000 milliseconds
let interval = 3000;
setInterval(updateGo, interval);
})(jQuery);
so it's work when you load your page in your browser.
NB: in the background on the server side, we can't use Ajax, you must try to add cronJob little documentation about
I hope it's help you
I have a shell script which is excecuted in a php file to get the temperature.
<?php
$output = shell_exec('sh temperature.sh');
echo "<pre>$outputĀ°C</pre>";
?>
Right now it is only excecuted once.
How do I need to change the php file so the Value is always up to date?
Thanks
PHP is a server side language. that means that when its loaded, its done.
if you want to update it always, you should use ajax calls for that.
var interval = 1000; // 1 second
function doAjax() {
$.ajax({
type: 'GET',
url: 'get_what_you_want.php',
data: {},
dataType: 'json',
success: function (data) {
$('body').text(data) // or put it on your page
},
complete: function (data) {
// Schedule the next
setTimeout(doAjax, interval);
}
});
}
setTimeout(doAjax, interval);
I am dynamically generating a web page via scriptA.php. Within this page,
I have a div element #WatchContainer that needs to be updated every 5 minutes. The content for #WatchContainer is created by scriptB.php. To accomplish this, I have used "include" to embed scriptB.php in scriptA.php. The variable $sum is defined in scriptA.php and used by scriptB.php to update the content in #WatchContainer.
On initial page load, $sum is correctly passed from scriptA.php to scriptB.php. However, when #WatchContainer is updated via AJAX request, $sum is no longer passed to scriptb.php. The jQuery function is as follows:
function updateWatch() {
$.ajax({
url:"scriptB.php",
success: function(data) {
$("#WatchContainer").html(data);
}
});
}
var WatchInterval = setInterval("updateWatch()", 300000);
i don't see you passing any variable to scriptB.php via ajax.
i thinnk you should pass your $sum variable to scriptB.php via ajax using POST OR GET method
POST
function updateWatch(sum) {
$.ajax({
type:"POST",
url:"scriptB.php",
data:"sum="+sum,
success: function(data) {
$("#WatchContainer").html(data);
}
});
}
GET
function updateWatch(sum) {
$.ajax({
type:"GET",
url:"scriptB.php?sum="+sum,
success: function(data) {
$("#WatchContainer").html(data);
}
});
}
Try this with 3 seconds interval time, and make sure you include the library and other related stuff.
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function() {
var WatchInterval = setInterval(function() {
$.ajax({
url:"scriptB.php",
success: function(data) {
console.log(data);
}
});
},3000);
});
<script>
Ok so I am trying to echo an function from jQuery inside of PHP.
I was searching a little bit, I did not found any exact answer but I found that I need to use ajax for it ?
I am not familiar with ajax so I have found this.
<?php
$status = "<script>
$.ajax({
url: '/',
data: {action: 'test'},
type: 'post',
success: function(output) {
notifyBox();
}
});
</script>";
?>
I also have this part somewhere on the page:
<?php if(isset($status)) { echo $status; } ?>
I am not quite sure if I need all of that to execute the function ?
But it works only if I put jQuery in head of the website.
I usually put all my scripts above </body> (at closing tag) and now it bothers me if I have to put all these scripts into head
Because of many pages I cannot separate all scripts now just cause of one page that bother me at the moment.
Can anyone please tell me, how do I call that function without getting console error of undefined '$' because jQuery is loaded at the end of the page.
Thank you in advance.
That should work so long as you make sure that you put your PHP if statement after the <script> tag that includes jQuery.
The problem is being caused by jQuery not being loaded when that variable is echoed. When Javascript sees a function call, it executes it right then and there. To prevent this, you will need to echo your variable after jQuery has been loaded.
Example:
<script type="text/javascript" src="path/to/jquery.js"></script>
<?php
echo $script;
?>
OR, you can put your AJAX call into a function that will be called later:
<?php
$script = '<script type="text/javascript">function myFunc() {
//Call AJAX here
}
</script>';
echo $script;
?>
Then later, you can call your function: myFunc();
Make sure jquery is loaded. This should work:
$status = "<script type='text/javascript'>
$(function(){
$.ajax({
url: '/',
data: {action: 'test'},
type: 'post',
success: function(output) {
notifyBox();
}
});
});
</script>";
$(document).ready() is also a jquery function, hence, try it with window.onload.
i have a jquery ajax post that for some reasons doesn't work:
<script>
var callback = function(data) {
if (data['order_id']) {
$.ajax({
type: 'POST',
url: '<?php echo $_SERVER['PHP_SELF']; ?>',
data: { myid: 123456 },
success: function(data) {
alert("Transaction Completed!");
}
});
}}
</script>
<?php if ($_POST['myid']) { echo $_POST['myid']; } ?>
the 'callback' functions works fine(i test it), just that it stops at the ajax post
and i cant see my echo's
any ideas on what i am doing wrong?
thanks
edit:
i edited the script a bit at the point where the ajax is posting successfully but i cant get the php to echo anything
If the AJAX - Call is succeeding now, you can't just echo anything with PHP. The data is sent to the client, but PHP is interpreted at the server. You're not sending an HTTP - Request anymore (which is pretty much the point of an AJAX-Call), so PHP is not going to do anything at this point.
You have to add your new content to the DOM with JavaScript. Try this and see if you get the message shown on your page. I append it to the body, because I don't know how your Markup and your returned data looks like:
$.ajax({
type: 'POST',
url: '<?php echo $_SERVER['PHP_SELF']; ?>',
data: { myid: 123456 },
success: function(data) {
alert("Transaction Completed!");
$('body').prepend('<p>Successful AJAX - Call</p>');
}
});
Then you can take a look at your data-variable with console.log(data), access the returned data and modify the DOM via JavaScript.
ok, for a start.
writeback("Transaction Completed!";
fix it to:
writeback("Transaction Completed!");
you have a trailing comma after 123456
data: { myid: 123456, },
you're missing a closing } to your callback function