最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

calling php functions from javascript, onload (AJAX?) - Stack Overflow

programmeradmin3浏览0评论

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());

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());
Share Improve this question edited Jun 26, 2012 at 14:57 Carrie Kendall 11.2k6 gold badges63 silver badges81 bronze badges asked Jun 26, 2012 at 13:51 Jeff NoelJeff Noel 7,6274 gold badges42 silver badges66 bronze badges 7
  • Take a look at performing an asynchronous HTTP request using jQuery – Bruce 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. – piddl0r Commented Jun 26, 2012 at 13:58
  • 2 All answers seem to be focused on GET but I would remend switching to POST for a delete action. – jeroen Commented Jun 26, 2012 at 14:02
  • POST won't bring anything more to this case.. – ilyes kooli Commented Jun 26, 2012 at 14:07
  • 1 @jeroen I never remend an ORM when someone asks how to connect to a mysql database :) – ilyes kooli Commented Jun 26, 2012 at 14:15
 |  Show 2 more ments

3 Answers 3

Reset to default 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
?>

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.

Using jquery (http://jquery./) 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.

发布评论

评论列表(0)

  1. 暂无评论