newbtophp Posted June 11, 2009 Share Posted June 11, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/161864-solved-making-a-form-work-with-variables/ Share on other sites More sharing options...
waynew Posted June 11, 2009 Share Posted June 11, 2009 You'll probably (unless you're magic) have to look into PHP's File Functions. Quote Link to comment https://forums.phpfreaks.com/topic/161864-solved-making-a-form-work-with-variables/#findComment-854016 Share on other sites More sharing options...
jxrd Posted June 11, 2009 Share Posted June 11, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/161864-solved-making-a-form-work-with-variables/#findComment-854017 Share on other sites More sharing options...
newbtophp Posted June 11, 2009 Author Share Posted June 11, 2009 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 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. Quote Link to comment https://forums.phpfreaks.com/topic/161864-solved-making-a-form-work-with-variables/#findComment-854026 Share on other sites More sharing options...
ted_chou12 Posted June 11, 2009 Share Posted June 11, 2009 are you tring to do this: <input type="text" name="email" value="$email" size="25"> so the email is in the input box, i dont know if i am understanding u correctly. Ted Quote Link to comment https://forums.phpfreaks.com/topic/161864-solved-making-a-form-work-with-variables/#findComment-854047 Share on other sites More sharing options...
jxrd Posted June 11, 2009 Share Posted June 11, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/161864-solved-making-a-form-work-with-variables/#findComment-854058 Share on other sites More sharing options...
PFMaBiSmAd Posted June 11, 2009 Share Posted June 11, 2009 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/161864-solved-making-a-form-work-with-variables/#findComment-854066 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.