简体   繁体   中英

Issue Passing JS variable to PHP include file

I have a php file where I am using it to setup dynamically generated pages based on the input variables. It starts on and index.html page where the variables are gathered some of which are not simple strings but complex Google Earth objects. On the submit of that page it is posted to another page and you are redirected to the created file. The trouble is coming when I try to use that variable within the php include file that is used to generate the pages.How do i properly get a variable from this form and then pass it through to be able to use it on the new generated page. Here is what I am trying currently.

On the click of this button the variable flyto1view is set.

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

    }
});

Then from here I have tried setting the value to an hidden field but Im not sure if that kinda of variable has a value that can be set like that. Whats the best way to get this variable to here after post

<? 
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: 
?> 

Firstly. creating files from user input is pretty risky. Maybe this is only an abstract of your code but doing a mkdir from the first letter of the input without checking that the first letter is actually a letter and not a dot, slash, or other character isn't good practice.

Anyway, on to your question. I would probably use $_GET variables to pass to the second file. So in the second file you use <?php $_GET['foo'] ?> and on the first file you do:

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

You could also echo the variable into your template like so:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM