Jump to content

[SOLVED] Making a form work with variables


newbtophp

Recommended Posts

I've got a file named "config.inc.php" which has variables like:

 

//Basedir
$siteurl = "text goes here";
//Website Name
$site_title = "text goes here";
//Your Email
$email = "text goes here";

 

Im trying to create a form which will edit the variables ("text goes here")

 

Like:

 

<form>

Website Url: (this edits $site_title)<BR>

<input type="text" name="url" size="25"><BR><BR>

Website Title: (this edits $siteurl)<BR>

<input type="text" name="title" size="25"><BR><BR>

Email: (this edits $email)<BR>

<input type="text" name="email" size="25">

<BR>
<BR>

<input type="submit" value="Submit">

</form>

 

How would I be able to build a form, so the fields in the form will actually edit the correct variables within config.inc.php.

Like what ever i put in the "Website Title:" field then it will edit the $site_title variable at config.inc.php.

 

Anyone can help?

 

Link to comment
Share on other sites

You'll probably want to look into fopen and fwrite.

 

You could eval the current contents to extract all the variables, and then overwrite any changed ones with the new data, and then write them all back into the file.

 

That's what I do for the config of my site.

 

It's pretty fun :D

Link to comment
Share on other sites

You'll probably want to look into fopen and fwrite.

 

You could eval the current contents to extract all the variables, and then overwrite any changed ones with the new data, and then write them all back into the file.

 

That's what I do for the config of my site.

 

It's pretty fun :D

 

Can you give me an example code of how you would do it  ???

 

Sorry for this, but I've never done this before, and im still learning.

Link to comment
Share on other sites

Basically, the way I did it was have the config in an array, and then do something like this:

 

$fh = fopen($this->config_file, 'r') or death::death();

$config_file_config = str_replace(array('<?php', '?>'), null, fread($fh, filesize($this->config_file)));
fclose($fh);

eval('$config='.$config_file_config);

That puts the entire config array into $config.

 

I then create a form, with the key of each config as the name (with a bit of formatting), and the value as the value.

 

I then collect the form data, change anything in the $config array that has been changed, by comparing it to the post data, and then construct a PHP file, and create the array again, then write to the array to the config file.

 

It sounds quite complicated...but it's pretty easy tbh. You just need to experiment :D

Link to comment
Share on other sites

If you want a generic system which will work with any number of variables, I recommend using an array. This will also help prevent collisions between the variables you use in your code and the settings -

 

<?php
$settings['siteurl'] = "text goes here";
$settings['site_title'] = "text goes here";
$settings['email'] = "text goes here";
?>

 

Next you will need to get these values into your code. I recommend using include as this will both read the file and cause the variables to be parsed and set to the values -

 

$file = "config.inc.php";
include "$file";

 

On your form, you will need to echo the existing values in the value="..." attribute in each input field - http://w3schools.com/tags/att_input_value.asp

 

I recommend using a HTML array for the input field name (with a different array name than the $settings array) and make each array index be the same name as the setting - http://us.php.net/manual/en/faq.html.php#faq.html.arrays

 

-------------------------------------

 

When the form is submitted, you will basically (extra items like validating each value to insure it is not empty, or any security so that only authorized visitors can perform the actions is up to you) need to write the php code tags and the lines of code that contain the variable names and the values.

 

The array that you used for the HTML form input fields will contain index/value pairs that you need to write back to the file. Assuming the input field names you used were - name="input[siteurl]", name="input[site_title]", and name="input", you would have the following post variables -

 

$_POST['input']['siteurl']

$_POST['input']['site_title']

$_POST['input']['email']

 

You would simply make the output that you want and write it to the file -

 

<?php
$output = "<?php\n";
foreach($_POST['input'] as $key => $value){
    $output .= "\$settings['$key'] = '$value';\n";
}
$output .= "?>\n";

$file = "config.inc.php";
file_put_contents($file,$output);
?>

 

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.