1

I have been searching for almost an hour without finding proper example to fix my problem: I would like to call a PHP function (it's a simple unlink with the path given from a javascript function that's executed on page load). I am not good at all with AJAX and I would like to understand how to call directly the PHP function, contained in the index.php file, from the javascript code.

Here's what I have in my javascript code snippet:

var xmlhttp;

if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","/dev/templates/absolu/index.php?s=" + Math.random(),true);
//@unlink(listeImages[i].toString());
7
  • Take a look at performing an asynchronous HTTP request using jQuery Commented Jun 26, 2012 at 13:56
  • The xajax project allows you to define functions in php then call them using javascript. I think it's just what you need. Commented Jun 26, 2012 at 13:58
  • 2
    All answers seem to be focused on GET but I would recommend switching to POST for a delete action. Commented Jun 26, 2012 at 14:02
  • POST won't bring anything more to this case.. Commented Jun 26, 2012 at 14:07
  • 1
    @jeroen I never recommend an ORM when someone asks how to connect to a mysql database :) Commented Jun 26, 2012 at 14:15

3 Answers 3

5

You send the function name (in case you will have more functions in the future) and params as get params

var fileToDelete;
var xmlhttp;

    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.open("GET","/dev/templates/absolu/index.php?s=" + Math.random()+"&action=delete&file="+fileToDelete,true);

On your PHP script, you should handle it:

<?php
if (isset($_GET['action'])){
  switch($_GET['action']){  
     case 'delete':
       @unlink(listeImages[$_GET['action']].toString());
     break;
     //Other functions you may call
  }
  exit;
}
//The rest of your index.php code
?>
Sign up to request clarification or add additional context in comments.

3 Comments

Actually, I would never write code like this one (I always use jQuery) but I just answered to the question :)
Hi, thanks a lot for the help provided. Although, won't the website become more vulnerable do file deletion (as stated here stackoverflow.com/a/11208983/1451422 ) ? Other than that, this is exactly what I need! Edit: A "POST" could do the job?
I gave you a working solution, you should wrap that code into a session control...
3

You can't call directly a php function from an ajax call, it will only call the php script like if you were opening the page index.php from a browser.

You have to add tests in your php script to know which function has to be called, eg. :

If you call in ajax the page /dev/templates/absolu/index.php?mode=delete_image&image=filename.png

<?php
if($_GET['mode'] == "delete_image") {
    unlink($_GET['image']);
}
?>

Please take care that anybody could call this page so you have to check what will be deleted and to verify what you receive in GET parameters. Here i could call /dev/templates/absolu/index.php?mode=delete_image&image=index.php to delete the php script page.

Comments

0

Using jquery (http://jquery.com/) you could make the call as:

$(document).ready(function() {
  $.get("/dev/templates/absolu/index.php?", {
    'action': 'delete',
    'other': 'get-parameters',
    'r': Math.random()
  });
});

Server side example:

<?php

switch( $_GET['action'] ) {
  case 'delete': 
    // call unlink here
  break;
  case 'dosomething':
    // else
  break;
  default: 
    // Invalid request
  break;
}

Please note that deleting files should be handled responsibly (enforce security checks) to make sure no wrong file gets accidentally or on purpose deleted.

12 Comments

yeah I was gonna suggest that too... but doesn't answer the question.
So OP should start using a library to be able to use this?
@PeeHaa Valid point, but the OP did state he's not good at all with ajax, so using a library might not be a bad idea. Not worse than copy-pasting code you don't understand any way at least...
IMO it's the other way around. You should only start thinking about using a library when you know what you are doing. This is not only about the library jQuery or the language javascript btw.
How is JavaScript different from c++ ? Do you learn to use QT before you know how to make a "hello world" ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.