通过JavaScript加载文件传递并打印$ _GET变量

时间:2015-09-09 10:48:08

标签: javascript php load

我搜索了一个将从index.php页面获取的get变量传递给included.php文件[由javascript加载]的解决方案。 php要求函数的一个很好的解决方案是Pass and print a $_GET variable through an included file by php require function

然而,在我的情况下,我有

for index.php => url[ index.php?var=item]
    <?php
    if(isset($_GET['var'])) {
    $var=$_GET['var'];
    }
    ?>

    <script>
    $(document).ready(function(){
       $("#ok").load("included.php");
    });
    </script>

    <div id='ok'>

    </div>
 in included.php [which will be loaded in index.php by javascript load function]
 <?php
 echo $var;
 ?>

错误是included.php文件中未定义的var。如何使用php和javascript的组合来回显这个变量?

2 个答案:

答案 0 :(得分:0)

如果您想将这些变量传递给包含的文件,您可以使用

   $("#ok").load("included.php?<?php echo $urlString; ?>");

可以使用此功能生成URL字符串

function GenGetVars(){
    $Base = NULL;
    foreach($_GET as $key => $variable){
        $Base .= '&'.$key.'='.$variable;
    }
    return(rtrim($Base, '&'));
}

另一种选择是:

$( "#ok" ).load( "included.php", { "choices[]": [ "Jon", "Susan" ] } );

基于第一个例子:

<强>的index.php:

<?php
function GenGetVars(){
    $Base = NULL;
    foreach($_GET as $key => $variable){
        $Base .= '&'.$key.'='.$variable;
    }
    return(rtrim($Base, '&'));
}

if(isset($_GET['var']) === true) {
    $var=$_GET['var'];
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
    <title>Test</title>
    <script type="text/javascript">
    $(document).ready(function(){
       $("#ok").load("included.php?<?php echo GenGetVars(); ?>");
    });
    </script>
</head>
<body>
    <div id='ok'>
    </div>
</body>
</html>

<强> included.php

 <?php
 echo print_r($_GET, true);
 ?>

答案 1 :(得分:0)

<强>的index.php:

<?php
function GenGetVars(){
    $Base = NULL;
    foreach($_GET as $key => $variable){
        $Base .= '&'.$key.'='.$variable;
    }
    return(rtrim($Base, '&'));
}

if(isset($_GET['var']) === true) {
    $var=$_GET['var'];
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
    <title>Test</title>
    <script type="text/javascript">
    function LoadScriptFile(){
        /// SENDING THE INFORMATION BY AJAX
        $.ajax({
            type : "POST",                  /// SEND TYPE
            url  : "getScriptName.php",     /// TARGET FILE TO RETRIEVE INFORMATION
            data : { 
                    'SomeParamID'                   : 1,
                    'AnotherParamID'                : 'Dunno.. just something'
                   },
                    ///######## IN CASE OF SUCCESS
                    success:function(response){
                    if( response != "" ){
                        $.getScript( 'js/' + response + '.js', function( data, textStatus, jqxhr ) {
                          console.log( data ); // Data returned
                          console.log( textStatus ); // Success
                          console.log( jqxhr.status ); // 200
                          console.log( "Load was performed." );
                        });
                    }
                    else{
                        alert( "Error retreiving data" );
                    }
               }
            }
        );  
    }
    </script>
    <script type="text/javascript">
    $(document).ready(function(){
       $("#ok").load("included.php?<?php echo GenGetVars(); ?>");
    });
    </script>
</head>
<body>
    <div id='ok'>
    </div>
</body>
</html>

<强> included.php

 <?php
 echo print_r($_GET, true);
 ?>

<强> getScriptName.php

 <?php
 switch($_POST['SomeParamID']){
    case 1:
        echo 'MyGreatJSFile';
    break;
    case 2:
        echo 'MyNotSoGreatJSFile';
    break;
 }
 ?>

你可以试试这个。