0

What I'm doing is checking a users status in the database.

The PHP page echo's the status back to the ajax call and depending on the status a function is being executed.

This works well. I know I could use json, but for now this is fine. What I'd like is not to echo the users status, but to echo the function that should be executed and execute it after ajax is done.

My jQuery/ajax code:

function checkstatus() {
    $.ajax({
    type: "GET",
    url: "./query/checkstatus.php",
    data: {}
    }).done(function(result) {
      if (result == "nieuw") {
        loadHomeNieuw();
      };
      if (result == "actief") {
        loadHome();
      };
    });
}

What I'd like but is not working. jQuery/ajax code:

function checkstatus() {
    $.ajax({
        type: "GET",
        url: "./query/checkstatus.php",
        data: {}
    }).done(function(result) {
    });
}

PHP code (not working):

if ($status == "nieuw") {
    echo "loadHomeNieuw();";
}

if ($status == "actief") {
    echo "loadHome();";
}

Is this even possible? The reason why I'm asking, is that I feel it's safer. It might be possible to hack the status as a user...so he can change it and the wrong function is being called. If I do this on the server side, it's way safer.

Or are there better options?

4

2 回答 2

0

你可以调用 eval

function checkstatus() {
    $.ajax({
        type: "GET",
        url: "./query/checkstatus.php",
        data: {}
    }).done(function(result) {
        eval(result);
    });
}

但有些人说这是邪恶的,所以你可以只返回一个名称并执行一个具有该名称的函数:

function checkstatus() {
    $.ajax({
        type: "GET",
        url: "./query/checkstatus.php",
        data: {}
    }).done(function(result) {
        window[result]();
    });
}

在 php 中只是回显名称

if ($status == "nieuw") {
    echo "loadHomeNieuw";
} else if ($status == "actief") {
    echo "loadHome";
}
于 2013-08-11T18:42:12.507 回答
0

您要查找的内容称为 GetScript:http ://api.jquery.com/jQuery.getScript/

这将执行返回的代码。编码 :

$.getScript("./query/checkstatus.php");
于 2013-08-11T18:44:03.467 回答