0

I am currently using the script below to fill some divs with data i need

function fillLeagues(){
    var load = $.get('drawleague.php');
    $(".really").html('Refreshing');
    load.error(function() {
      console.log("Mlkia kaneis");
      $(".really").html('failed to load');
      // do something here if request failed
    });
    load.success(function( res ) {
      console.log( "Success" );
      $(".really").html(res);
    });
    load.done(function() {
      console.log( "Completed" );
    });
}

I was wondering if it was possible to call a certain function in the drawleague.php file that will return the info since i don't wanna use say 10 php files to fill 10 divs for example.

2
  • 1
    use GET parameters and let your PHP script handle what to output depending on these parameters Commented Jan 9, 2014 at 10:48
  • i have not thought of that. Thanks a lot! Commented Jan 9, 2014 at 10:50

2 Answers 2

4

Ajax & PHP are different technologies, which means they can only communicate in certain ways

This means that it doesn't matter if you have 1 or 100 files, if you can call the functions inside the file, you can keep it to one file

I would recommend this:


Params

jquery $.GET parameters passing in the url

You can pass parameters through your $.GET request to your PHP file. This means inside your PHP, you can take these params & determine the output

Because you havn't posted any PHP code, here's what you might be able to use:

<? 
   switch ($_GET["name"]) {
      case "x":
         return $image_x;
         break;
      }
   }
?>
Sign up to request clarification or add additional context in comments.

3 Comments

Yes pretty much what Thomas David was saying above. I've been passing data to PHP but haven't thought of that to determine the output from the PHP file. Much more detailed answer. Thank you
Np, Thomas's comment went live before I had a chance to finish, so figured I'd publish anyway
If you decide to accept on of both answers I'd suggest you pick @RichPeck 's. Background information is more detailed here.
2

I'd suggest you use get parameters in order to achieve this behavior. Pass the function name via Get parameter and call it in your PHP file. Of course you need to create some kind of protection. See below (pretty provisoric).

<?php

$param = $_GET['p'];

$whitelist = [
    'func1',
    'func2',
    'func3'
];

if(!in_array($param, $whitelist))
    die("Sorry, no manipulating");

call_user_func($param);

function func1()
{
    //Some stuff
}


function func2()
{
    //Some stuff
}


function func3()
{
    //Some stuff
}

?>

Then pass the param in your JS part:

var load = $.get('drawleague.php?p=func1');

Comments

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.