Ajax request for PHP loop number - php

I'm currently trying to get a PHP loop status and update a HTML5 progress bar.
The loop is triggered by a button click and set to 5000 with 1sec sleep() after every execution. I of course also made an Ajax request to which reuqests the current loop number every second and update the progress bar. However the request always waits until the loop is completed and shows me a number like "1234567891011..."
This is my JQuery code which is very simple because it's only for testing and learning purpose
function getStatus() {
$.ajax({
url : 'ajax.php',
dataType: "html",
type: "POST",
data: {request: 'request',},
success: function( data ) {
//Call function to update status on the loading bar
updateBar(data);
}
});
//Update loading bar
function updateBar(data) {
$('progress').attr('value', data);
}
}
function setGo() {
$.ajax({
url : 'ajax.php',
dataType: "html",
type: "POST",
//async: false,
data: {status: 'GO',},
success: function( data ) {}
});
}
$('#start').click(function(event) {
setGo();
setInterval(function() {
getStatus();
}, 1000);
});
This is my php Code
<?php
//Overwrite php execution limit
set_time_limit(120);
if($_POST['status'] = 'GO') {
$number = 5000;
$counter = 0;
for($i=0; $i != $number; $i++) {
$counter++;
sleep(1);
if(isset($_POST['request'])) {
echo $counter;
}
}
}
?>
I tried a lot of different ways and read a lot of posts but somehow nothing worked for me.
Hope someone got an idea.

Take a good look at your "echo $counter;" part in your PHP script, that's in a (5000 times) loop.

After the PHP code is interpreted and executed, the web server sends resulting output to its client, usually in form of a part of the generated web page – for example, PHP code can generate a web page's HTML code
From PHP's wiki page
This basically means that the whole php file will always be executed before returning the output. The loop will be executed 5000 times before returning the output.

Related

Multiple Ajax call with same JSON data key calling one php file

I am trying to validate list of dynamic text fields.
Validation needs an AJAX call to interact with server.
At the backend I have written just one php file that reads the input request data and performs operation. Below is the example.
abc.js
row_count = 6
for (i = 1; i <=row_count; i++) {
id = "#val"+i.toString() ;
$(id).change(function(){
input_val="random";
$.ajax({
url:"url.php",
type:post,
async:true,
dataType: 'json',
data : {temp:input_val},
success:function(result){},
error: function (request, status, error) {}
});
});
}
url.php
<?php
$random_val = $_POST['temp'];
$cmd = 'systemcommand '.$random_val;
$flag = exec($cmd);
if ($flag == 0){
echo json_encode(array("status"=>'Fail'));
}
else{
echo json_encode(array("status"=>'Success'));
}
?>
It works fine when the row_count = 1 (Just one text field) but fails when the input is more than 1.
When the count is more than 1, the php script is not able to read the request data(The key in JSON data "temp"). it is blank in that case.
Any lead or help should be appreciated.
Thanks
Your javascript bit needs some adjusting, because you do not need to define an ajax for every single element. Use events based on a class. Also, since input behave differently than select, you should setup two different event class handlers.
function validateAjax ( element ) {
var input_val = element.val();// get the value of the element firing this off
$.ajax({
url: "url.php",
type: 'post',
async: true,
dataType: 'json',
data : { temp: input_val },
success: function(result) {
// check your result.status here
},
error: function (request, status, error) { }
});
}
$(".validate_change").on("change",function() { // for selects
validateAjax( $(this) );
});
$(".validate_input").on("input",function() { // for text inputs
validateAjax( $(this) );
});
And for your select or input you add that appropriate class.
<select class="validate_change" name="whatever"><options/></select>
<input class="validate_input" name="blah">
PS
I really worry about this code you have:
$cmd = 'systemcommand '.$random_val;
$flag = exec($cmd);
So, you are just executing anything that is coming in from a webpage POST var??? Please say this website will be under trusted high security access, and only people using it are trusted authenticated users :-)

AJAX how to grab returning value AND database connections

I am quite new to jQuery and AJAX.
I am sending the request via AJAX to check file 'test' every 5 seconds which works fine. 'test' stands for test.php file.
<script>
$(document).ready(function () {
function load() {
var test = "<?php echo $test; ?>/";
$.ajax({ //create an ajax request to load_page.php
type: "GET",
url: test,
dataType: "html", //expect html to be returned
contentType: "text/html",
success: function (response) {
$("#responsecontainer").html(response);
setTimeout(load, 5000)
}
});
}
load(); //if you don't want the click
// $("#display").click(load); //if you want to start the display on click
});
</script>
<div id="responsecontainer"></div>
test.php
$id = $this->session->userdata('UserID');
$get_friends_notification = $this->friends_model->get_friends_alert_by_ID($id);
if(isset($get_friends_notification) && !empty($get_friends_notification))
{
?>
<div id="test" style="width:200px; height:auto; border:1px solid red;">
<h3>Friend invitation from:</h3>
<?php
foreach($get_friends_notification as $key => $value)
{
$new_id = $get_friends_notification[$key] = $value["FrieInviter"];
$new_name = $get_friends_notification[$key] = $value["UserName"];
echo ''.$new_name.'<br />';
}
?>
</div>
<?php
}
Then it just displays it in the div # responsecontainer which works fine too.
$("#responsecontainer").html(response);
In the file 'test' I am checking database if there were any updates.
So I am pulling the information from DB and return to #responsecontainer. As it runs every 5 seconds, after it ran for the first time I would like to grab the last ID that I pulled and before it runs again and save it in variable and then I would like to pass that ID to the 'test' or process it differently. Basically I want to be able to use it. How can I do that??
EXAMPLE:
ajax checks test.php file and find 5 rows. returns these 5 rows with 5 IDs. The last ID is number 5. In the meantime there were some other rows inserted so next time it will find more rows.
Before it checkes again I want to tell it to not to check ID 1,2,3,4,5 but start from ID 6.
Also how does this method works with DB connections? Assuming that I have for example 500 users, and on all of theirs profiles that check would run every 5 seconds wouldnt it kill database connections?
Basically you need to add a pgination effect in here. Pass a parameter in the ajax request for example : if you are getting 5 records at a time,
then initialize a variable say
current_page = 0 , increment the same as you request via ajax
var current_page=0;
function load() {
var test = "<?php echo $test; ?>/";
$.ajax({ //create an ajax request to load_page.php
type: "GET",
url: test,
data : current_page
dataType: "html", //expect html to be returned
contentType: "text/html",
success: function (response) {
if(response.length!==0){
$("#responsecontainer").html(response);
current_page+=1;
}
setTimeout(load, 5000)
}
});
}
in the php page make the necessary changes (hope you know how pagination is done).

Delay in populating session.upload_progress data

I'm trying to check the progress of files uploaded. I'm using the Kohana framework which has a Session class, but for the upload progress I'm using native PHP sessions. I'm calling session_start() in Kohana's bootstrap.php, which means session_start() will be called on every page request.
After the upload form is submitted, I wait 1 second and then begin calling a PHP file to check the upload progress using jQuery $.ajax().
The problem is that $_SESSION[$key] ($key contains the key for the upload data) isn't set on the first call to the PHP. I've tried debugging this quite a bit, and session_id() returns the correct session ID, so the session is definitely the right one and is active. I'm also waiting 1 second before checking the upload progress, so it's not a timing issue. I could fix this by continuing even if $_SESSION[$key] is not set, but the way to check if the upload is complete is when $_SESSION[$key] is unset.
The HTML form is created on-the-fly with jQuery because this is a multi-file upload. Here's the HTML for a generated form:
<form action="ajax/upload" id="form-HZbAcYFuj3" name="form-HZbAcYFuj3" method="post" enctype="multipart/form-data" target="frame-HZbAcYFuj3">
<iframe id="frame-HZbAcYFuj3" name="frame-HZbAcYFuj3"></iframe>
<input type="hidden" name="PHP_SESSION_UPLOAD_PROGRESS" value="HZbAcYFuj3">
<input type="file" id="file-HZbAcYFuj3" name="photo" accept="image/jpeg,image/pjpeg,image/png,image/gif">
<button type="button">+ Select Photo</button>
</form>
Here's the PHP that the JavaScript calls to check the progress:
public function action_uploadprogress()
{
$id = isset($_POST['id']) ? $_POST['id'] : false;
if (!$id)
throw new Kohana_HTTP_Exception_404();
$progress = 0;
$upload_progress = false;
$key = ini_get("session.upload_progress.prefix") . $id;
if (isset($_SESSION[$key]))
$upload_progress = $_SESSION[$key];
else
exit('100');
$processed = $upload_progress['bytes_processed'];
$size = $upload_progress['content_length'];
if ($processed <= 0 || $size <= 0)
throw new Kohana_HTTP_Exception_404();
else
$progress = round(($processed / $size) * 100, 2);
echo $progress;
}
Here's the jQuery ajax() request:
this.send_request = function()
{
$.ajax(
{
url: 'ajax/uploadprogress',
type: 'post',
dataType: 'html',
data: { id: _this.id },
success:
function(data, textStatus, jqXHR)
{
if (textStatus == "success")
{
if (data < 100)
setTimeout(_this.send_request, 1000);
}
}
}
);
};
You are sending a POST called 'id' to the PHP script.
However, the documentation says that the upload progress will be available only when you send a POST with same name as session.upload_progress.name configured in php.ini.
So, in other words, if your session.upload_progress.name is set to default value (PHP_SESSION_UPLOAD_PROGRESS), you have to change the following line, in send_request function:
Change:
data: { id: _this.id }
To:
data: { PHP_SESSION_UPLOAD_PROGRESS: _this.id }
You also have to change the $_POST['id'] to $_POST['PHP_SESSION_UPLOAD_PROGRESS'] or $_POST[ini_get("session.upload_progress.name")] in the PHP script (and the name of input too in case it's not default).
The upload progress will be available in the $_SESSION superglobal when an upload is in progress, and when POSTing a variable of the same name as the session.upload_progress.name INI setting is set to. When PHP detects such POST requests, it will populate an array in the $_SESSION, where the index is a concatenated value of the session.upload_progress.prefix and session.upload_progress.name INI options. The key is typically retrieved by reading these INI settings, i.e.
Source: http://php.net/manual/pt_BR/session.upload-progress.php
Lets see if I can get some of that sweet sweet bounty.. My first thought is that the $key string is not getting set properly.
Try echoing its value out and doing a print_r on the entire $_SESSION variable to keep track of things.
Now I don't see a 'success' output from action_uploadprogress() at all. I see 100 which I guess indicates done but you aren't checking for that in js. I would recommend looking into that. Might as well echo out your calculations as well. I assume its very unlikely but make sure that you are uploading files properly and are able to determine their current size without any issue.
Another issue could be with how you are handling ajax with jquery. I'm not 100% sure about this but I think the success option has been depreciated (1.5+).
From : http://api.jquery.com/jQuery.ajax/
$.ajax({
type: "POST",
url: "some.php",
data: { name: "John", location: "Boston" }
}).done(function( msg ) {
alert( "Data Saved: " + msg );
});
The only way I've seen success being used is like this....
$.ajax({
type: "POST",
url: '/login/spam',
data: formData,
success: function (data) {
if (data == 'value') {
//Do stuff
}
}
});
However I could be completely wrong about this....your setup might be perfectly fine. What I want you to do is get is directly in the success/done function is
alert(data);
alert(textStatus); //if you still use it
This will tell you if you are getting a proper response from your ajax query.
I will check back a few times tonight and I'll be around tomorrow to help. Tell me if anything I said helps anything.

Load only new data with ajax

I need to load only new data into my div with ajax. At the moment I'm currently loading all data, because if I delete a record in the database it also removes it from my chat div.
Here is my js code:
var chat = {}
chat.fetchMessages = function () {
$.ajax({
url: '/ajax/client.php',
type: 'post',
data: { method: 'fetch', thread: thread},
success: function(data) {
$('.chat_window').html(data);
}
});
}
chat.throwMessage = function (message) {
if ($.trim(message).length != 0) {
$.ajax({
url: '/ajax/client.php',
type: 'post',
data: { method: 'throw', message: message, thread: thread},
success: function(data) {
chat.fetchMessages();
chat.entry.val('');
}
});
}
}
chat.entry = $('.entry');
chat.entry.bind('keydown', function(e) {
if(e.keyCode == 13) {
if($(this).val() == ''){
} else {
chat.throwMessage($(this).val());
e.preventDefault();
}
}
});
chat.interval = setInterval(chat.fetchMessages, 8000);
chat.fetchMessages();
I have had a look around and some say that if you pass a timestamp to the server and load new content that way, but I can't seem to get my head around that. If you need php let me know.
Right, so the timestamp thing makes the most sense. You'll need to do a few things:
On the back end, you need to make client.php accept a timestamp parameter in the querystring. When returning data, instead of just returning all of it, make it return everything since the time stamp, if given. Otherwise return everything.
The very first time you load the chat client, the first thing you should do is make an Ajax call to a new PHP file that returns the current server timestamp. Store the value of that in a Javascript variable as a Number.
During chat.fetchMessages(), increment the value of the timestamp variable by however long it's been since the last fetch (looks like 8000 milliseconds), and feed that to client.php, like url: '/ajax/client.php?timestamp=' + latestFetchTimestamp,
Instead of replacing all HTML content, append instead.

How to handle multiple ajax requests

NOTE:
I gave up on trying to do the processing in one go, and just let it return after every x number of sends.
Two paths,
/sms?action=send
/sms?action=status
Let's say that the send path starts sending 10,000 sms messages via REST api calls.
I make a call to that page via ajax.
Then every few seconds, I make a call to /sms?action=status to see how the progress is going, and to update a progress bar.
The status path returns false if no messages are being sent.
What ends up happening is that the ajax call to the SEND path gets the ajax success: function called almost instantly, even though I know the script is taking 1+ minute to complete execution.
My progress bar never gets shown because the status ajax call (which is in a set interval with a few second delay) never seems to actually get called until the send call completes.
I'm trying to put the relevant code in here, but it may not be as clear as it should be without all the context.
<script type="text/javascript">
var smsInterval = 0;
var smsSending = false;
$(document).ready(function() {
var charCount = 0;
var smsText = "";
var smsTotal = <?php echo $options["smsTotal"]; ?>;
<?php if($options["sending"]): ?>
smsStatus();
smsSending = true;
smsInterval = setInterval("smsStatus()", 5000);
<?php endif; ?>
$("span#smsadmin_charcount").html(charCount.toString());
//send button
$("div#smssend").click(function() {
if(smsSending == true) {
return false;
}
smsStatus();
var dataString = $("#smsadmin_form").serialize();
smsSending = true;
$("div#smssend").html("Sending...");
$.ajax({
type: "POST",
url: "<?php echo $base_url; ?>/admin/sms",
data : dataString,
success: function(data) {
},
error: function(request, error) {
$("div.notice.sms").html("ERROR "+error+ "REQUEST "+request);
}
});
});
});
function smsStatus() {
var dataString = "smsaction=status&ajax=true";
$.ajax({
type: "POST",
url: "<?php echo $base_url; ?>/admin/sms",
data : dataString,
success: function(data) {
//data being false here indicates the process finished
if(data == false) {
clearInterval(smsInterval);
var basewidth = $("div.sms_progress_bg").width();
$("div.sms_progress_bar").width(parseInt(basewidth));
$("div.sms_progress_notice").html(parseInt(100) + "% Complete");
smsSending = false;
$("div#smssend").html("Send To <?php echo $options["smsTotal"]; ?> Recipients");
} else {
var pcomplete = parseFloat(data);
$("div.sms_progress_bg").show();
var basewidth = $("div.sms_progress_bg").width();
$("div.sms_progress_bar").width(parseInt(basewidth * pcomplete));
$("div.sms_progress_notice").html(parseInt(pcomplete * 100) + "% Complete");
}
},
error: function(request, error) {
$("div.notice.sms").html("ERROR "+error+ "REQUEST "+request);
}
});
}
I might be missing the point, but inside the $("div#smssend").click you got this line:
smsStatus();
shouldn't it be:
smsInterval = setInterval("smsStatus()", 5000);
and INSIDE the success: function(data) for /admin/sms ?
If the send part is sending out 10k messages, and the status returns true if currently sending a message, and false if in between sending, then you have a design issue.
For example, what is status supposed to be showing?
If status is to show how many of a certain block have been sent, then what you can do is to submit the message to be sent (or addresses), and get back some id for that block.
Then, when you ask for a status, pass the id, and your server can determine how many of that group has been sent, and return back the number that were successful, and unsuccessful, and how many are still pending. If you want to get fancy, you can also give an indication how much longer it may be before finishing, based on how many other requests are also pending.
But, how you approach this really depends on what you expect when you ask for the status.

Categories