开发者

question about functions in php

i have a php file with two function

<?php 

function first () {
//in real case more conditions
echo "first";
return true;
}

function second开发者_运维知识库 () {
//in real case more conditions
echo "second";
return true;
}

first();
second();

?>

and in an ajax file

function(data) {
                if (data == "first") {
                    $("#msgbox1").fadeTo(200, 0.1, function() {
                        $(this).html('something');
                    });
                } 
                else if (data == "second") {
                    $("#msgbox1").fadeTo(200, 0.1, function() {
                        $(this).html('Something else');
                    });
                } 

the problem is because the php devolve firstsecond and i need to check in a individual mode - first and second. If i make return first; how i can check in data if the return is correct? data==first not working

thanks


You'd want something more along these lines:

function first() {
    if (some condition ...) {
       return true;
    }
    return false;
}

function second() {
   if (some other condition...) {
      return true;
    }
    return false;
}

$data = array();

$data['first'] = first();
$data['second']= second();

echo json_encode($data);

Then in your Javascript code:

if (data.first) {
   ... first occurred
}
if (data.second) {
   ... second occured;
}

Must better to return actual Javascript data structures via JSON encoding, than a string. This way you can pass in other data elements. For instance, if first() was doing a database lookup and something blew up, you can return a 'false' along with an error message indicating why the request failed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜