Is it possible to call a php function on jquery ajax call.When i tried to do this i get function not defined error on php function
$.ajax({
type:"POST",
url:"x.php?z=" + id,
cache:false,
success: function(data)
{
<?php xcz(); ?>
}
});
$.ajax({
type:"POST",
url:"x.php?z=" + id,
cache:false,
success: function(data)
{
anotherFunction();
}
});
function anotherFunction(){
$.ajax({
type:"POST",
url:"anotherFile.php",
cache:false,
success: function(data)
{
//do something else;
}
});
}
That's not possible. PHP and javascript run on different computers. PHP runs on the server, and javascript runs in the browser. At the time the javascript is being executed, that server has already executed the PHP code, and has sent that the the browser.
The only way you can achieve such a thing is making an additional Ajax request.
Not possible. Because jQuery (javascript) runs on client side, php runs on server side.
When page loaded on browser, php's job is finished.
You can call just javascript's function like this approach.
Related
I'm trying to get echoed variable from php and display on the console. I'm using jquery to send data and I know the backend php file script is working fine. Since I want to see the progress and the backend php script echo out the iteration number while the script is running, I'm trying to see how I can get the echoed variable with jquery and display on the console.
I've tried success, onloading but it doesn't seem to work. Any help would be very appreciated. Thank you!
EDIT
$.ajax({
url: location,
type: 'POST',
data:{
iteration:"10",
readsize:"200",
slimdepth:"3",
sourcedirectory:source,
packagepath:MP
},
dataType:'json',
success: function (response) {
console.log("SUCCESS!");
console.log(response);
},
onInteractive:function (response) {
console.log(response.responseText);
},
complete:function (response) {
console.log(response.responseText);
},
error: function (response) {
console.log("Failure!");
console.log(response.responseText);
}
});
So this is how I send the data and backend php script gets the parameters without problems and it runs. Now I can't see anything until php is done running and then it echo everything. In the backend php script, I have iteration number that I want to check and that is the problem since I can't check while it is running but only it is done running.
From what I could understand from your question probably this is what you are looking for.
You are sending data to PHP file with jQuery post or send:
$.post(file.php, {post}, function(data){
});
If you want to log returned data from php in console you must use console.log() function:
$.post(file, {post}, function(data){
console.log(data)
});
and now data will be logged into browser console.
I know how to use $.ajax. I have a Codeigniter project so I just call:
url:'<?php echo base_url()."somecontroller/somefunction"?>',
This is all ok but ajax waits for the response. I just want to the call the url as you would by typing it in your browser. I don't want to wait for the response because the controller does a redirect and then loads a view. Also i need to be able to send some data via POST.
How can I do this?
You can use the following for sending asynchronous request with additional parameters.
$.ajax({
type: "POST",
url: url,
data: data, // additional parameters
async:true,
success: function(response){
}
});
If you want the request to be synchronous, set async:false.
For post, type:POST
Refer http://api.jquery.com/jQuery.ajax/
Do you can try this ?
Change the (alert) function with your function
$.ajax({
url: 'test.html',
async: false,
success: function () { alert('hello people from the world'); }
});
Never use async: false.
Since Javascript runs on the UI thread, an async: false request will freeze the browser until the server replies
This is my code
<script>
$(document).ready(function() {
// delete the entry once we have confirmed that it should be deleted
$('.delete').click(function() {
var parent = $(this).closest('tr');
$.ajax({
type: 'POST',
url: 'delete.php',
data: 'ajax=1&delete=' + $(this).attr('id'),
success: function() {
parent.fadeOut(300,function() {
parent.remove();
});
}
});
});
$('.delete').confirm({
});
});
</script>
My question is why delete.php is not executing? It's in the same folder, do I need type absolure url to this file? But the best of it is that row in table is deleting, but php file is not executing.
And second question is why this code is not working without this line:
$('.delete').confirm({
});
In php file i got this to know is it executed:
<script>
alert('aaaa');
</script>
Or even I had change it on echo 'something'; but still not working
If the row is deleted the certainly the php file is executed and you have to check the url data as #Saifuddin has described.
use firebug console to see your result. and also you are not passing parameter to the success function
so change this
data: { ajax: 1, delete: $(this).attr('id')},
and also this
success: function(msg) {
alert(msg)
parent.fadeOut(300,function() {
parent.remove();
});
}
I assume that the php file is executed in your case, while better approach to define the url with absolute path like http://www.example.com/delete.php. however the way you pass data is not as per standard.
so replace this line.
data: 'ajax=1&delete=' + $(this).attr('id'),
with
data: { ajax: 1, delete: $(this).attr('id')},
and for debugging print $_POST variable in your php file.
hope it helps.
I would add an error handler to see what is happening with the ajax call, something like this:
error: function(jqXHR, textStatus, errorThrown) {
console.log(errorThrown);
}
Keep in mind that the ajax call gets the current directory preppended to the relative uri you set. I assume you are sending the request to a working web server, i.e. using the HTTP protocol.
There is no confirm() jQuery function as far as I know. You usually do:
if (confirm('Are you sure you want to delete this item?')) do_delete();
The confirm() method returns true when the user selects OK, and false otherwise.
Cheers.
I have some ajax script that fire off about 250 synchronous PHP calls . This is my script
$(document).ready(function(){
$("#generate").html("<div class='modal'><p>Initializing...</p></div>");
$.ajax({
url:'/fetch around 250 url from database.php',
async:false,
dataType: 'json',
success: function(data){
$.each(data,function(key,val){
$("#generate").html("<div class='modal'><p>Fetching "+val.url+"</p></div>");
saveimage(val.url);
}
$("#generate").html("<div class='modal'><p>done</p></div>");
finalcreate();
},
});
});
function saveimage(){
$.ajax({
url: 'do some php work.php',
async: false,
});
}
function finalcreate(){
$.ajax({
url: 'do some php work.php',
async: false,
});
}
In the first part script fetch more than 250 urls from database and for every url script do some php calculation using another ajax call. when the loop ends script do final ajax call.
When i run this programe in firefox, it run successfully for only 40 urls, then browser shows dialog box with option of whether user want to stop this script or not, if user want to run this script then the script run again for next 40 urls , same proccess occure till the end.
How i can optimize this script, i dont want browser show option to stop this script. Please help.
Thanks
Try this:
function nextrequest() {
if (requests.length == 0) {
$("#generate").html("<div class='modal'><p>done</p></div>");
finalcreate();
return;
}
var val = requests.pop();
$("#generate").html("<div class='modal'><p>Fetching "+val.url+"</p></div>");
saveimage(val.url);
}
var requests = [];
$(document).ready(function(){
$("#generate").html("<div class='modal'><p>Initializing...</p></div>");
$.ajax({
url:'/fetch around 250 url from database.php',
dataType: 'json',
success: function(data){
requests = data;
nextrequest();
},
});
});
function saveimage(){
$.ajax({
url: 'do some php work.php',
success: function(data) {
// do something...
nextrequest();
}
});
}
function finalcreate(){
$.ajax({
url: 'do some php work.php',
});
}
You store all the URLs in a global variable, and everytime a request is done, you get the next one, until all of them are consumed, (requests.length == 0), you call the final request.
This way the user can still do something else on the page, and you can display progress everytime a request is done. Also, a good thing is that you can make 2 calls at once, or more, to make the process faster.
Ajax call needs much time to complete, as it communicates with remote server. The slowest thing there is a query to the server. You should send one batch request with all data needed to the server, that should separate the data and handle it. Everything should be completed about 250 times faster.
make some time interval for each ajax request
success: function(data){
$.each(data,function(key,val){
$("#generate").html("<div class='modal'><p>Fetching "+val.url+"</p></div>");
setTimeout(saveimage(val.url),3000);
}
The ajax request is working but the problem I'm having is how to encrypt the data with my PHP function before the request is made?
Here is the JS I'm trying
$("#sensitive_data").blur(function() {
alert($(this).val());
$.ajax({
url: 'db.php?check=<?php echo $object->encrypting_data('+$(this).val()+') ?>',
type: 'POST',
error : function (){ document.title='error'; },
success: function (data) {
alert(data);
}
});
});
It works if I pass a static value in the php function
If I understand right, you're trying to use a PHP function to encrypt client-side data before it's sent to the server.
That's impossible, because you can't call a PHP function on the client-side. I'm also not sure what you want to accomplish.