Can I edit a variable in a form

Discussion in 'PHP' started by baris22, Aug 15, 2009.

  1. #1
    Hello,

    Can I edit a variable in a form without using a database.

    forexample

    
    
    $title = "edit me";
    
    
    PHP:
    Can i call and edit $title in a form input?

    Thanks
     
    baris22, Aug 15, 2009 IP
  2. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #2
    So you want to have a form to edit a PHP file directly if I understand you correctly?

    Yes, you can do that, you'll have to do a regex to find the variable line in the PHP file, then replace it and save it.

    So something like this: (it may not work, untested)

    
    $contents = file_get_contents($filename);
    $contents = preg_replace('/\$title = "[^"]";/i', '\$title = "' . $_POST['variable'] . '";', $contents);
    file_put_contents($filename, $contents);
    
    PHP:
     
    premiumscripts, Aug 15, 2009 IP
  3. baris22

    baris22 Active Member

    Messages:
    543
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #3
    I managed to write it.

    
    
    <?php 
    if (isset($_POST["Submit"])) { 
    
    $string = '<?php  
    $dbhost = "'. $_POST["dbhost"]. '"; 
    $dbuname = "'. $_POST["dbuname"]. '"; 
    $dbpass = "'. $_POST["dbpass"]. '"; 
    $dbname = "'. $_POST["dbname"]. '"; 
    $prefix = "'. $_POST["prefix"]. '"; 
    $user_prefix = "'. $_POST["user_prefix"]. '"; 
    $dbtype = "'. $_POST["dbtype"]. '"; 
    ?>'; 
    
    $fp = fopen("config.php", "w"); 
    fwrite($fp, $string); 
    fclose($fp); 
    } 
    include "config.php"; 
    ?>
    
    <form action="submit.php" method="post" name="install" id="install"> 
      <p><?=$dbhost?> <input name="dbhost" type="text" id="dbhost" value="<?=$dbhost?>"> DB Host</p> 
      <p><?=$dbuname?> <input name="dbuname" type="text" id="dbuname" value="<?=$dbuname?>"> DB Username</p> 
      <p><?=$dbpass?> <input name="dbpass" type="password" id="dbpass" value="<?=$dbpass?>"> DB Pass </p> 
      <p><?=$dbname?> <input name="dbname" type="text" id="dbname" value="<?=$dbname?>"> DB Name </p> 
      <p><?=$prefix?> <input name="prefix" type="text" id="prefix" value="<?=$prefix?>"> DB Prefix</p> 
      <p><?=$user_prefix?> <input name="user_prefix" type="text" id="user_prefix" value="<?=$user_prefix?>"> Userprefix</p> 
      <p><?=$dbtype?> <input name="dbtype" type="text" id="dbtype" value="<?=$dbtype?>"> DB Type </p> 
      <p><input type="submit" name="Submit" value="Install"></p> 
    </form>
    
    
    PHP:
     
    baris22, Aug 15, 2009 IP