1

I am using Ajax for Bootstrap Modal that pulling content from Database. I have a function in my functions file that check if a remote file exists:

function checkRemoteFile($url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    // don't download content
    curl_setopt($ch, CURLOPT_NOBODY, 1);
    curl_setopt($ch, CURLOPT_FAILONERROR, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    if (curl_exec($ch) !== FALSE) {
        return true;
    } else {
        return false;
    }
}

I am trying to call the above function inside my ajax which is this:

$(document).on('click', '.show-detail', function (e) {
      e.preventDefault();
      $('#dynamic-content').html('');
      $('#modal-loader').show();
      $('#view-modal').modal();
      $.ajax({
            url: '',
            type: 'post',
            data: {GetData: 1, EmployeeID: $(this).data('id')},
            dataType: 'json'
      }).done(function (abc) {
      var number = '';
      $.each(abc[0].Phone, function (i, item) {
            number += '<p><span class="icon_phone" data-toggle="tooltip" data-placement="left" title="Phone/Mobile" /></span> ' + item.Phone + '' + (item.Extension !== "" ? ' Ext#: ' + item.Extension : '') + '</p>';
      });
      var email = '';
      $.each(abc[0].Email, function (i, item) {
            email += '<p><span class="icon_mail" data-toggle="tooltip" data-placement="left" title="Phone/Mobile" /></span>  <a href="mailto:' + item.Email + '">' + item.Email + '</a></p>';
       });
      var pic ='';
      $.each(abc, function (i, item) {
      if (checkRemoteFile('http://http://url.com/img/' + item.pictureCode+'.jpg?code=xxxx')){
      pic += '<img src="http://http://url.com/img/'+ item.pictureCode+'.jpg?code=xxxx" width= "50px" height= "50px" />';
      } else {
      pic += '<img src="assets/img/avatars/unknown-user.png" width= "50px" height= "50px" />';
      }  
      });

      var temp = '<div class="row">\n\
                        <div class="col-md-6">\n\
                              <p> '+ pic + '\n\
                              <h4>' + abc[0].FullName + '</h4>\n\
                              ' + number + '\n\
                              ' + email + '\n\
                        </div>\n\
                  </div>';
      $('#dynamic-content').html(temp);
      $('#modal-loader').hide();
      }).fail(function () {
      $('#dynamic-content').html('<i class="glyphicon glyphicon-info-sign"></i> Something went wrong, Please try again...');
      $('#modal-loader').hide();
      });
});

I am trying to call the function using:

var pic ='';
$.each(abc, function (i, item) {
          if (checkRemoteFile('http://url.com/img/' + item.pictureCode+'.jpg?code=xxxx')){
 pic += '<img src="http://url.com/img/'+ item.pictureCode+'.jpg?code=xxxx" width= "50px" height= "50px" />';
  } else {
  pic += '<img src="assets/img/avatars/unknown-user.png" width= "50px" height= "50px" />';
  }  
  });

but its not working

Rotimi
  • 4,783
  • 4
  • 18
  • 27
Fahad
  • 113
  • 2
  • 15
  • 1
    This is never going to work. The function checkRemoteFile() is a php function and you are calling it via javascript. Why dont you just put the function checkRemoteFile in a file, save it and then make an ajax call to that php file – Rotimi Mar 28 '17 at 07:56
  • Possible duplicate of [What is the difference between client-side and server-side programming?](http://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – hassan Mar 28 '17 at 07:56
  • Your console should be screaming with errors! – Rotimi Mar 28 '17 at 07:58
  • URL is empty ! fill PHP script path – 4EACH Mar 28 '17 at 07:59
  • this is the error i am getting `Uncaught ReferenceError: checkRemoteFile is not defined` – Fahad Mar 28 '17 at 07:59
  • @4EACH when i fill the php path I will get the error message I have set in the fail section of the ajax – Fahad Mar 28 '17 at 08:02
  • @FahadAlt -- you should read the linked post above about client versus server side programming. You can't mix functions like this. – James J. Hill Mar 28 '17 at 08:07
  • It means that php script failed ! try to understand what the problem – 4EACH Mar 28 '17 at 08:10
  • Possible duplicate of [using jquery $.ajax to call a PHP function](http://stackoverflow.com/questions/2269307/using-jquery-ajax-to-call-a-php-function) – Masivuye Cokile Mar 28 '17 at 08:40

1 Answers1

0

You must use ajax to call server side (php function) function from client (javascript code).

jQuery Ajax POST example with PHP

Community
  • 1
  • 1
Anph
  • 163
  • 1
  • 14