Edit .php page with a form

driveflexfuel

New Member
Messages
159
Reaction score
0
Points
0
I am trying to create an options form that will change php variable within a configuration file. If anyone knows how to do this it would be great help.

Config.php looks like this
Code:
//language
$language = 'EN';

//Currency
$currency = 'USD'

//Page Title
$title = "Welcome - Home Page"

Options.php the submission form
Code:
<form>
Language <select name="language">
            <option value='EN'>English</option>
            <option value='SP'>Spanish</option>
            <option value='FR'>French</option>
         </select>
         <br/>
Currency <select name="currency">
            <option value='USD'>US Dollar</option>
            <option value='EUR'>Euro</option>
            <option value='GBP'>British Pound</option>
         </select>
         <br/>
Page Title <input type="text" name="title" />
<br/>
<input type=submit name=submit />
</form>
 

leafypiggy

Manager of Pens and Office Supplies
Staff member
Messages
3,819
Reaction score
163
Points
63
you'd have to do:

Code:
<form action="config.php" action="post">
Language <select name="language">
            <option value='EN' name="EN">English</option>
            <option value='SP' name="SP">Spanish</option>
            <option value='FR' name="FR">French</option>
         </select>
         <br/>
Currency <select name="currency">
            <option value='USD' name="USD">US Dollar</option>
            <option value='EUR' name="EUR">Euro</option>
            <option value='GBP' name="GBP">British Pound</option>
         </select>
         <br/>
Page Title <input type="text" name="title" />
<br/>
<input type=submit name=submit />
</form>

Now, we create "config.php".

PHP:
$lang = $_POST['language'];
$cur = $_POST['currency'];
$title = $_POST['title'];

Done. :)
 

driveflexfuel

New Member
Messages
159
Reaction score
0
Points
0
I don't think you understood what I meant. Im guessing you thought i just needed to pass variables between pages with post but that is not the case.

config.php is a page filled with variables that are called by many different pages.

Options.php is a page in a admin panel where they can permanently change the variables found in config.php.
 

gomarc

Member
Messages
516
Reaction score
18
Points
18
Are you asking about different users each one able to pick their own options?

If that’s the case, you will want to save those options into the user information table so they can be retrieved or changed when needed.
 

leafypiggy

Manager of Pens and Office Supplies
Staff member
Messages
3,819
Reaction score
163
Points
63
I'd do it with mySQL, much easier.
 

garrettroyce

Community Support
Community Support
Messages
5,609
Reaction score
250
Points
63
I'd do it with mySQL, much easier.

If you have a lot of values that are never changed or changed very infrequently, I would think that using a config file would be faster and simpler than many queries, though normal day to day tasks I would definitely use SQL.
 

driveflexfuel

New Member
Messages
159
Reaction score
0
Points
0
I think everyone missed what i was getting at so ill show you what I found. This code opens my config.php file collects the variables, displays them in the form. When the form is submitted it writes the new values in. It took me 4 hours to find someone to help on this subject so share this code with everyone and get it on more forums.

Code:
<?php
$handle = fopen('config.php', 'r+');
$contents = fread($handle, filesize('config.php'));
$config_array = array( 
    '\$DB_host' => $_POST['DB_host'], 
    '\$DB_user' => $_POST['DB_user'], 
    '\$DB_pass' => $_POST['DB_pass'], 
    '\$DB_name' => $_POST['DB_name'] 
);


// get values 
foreach($config_array as $from => $to)
{ 
    if(preg_match("/$from\=\'(.*?)\';/", $contents, $matches)) 
    { 
         $config_values[$from] = $matches[1]; 
    } 
    else 
    { 
         $config_values[$from] = ''; 
    } 
} 
// now config-values has all the prior values of config.php :
//    $config_values['\$DB_host'] = 'localhost';    for example

        $DB_host = $config_values['\$DB_host'];
        $DB_user = $config_values['\$DB_user'];
        $DB_pass = $config_values['\$DB_pass'];
        $DB_name = $config_values['\$DB_name'];
        
        if (isset($_POST['submit'])){

            // this line creates config-values as an empty array. no changing ;)
            $config_values = array();
                            
            foreach($config_array as $from => $to) 
            { 
                $contents = preg_replace("/$from\=\'(.*?)\';/", "$from='$to';", $contents); 
            } 
            
            fclose($handle);
            $handle = fopen('config.php', 'w');
            fwrite($handle, $contents);
            fclose($handle);
            echo '<meta http-equiv="refresh" content="0;url=index.php" />';
 

xav0989

Community Public Relation
Community Support
Messages
4,467
Reaction score
95
Points
0
BTW, you should use the header function, not a meta redirect.
 
Top