views:

179

answers:

1

I have a ajax.php file that contains the following sample code:

switch($_REQUEST['request_name'])
    {
        case 'edit':
           echo "edit mode";
           break;
        case 'delete':
           echo "delete mode";
           break;
        default:
           die("Error: wrong request name ".$_REQUEST['request_name']);
           break;
    }

I have another file index.php that I want to call results from ajax.php. Hmm.. how do I do it? I normally use javascript to call results from ajax.php. But is there a way I can call results from within index.php as well? Code is wrong below but something to this effect.

$result = include("ajax.php?request_name=delete");
echo $result;
+4  A: 

You were correct to use include but instead of passing in the vars like a query string you can just define them before your include and they'll be brought in to the file.

$_REQUEST['request_name'] = 'edit';
include('ajax.php');

Any variables that are then defined in your included file will now be available in the parent file as well. If you were to process the edit action and store the results in a var called $results in ajax.php you would have access to that same variable from within the including file (after the include statement).

$_REQUEST['request_name'] = 'edit';
include('ajax.php');
echo $results;
Cryo
ah gotcha! Thanks! so many smart people here.
Scott