Jump to content

Creating page to set config file - newbie question


fribse2011

Recommended Posts

I'm very fresh to php coding, and embedding it in html, so I'm a bit lost here.

I have made a script for doing backups of mysql on the QNAP box, I want to make a page to set the variables in it, so the users don't have to change directly in the script like they do now.

 

First the page should read in the current config, and display that in the form, and then users can change the values, and press save, quite simple I'm sure for the experienced php coder :-)

 

I'm not sure if it's a HTML or PHP question, so here goes, if it's in the wrong forum, maybe the moderator will forgive me, and move the topic to the right one.

 

THe page I've currently created looks like this:

 

<html><body>
<h1>Mysql Backup Script</h1>
<?php
$config=parse_ini_file("config.txt");
?>
<form action="writeconfig.php" method="post"> 
<p>Days to save config: <input type="text" name="configdays" value="<?php echo $config('configdays'); ?>"/>
<br>
<p>Name of Backup location (share): <input type="text" name="share" value="<?php echo $config('share'); ?>"/>
<br>
<p>MySQL Backup User: <input type="text" name="user" value=<?php echo $config('user'); ?>"/>
<p>Errorlevel (0=off, 1=error, 2=error+warn, 3=error+warn+info: <select name="error"> 
<option>0</option>
<option>1</option>
<option>2</option>
<option>3</option>
</select>
<br>
<p><input type="submit" value="Save" />
</form>
</body></html>

 

The problem is that it doesn't show the variables in the value fields, but rather the php code

 

And the writeconfig.php looks like this:

<?php
$configdays = $_POST['configdays'];
$share = $_POST['share'];
$user = $_POST['user'];
$error = $_POST['error'];                 

$int_options = array("options"=>array("min_range"=>1, "max_range"=>100));
if (!filter_var($configdays, FILTER_VALIDATE_INT, $int_options)) die("Value for number of config days is incorrect allowed value is 1-100");

$fp = fopen("config.txt", "w");
fwrite($fp, "configdays=" . $configdays . "\r\n" . "share=". $share . "\r\n" . "user=". $user . "\r\n" . "error=". $error . "\r\n");
fclose($fp);

echo "Config file successfully written"

?>

 

It's all still very raw, but the write part works ok, still needs a lot of input validation of course.

As you can see, I'm still in the beginning part of this, so if I'm doing something wrong, or there's another way to do it properly, let me know!

Link to comment
Share on other sites

After a lot of searching, I've found some help on the net, so now it actually works, it prefills the form.

It is changed from a .html to a .php file

<?php
  session_start();
  if(isset($_SESSION['submitted_form_values'])){extract($_SESSION['submitted_form_values']);}
  $config = parse_ini_file("config.txt");
  print_r($config);
  $configdays = $config['configdays'];
  $share = $config['share'];
  $user = $config['user'];
  $error = $config['error'];
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Mysqlbackup for QNAP NAS</title>
  </head>
  <body>
    <h1>Mysql Backup Script for QNAP NAS</h1>
    <form action="writeconfig.php" method="post"> 
      <table border="0" style="background:#ececec" cellspacing="5">
      <tr><td>Days to save config: <input type="text" name="configdays" value="<?php if(isset($configdays)){print stripslashes($configdays);}else{print '';} ?>"></td></tr>
      <tr><td>Name of Backup location (share): <input type="text" name="share" value="<?php if(isset($share)){print stripslashes($share);}else{print '';} ?>"></td></tr>
      <tr><td>MySQL Backup User: <input type="text" name="user" value="<?php if(isset($user)){print stripslashes($user);}else{print '';} ?>"></td></tr>
      <tr><td>Errorlevel (0=off, 1=error, 2=error+warn, 3=error+warn+info: <select name="error"> 
        <option <?php if(isset($error) && $error == "0"){print "selected=\"selected\"";} ?>>0</option>
        <option <?php if(isset($error) && $error == "1"){print "selected=\"selected\"";} ?>>1</option>
        <option <?php if(isset($error) && $error == "2"){print "selected=\"selected\"";} ?>>2</option>
        <option <?php if(isset($error) && $error == "3"){print "selected=\"selected\"";} ?>>3</option>
        </select></td></tr>
      </table>
      <input type="submit" value="Save" />
    </form>
  </body>
</html>

<?php

if(isset($_SESSION['submitted_form_values'])){unset($_SESSION['submitted_form_values']);}

?>

 

Now I need to make some checks to see if the config exists, and the values in it are present, to make sure that it doesn't error out, and then it has to validate the input given.

 

Progress, nice!  :D

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.