Can you use $_GET variables when including a file in PHP? Is it possible, even from an AJAX call?

前端 未结 3 889
暗喜
暗喜 2021-01-20 17:04

I have a PHP file that handles sending out confirmation emails. I also have a calendar that I use AJAX to do various updates. When AJAX calls the update file, it updates the

相关标签:
3条回答
  • 2021-01-20 17:35

    $_GET is a global variable, so can be used anywhere within your php script, including any included scripts

    0 讨论(0)
  • 2021-01-20 17:41

    You can handle the variables within your included script, rather than appending them onto the include path itself:

    $var = "foo";
    include("script.php");
    

    -- contents of script.php --

    echo $var; // 'foo'
    

    In my opinion this is much cleaner anyway. You may desire to check to make sure the values exist within script.php before echoing out any variables.

    0 讨论(0)
  • 2021-01-20 17:49

    There are, at least, two alternatives to solve this:

    1)

    
    if($_GET['farm']) {
        $var = $_GET['farm'];
        $farm = $var;
        include("ajaxInclude.php");
    }
    

    Then, in ajaxInclude.php:

    
    if($farm) {
        $var = $farm;
        echo $var;
    }
    

    2)

    
    if($_GET['farm']) {
        $var = $_GET['farm'];
        header("Location: ajaxInclude.php?farm={$var}");
    }
    

    0 讨论(0)
提交回复
热议问题