How to call a PHP function via onClick.

How to call a PHP function via onClick.

dinobernaldinobernal Posts: 6Questions: 0Answers: 0
edited September 2011 in General
Is it possible to call a PHP function via click()? Below is a portion of the code.

jQuery(document).ready(function() {
var oTable = jQuery('#syslog-list').dataTable({
bProcessing: true,
sAjaxSource: '%getUrl',
bJQueryUI: true,
sPaginationType: 'full_numbers',
iDisplayLength: 100,
bFilter: true,
bAutoWidth: false,
bProcessing: true,
bServerSide: true,
bFilter: true,
aoColumns: [
{sWidth: '160px', bSearchable: false, bSortable: false},
{bSearchable: true, bSortable: false}
],
fnFilter: function(value, index) {
},
fnServerData: function ( url, data, callback ) {
jQuery.ajax({
"url": url,
"data": data,
"success": callback,
"dataType": "json",
"cache": false,
"error": function (xhr) {
if (!xhr || xhr.status != 401) {
alert( "DataTables warning: JSON data from server failed to load or be parsed. "+
"This is most likely to be caused by a JSON formatting error." );
} else {
alert('Your session expired !');
window.location.href = '%loginUrl';
}
}
});
}
});
oTable.fnSetFilteringDelay(400);
oTable.fnFilter('[' + jQuery('#log_date_from').val() + ',' + jQuery('#log_date_to').val() + ']');
$('.dataTables_filter input').keyup(function(){
$('#log_search').val($('.dataTables_filter input').val());
});

jQuery('.dataTables_filter').hide();

jQuery('#log_search_combined').click(function() {
var sFilter = '[' + jQuery('#log_date_from').val() + ',' + jQuery('#log_date_to').val() + '] ';
if (jQuery('#fromhost').val()) {
sFilter = sFilter + 'FROMHOST="' + jQuery('#fromhost').val() + '"';
}
if (jQuery('#syslogtag').val()) {
sFilter = sFilter + ' SYSLOGTAG="' + jQuery('#syslogtag').val() + '"';
}

if (jQuery('#searchstring').val()) {
sFilter = sFilter + ' MESSAGE="' + jQuery('#searchstring').val() + '"';
}

oTable.fnFilter(sFilter);
$('#log_search').val($('.dataTables_filter input').val());
});

});

=============================

I'm trying to add a click() function that will call a PHP function that will process the current filtered data and output it to a file.

Replies

  • fbasfbas Posts: 1,094Questions: 4Answers: 0
    only in the sense that you can make an AJAX call to a script and get the results.
  • dinobernaldinobernal Posts: 6Questions: 0Answers: 0
    So basically, I have to change the script -> "sAjaxSource: '%getUrl',"?
    Problem I have there is when I do this, I can't prompt for the output filename (and target directory).
  • fbasfbas Posts: 1,094Questions: 4Answers: 0
    no, not necessarily. you can use javascript/jquery to fire any ajax call

    [code]
    $.ajax( sUrl, {
    //url: sUrl, // you can put the url here and skip it in the first parameter.. same thing)
    data: aData, // array of data (parameters), or string formatted as querystring
    dataType: "json", // xml, json, script, or html
    async: false, // if you want this to run asynchronously or not. default true

    error: function (jqXHR, textStatus, errorThrown) { }, // what to do in case of error
    success: function (data, textStatus, jqXHR) { } // what to do on success
    });
    [/code]

    http://api.jquery.com/jQuery.ajax/
  • dinobernaldinobernal Posts: 6Questions: 0Answers: 0
    I'm not that familiar with the .ajax() call.
    Does this mean that in the ajax call, I can prompt for where to save the file on my computer and not on the server? Because it is a report file.
  • fbasfbas Posts: 1,094Questions: 4Answers: 0
    Javascript won't let you save things to local filesystems. But I suppose you could trigger downloading an url which would prompt the user about where to save. Never done that, but seems like it would work.
  • fbasfbas Posts: 1,094Questions: 4Answers: 0
    Here's a javascript solution to triggering a download of a file (the file could be generated by your PHP on the backend, but this code is all front-end javascript).

    http://stackoverflow.com/questions/445774/correct-way-to-trigger-a-file-download-in-php

    Basically the author sets up an iframe and triggers the HTTP download on the iframe. I suppose iframe could be invisible.
This discussion has been closed.