【问题标题】:Pass and print a $_GET variable through php load file by javascript通过 javascript 的 php 加载文件传递并打印 $_GET 变量
【发布时间】:2015-12-05 07:12:10
【问题描述】:

我搜索了一种解决方案,将从 index.php 页面获得的 get 变量传递给 include.php 文件[由 javascript 加载]。 php require 函数给出了一个很好的解决方案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 文件中未定义的变量。如何结合php 和javascript 回显这个变量?

【问题讨论】:

    标签: javascript php load


    【解决方案1】:

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

       $("#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);
     ?>
    

    【讨论】:

    • 看来你的答案对多个变量都有好处,我真的很感兴趣。兄弟,你能再解释一下,举个例子更好吗?我猜你知道我的水平差。
    • 哥,我明白了,我用了$("#ok").load("included.php?var=");在 index.php 中。最初,javascript 在外部 .js 文件中。看来如果我将 javascript 保留在 index.php 中,它可以正常工作。但是如何将脚本保存在外部文件中?请多一点帮助......!!!
    • 感谢您的精彩解释。我会很高兴,如果您愿意再回复一个关于在外部 .js 文件中使用 javascript 的可能性以及如何使用的问题?
    • 一般来说,您不能在 Javascript 包含的文件中加载任何 javascript 文件。
    • 谢谢,你是对的。但是保持一切不变,我只是希望 javascript 保存在像 function.js 这样的外部文件中,并通过 将此文件添加到 index.php 中。最后我会有三个文件。 (1).index.php、(2).included.php 和 (3)。函数.js
    【解决方案2】:

    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;
     }
     ?>
    

    你可以试试这个。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-30
      • 2013-03-05
      • 1970-01-01
      • 2011-10-19
      相关资源
      最近更新 更多