2013-03-24 106 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