<?php
    
// define the pages
    
$options = array('contact','welcome','test');
    
    
// check if no page has been selected
    
if(!isset($_POST['DATA_page']))
        
$_POST['DATA_page']=$options[0];
    
    
// check if the page exists in the array
    
if(!in_array($_POST['DATA_page'],$options))
        
$_POST['DATA_page']=$options[0];
    
    
$filename='Pages/'.$_POST['DATA_page'].'.php';
    
    
// save 
    
if(isset($_POST['BUTTON_save']))
    {
        
// check if the file eixsts
        
if(file_exists($filename))
        {
            
// save code to file
            
file_put_contents($filename,$_POST['DATA_code']);
        }
    }
?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Backend</title>
  </head>
  <body>
    <form id="form" action="cms.php" method="POST">
        <nav>
            <select name="DATA_page" onchange="document.getElementById('form').submit();">
                <?php
                    
// display the select box
                    
foreach($options as $option)
                    {
                        echo 
'<option';
                        
// select the actual page
                        
if($_POST['DATA_page']==$option
                            echo 
' selected="selected"';
                        echo 
'>'.$option.'</option>';
                    }
                
?>
            </select>
            <!--<button type="submit" name="BUTTON_edit">Edit</button>-->
        </nav>
        <div id="main">
            <?php
                
// check if the file exists
                
if(file_exists($filename))
                {
                    
$code file_get_contents($filename);
                    
?>
                    <textarea name="DATA_code"><?=$code?></textarea>
                    <button type="submit" name="BUTTON_save">Save</button>
                    <?php
                
}
                else
                    echo 
"Sorry, the file $page does not exist on the server.";
            
?>
        </div>
    </form>
  </body>
</html>