irkevin Posted July 27, 2009 Share Posted July 27, 2009 Hi, I need some help and some advice on how i should proceed with this. Let's say i have config.php with those variables: <?php $host = "localhost"; $user = "username"; $pass = "pass"; $db = "db_name"; $allow = "no"; ?> I will have a form that will list each of those values in a text input. Now, is it possible to update the values with this form? If i have to set $allow = "yes", will it be possible using a form? Please give some advice! I searched this forum but didn't get a clue on how to achieve this! Thanks, any advice is welcome. Have a nice Day! Quote Link to comment https://forums.phpfreaks.com/topic/167610-solved-update-variables-in-a-php-page-with-a-form/ Share on other sites More sharing options...
itsallbroken Posted July 27, 2009 Share Posted July 27, 2009 Use a form and action to link to the php file that this is in.... $host = $_POST['hostname']; hostname = the input name on the form is this what your after? Quote Link to comment https://forums.phpfreaks.com/topic/167610-solved-update-variables-in-a-php-page-with-a-form/#findComment-883842 Share on other sites More sharing options...
irkevin Posted July 27, 2009 Author Share Posted July 27, 2009 Well, but i need to write the file and update whatever value is assign to the variable So if $host = 'somehost', i would like to change it to $host = 'anotherhost' with a form! Quote Link to comment https://forums.phpfreaks.com/topic/167610-solved-update-variables-in-a-php-page-with-a-form/#findComment-883844 Share on other sites More sharing options...
irkevin Posted July 27, 2009 Author Share Posted July 27, 2009 Bump, No one ever tried this? Quote Link to comment https://forums.phpfreaks.com/topic/167610-solved-update-variables-in-a-php-page-with-a-form/#findComment-883861 Share on other sites More sharing options...
patrickmvi Posted July 27, 2009 Share Posted July 27, 2009 This sort of thing is done all of the time but I don't think you're being too clear in what you're trying to accomplish. You probably want to store the form values you're talking about into some session variables for you to access later (I'm assuming) so you'd want to do something like this: <? if($_POST[action] == "submit") { $_SESSION[field] = $_POST[field]; } ?> <form method="POST"> <input type="hidden" name="action" value="submit"> Field: <input type="text" name="field"><br> <input type="submit" value="Submit"> </form> Then you could reference $_SESSION[field] throughout any of your pages to get the value of what was submitted via the form. Quote Link to comment https://forums.phpfreaks.com/topic/167610-solved-update-variables-in-a-php-page-with-a-form/#findComment-883869 Share on other sites More sharing options...
waterssaz Posted July 27, 2009 Share Posted July 27, 2009 Well, but i need to write the file and update whatever value is assign to the variable Not sure what you mean by write to the file as itallbroken's advice would assign the value from the form to the variable???? Quote Link to comment https://forums.phpfreaks.com/topic/167610-solved-update-variables-in-a-php-page-with-a-form/#findComment-883870 Share on other sites More sharing options...
irkevin Posted July 27, 2009 Author Share Posted July 27, 2009 well the point it, I have a config.php page where some variable are set for the website. If one day i have to change something in this config.php page (example: change $allow = 'no' TO $allow = 'yes'), instead of doing it manually in the file, i would like to do it via a form! Hope that makes sense. lol Quote Link to comment https://forums.phpfreaks.com/topic/167610-solved-update-variables-in-a-php-page-with-a-form/#findComment-883873 Share on other sites More sharing options...
waterssaz Posted July 27, 2009 Share Posted July 27, 2009 no offence, but unless this will in some way benefit your application, sounds like you are making a lot of work for yourself just to change the value of a config variable Quote Link to comment https://forums.phpfreaks.com/topic/167610-solved-update-variables-in-a-php-page-with-a-form/#findComment-883877 Share on other sites More sharing options...
irkevin Posted July 27, 2009 Author Share Posted July 27, 2009 If i have an Admin Backend, it will then be easier and faster to change a configuration instead of doing it manually, then re upload the file! Anyways, i will try to sort things out! Thanks for every suggestion Quote Link to comment https://forums.phpfreaks.com/topic/167610-solved-update-variables-in-a-php-page-with-a-form/#findComment-883879 Share on other sites More sharing options...
waterssaz Posted July 27, 2009 Share Posted July 27, 2009 Ok I see where you are going with this now. What about rewriting the file entirely each time like this : <?php if (isset($_POST["Submit"])) { $string = '<?php $dbhost = "'. $_POST["dbhost"]. '"; $allow= "'. $_POST["allow"]. '"; /// more variables etc passed from form ?>'; $fp = fopen("config.php", "w"); fwrite($fp, $string); fclose($fp); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/167610-solved-update-variables-in-a-php-page-with-a-form/#findComment-883888 Share on other sites More sharing options...
irkevin Posted July 27, 2009 Author Share Posted July 27, 2009 Yeah, thats what i was going for.. And it's actually working.. Thank you so much for this little example! Quote Link to comment https://forums.phpfreaks.com/topic/167610-solved-update-variables-in-a-php-page-with-a-form/#findComment-884005 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.