I want to get a watingmessage in extjs while loading a link. The response is a binarycode, which I want to downlad. The link is for example "test.php".
function loadurl(link){
Ext.MessageBox.wait('Loading ...');
Ext.Ajax.request({
url: link,
callback: function(options, success, response){
Ext.MessageBox.updateProgress(1);
Ext.MessageBox.hide();
if (success) {
// response : my attachment
}
else {
}
},
scope: this
});
}
{
...
//functioncall
loadurl('test.php');
}
I also tried in test.php.
<?php
header('Content-Disposition: attachment; filename="'.$filename.'"');
echo $content;
?>
But it doesnt work. If I just load the link it works, but without waiting message.
In the ExtJS Documentation there is a class called LoadMask which is designed to show a loading 'spinner' along with a short message. In your case, you would use it like this:
function loadurl(link){
var mask = Ext.LoadMask(Ext.getBody(), {msg:"Loading..."})
mask.show();
Ext.Ajax.request({
url: link,
callback: function(options, success, response){
if (success) {
// response : my attachment
}
else {
}
//do whatever work we need to do, then hide the mask
mask.hide()
},
scope: this
});
However, if, for whatever reason, the callback comes back almost immediately, then it is possible that the mask will not be visible because your file loaded too fast. If this is an issue, you could force a delay by putting your Ajax request inside a setTimeout.
Related
After a JQuery AJAX call, all subsequent Slim redirects includes "X-Requested-With:XMLHttpRequest" in the request header. As a result, content of the redirected page is being returned in the background in the response, but browser is not redirecting to the intended URL. Not sure what I am doing wrong here.
My Ajax call is given below (this is an implementation of: https://developers.google.com/+/web/signin/server-side-flow):
function signInCallback(authResult) {
if (authResult['code']) {
// Hide the sign-in button now
$('#signinButton').attr('style', 'display: none');
// Send the code to the server
$.ajax({
type: 'GET',
url: '../google/',
contentType: 'application/octet-stream; charset=utf-8',
success: function(result) {
console.log(authResult['code']);
},
data: "gplustoken="+authResult['access_token'],
error: function (request, status, error) {
console.log("Error");
}
});
} else if (authResult['error']) {
// There was an error.
}
}
}
PHP Slim URL redirect code is given below:
$app->redirect($app->urlFor("home"));
For the context of above line, kindly see:
https://github.com/TheRosettaFoundation/SOLAS-Match/blob/master/ui/RouteHandlers/UserRouteHandler.class.php#L413
I tried to remove the 'X-Requested-With' from the request header in the PHP/Slim Code as below, but it did not work either.
$isAjaxRequest = $app->request->headers->get('X-Requested-With') == 'XMLHttpRequest';
if ($isAjaxRequest) {
if (isset($_SERVER['HTTP_X_REQUESTED_WITH']))
{
unset($_SERVER['HTTP_X_REQUESTED_WITH']);
//$app->request->headers->set('X-Requested-With','');
$app->redirect($app->urlFor("home"));
}
} else
$app->redirect($app->urlFor("home"));
Any help to resolve this issue is much appreciated.
Looking at the Slim docs for the Http Headers object, you should be able to achieve this by calling the remove method:
$app->request->headers->remove('X-Requested-With');
I am very new to PHP and Javascript.
Now I am running a PHP Script by using but it redirect to another page.
the code is
<a name='update_status' target='_top'
href='updateRCstatus.php?rxdtime=".$time."&txid=".$txid."&balance=".$balance."&ref=".$ref."'>Update</a>
How do I execute this code without redirecting to another page and get a popup of success and fail alert message.
My script code is -
<?PHP
$rxdtime=$_GET["rxdtime"];
$txid=$_GET["txid"];
$balance=$_GET["balance"];
$ref=$_GET["ref"];
-------- SQL Query --------
?>
Thanks in advance.
You will need to use AJAX to do this. Here is a simple example:
HTML
Just a simple link, like you have in the question. However I'm going to modify the structure a bit to keep it a bit cleaner:
<a id='update_status' href='updateRCstatus.php' data-rxdtime='$time' data-txid='$txid' data-balance='$balance' data-ref='$ref'>Update</a>
I'm assuming here that this code is a double-quoted string with interpolated variables.
JavaScript
Since you tagged jQuery... I'll use jQuery :)
The browser will listen for a click event on the link and perform an AJAX request to the appropriate URL. When the server sends back data, the success function will be triggered. Read more about .ajax() in the jQuery documentation.
As you can see, I'm using .data() to get the GET parameters.
$(document).ready(function() {
$('#update_status').click(function(e) {
e.preventDefault(); // prevents the default behaviour of following the link
$.ajax({
type: 'GET',
url: $(this).attr('href'),
data: {
rxdtime: $(this).data('rxdtime'),
txid: $(this).data('txid'),
balance: $(this).data('balance'),
ref: $(this).data('ref')
},
dataType: 'text',
success: function(data) {
// do whatever here
if(data === 'success') {
alert('Updated succeeded');
} else {
alert(data); // perhaps an error message?
}
}
});
});
});
PHP
Looks like you know what you're doing here. The important thing is to output the appropriate data type.
<?php
$rxdtime=$_GET["rxdtime"];
$txid=$_GET["txid"];
$balance=$_GET["balance"];
$ref=$_GET["ref"];
header('Content-Type: text/plain; charset=utf-8');
// -------- SQL Query -------
// your logic here will vary
try {
// ...
echo 'success';
} catch(PDOException $e) {
echo $e->getMessage();
}
Instead of <a href>, use ajax to pass the values to your php and get the result back-
$.post('updateRCstatus/test.html', { 'rxdtime': <?php ecdho $time ?>, OTHER_PARAMS },
function(data) {
alert(data);
});
I am here to ask that is there any settings argument in jQuery ajax() which let us to call a function when data has transferred to php file successfully not when the data has transferred and php file parsed the code and returned a value.
I have following code:
$.ajax({
..
..
..
success: function(data) {
// do something
}
});
But I want it like:
$.ajax({
..
..
..
onTransfer: function() {
// do something like show a div saying "Done".
},
success: function(data) {
// do something
}
});
You only get 1 answer from server side, what's wrong with success? If your process takes too long then you could return from script as soon as it get's started and show 'Processing your request' as result for success:
<?php
ob_end_clean(); // discard everything
header("Connection: close"); // send Connection close
ignore_user_abort(true); // ignore user abort
set_time_limit(0); // no time limit
ini_set("memory_limit","200M"); // setup a memory usage needed
ob_start(); // start output buffer
header("Content-Length: 0"); // send lenght 0
ob_end_flush(); // end and flush
flush();
// continue your script
Your script will continue to run while you receive a success answer from it.
You might use it like this
$.ajax({
url: "test.html",
context: document.body
}).done(function() {
$(this).addClass("done");
});
For more details please check following link
http://api.jquery.com/jQuery.ajax/
Well, another try:
this is all the jquery code i'm using maybe i made something wrong in the code before $.post(); i call the following function with the onclick of the same form...
function setLogin()
{
$('#login-form').submit(function(e) {
e.preventDefault();
//passing form field to vars
var formUsername=$("#login-form #username").val();
var formPassword=$("#login-form #password").val();
//checks on fields lenght
if((formUsername.length<6))
{
$("#ajax-output").html("<div class='error'>Attenzione username troppo breve!</div>");
}
else if((formPassword.length<6))
{
$("#ajax-output").html("<div class='error'>Attenzione password troppo breve!</div>");
}
else
{
$.post(
//the url
'?module=login',
//data got from login form
{
"username": formUsername,
"password": formPassword,
},
//response
function(data){
$("#ajax-output").html(data.reply)
},
//type
"json"
);
}
});
}
i tried with only this code in php file and it still doesn't return anything...
function Login()
{
//just to try
echo json_encode(array('reply'=>'foo'));
}
it still doesn't work...
Are you sure the post is being run in the first place?
Use Firebug! (or chrome's built-in developer tools)
You can use firebug to pick apart every bit of a web page.
It has a "net" tab that shows every request that is made by the browser, including AJAX requests, and their results, headers and contents.
Use it to see if your requests is really being made, and what the result is. Then take it from there.
Make sure that you're setting a header for the content type when responding - the browser may not attempt to use the JSON if it doesn't know it's receiving JSON.
function Login()
{
header('Content-Type: application/json');
echo json_encode(array('reply'=>'foo'));
}
I am building a registration form with jquery which should handle the request via Ajax.
My code:
$.ajax(
{
type: "POST",
url: "http://www.example.com/register.php",
data: $("#register-form").serialize(),
cache: false,
success: function(message)
{
$("#reg_notification").html(message).fadeIn(400);
}
});
Now the register.php should either output an error if for example the email address is not valid and this will be put into the div reg_notification. If no error is made, then the user should be redirected to a "welcome page". But it is important that this page is not allways static, so in other words, I can not use a location.href in the jquery code but want to use
header("Location: http://www.example.com")
in PHP. The problem is that the user wont be redirected but the content of www.example.com will be put inside the div reg_notification.
I found a similar problem here: How to manage a redirect request after a jQuery Ajax call but the difference is that there they are using json which I can not use due to my server and they are also redirecting inside the javascript and not in the php file.
Impossible with header redirection.
Use something like that:
...
success: function(message)
{
$("#reg_notification").html(message)
.fadeIn(400, function() {window.location = "url"});
}
or
success: function(response)
{
if (response.success) {
$("#reg_notification").html(response.message)
.fadeIn(400, function() {window.location = response.redirectTo; } );
} else {
// somethings else
}
}
in php:
header("Content-type: application/json");
echo json_encode(array(
"success" => true,
"message" => "some message",
"redirectTo" => "my url"
));
UPDATE
header redirect impossible because XMLHttpRequest transparently follows to redirect url for get result. See there