2013-03-24 107 views
0

我有一个php文件,我使用它来基于输入变量设置动态生成的页面。它开始于index.html页面,其中收集了一些变量,这些变量不是简单的字符串,而是复杂的Google Earth对象。在提交该页面时,它会发布到另一个页面,并且您被重定向到创建的文件。当我尝试在用于生成页面的php包含文件中尝试使用该变量时,麻烦即将到来。我如何从此表单正确获取变量,然后将其传递到新页面中以便能够使用它。这是我目前正在尝试的。问题将JS变量传递给PHP包含文件

点击这个按钮变量flyto1view被设置。

$("#flyto1").click(function(){   
    if (!flyto1view){ 
     flyto1view = ge.getView().copyAsLookAt(ge.ALTITUDE_RELATIVE_TO_GROUND); 
      $("#flyto1view1").val(flyto1view) 
    } 
    else { 
     ge.getView().setAbstractView(flyto1view); 

    } 
}); 

然后从这里我已经尝试将该值设置为一个隐藏领域,但如果林不知道这有点儿变的具有可设定这样的值。有什么最好的办法让这个变量在这里后

<? 
if (isset($_POST['submit']) && $_POST['submit']=="Submit" && !empty($_POST['address']))  {//if submit button clicked and name field is not empty 
$flyto1view1 = $_POST['flyto1']; 
$address = $_POST['address']; //the entered name 
$l = $address{0}; // the first letter of the name 

// Create the subdirectory: 
// this creates the subdirectory, $l, if it does not already exists 
// Note: this subdirectory is created in current directory that this php file is in. 
if(!file_exists($l)) 
{ 
mkdir($l); 
} 
// End create directory 

// Create the file: 
$fileName = dirname(__FILE__)."/$address.html"; // names the file $name 
$fh = fopen($fileName, 'w') or die("can't open file"); 
// The html code: 
// this will outpout: My name is (address) ! 
$str = "  
<? php include ('template.php') ?> 

"; 
fwrite($fh, $str); 
fclose($fh); 
// End create file 

echo "Congradualations!<br /> 
The file has been created. 
Go to it by clicking <a href=\"$address.html\">here</a>."; 
die(); 
} 

// The form: 
?> 

回答

1

首先。从用户输入创建文件是相当危险的。也许这只是你的代码的一个摘要,但是从输入的第一个字母开始输入mkdir而不检查第一个字母实际上是一个字母,而不是一个点,斜杠或其他字符不是一个好习惯。

无论如何,在你的问题。我可能会使用$ _GET变量传递给第二个文件。所以在第二个文件使用<?php $_GET['foo'] ?>并在第一个文件,你做的事:

echo "Congradualations!<br /> 
    The file has been created. 
    Go to it by clicking <a href=\"$address.html?foo=$flyto1view1\">here</a>."; 

你也可以呼应变量到您的模板,像这样:

$str = ' 
    <?php 
     $var = \'' . $flyto1view1 . '\'; 
     include (\'template.php\') 
    ?>'; 
+0

同意风险程度,但我会是只有一个使用此页面来创建“模板化”文档。如果我像这样做,我可以只使用url中的变量? – GO3DExpansion 2013-03-24 20:26:56

+0

是的,你可以在url中使用这个变量。我将编辑我的评论以反映这一点。 – chrislondon 2013-03-24 20:28:08

+0

我也更新了我的评论,以显示如何直接将var添加到模板中。 – chrislondon 2013-03-24 20:30:54