1

Maybe a stupid question but it is possible to write a form sort of thing that you can fill in which either writes or replaces the code in the file?

For example I have a result table with possible results. Instead of using either a database or replace the code in my file manually I'd like to write a form which changes the code for me when I fill it in. Sort of like a database but then just in the file itself.

Is this possible?

Wit kind regards

4
  • where and how are you storing your table? whats the format? Commented May 13, 2016 at 7:30
  • At the moment it is just an array in PHP like: $results = array( 'A' => ['color'=> $variable, 'value'=> 'A'], 'B' => '.... etcetc Commented May 13, 2016 at 7:34
  • When you say "the code", are you talking about code values like country codes or colour codes or something, or do you mean PHP/other source code? Commented May 13, 2016 at 7:38
  • like this: $results = array( 'A' => ['percent' => $percentA, 'value'=>'blauw','letter' => 'A', 'text' => 'This text'], 'B' => ['percent' => $percentB, 'value'=>'rood', 'letter' => 'B', 'text' => 'Other text']); Commented May 13, 2016 at 7:40

1 Answer 1

1

Simple answer: Yes, it is possible, but not recommended.

Elaborating the answer: The reason it is not recommended is because you are opening your doors to hackers that could use XSS (Cross Site Scripting) attacks, unmasking your site, or many other possibilities.

If however you are just curious on how you would modify the code from a form, you can do it as follows.

$new_code = $_POST['newcode'];
$myFile = fopen('table.html', 'w');
fwrite($myFile, $new_code);
fclose($myFile);

A file called table.html would then be created in the same location as the page that is running the previous code. If you want to place the file in another page, you could just add a relative or absolute path to the name, for instance:

$myFile = fopen('../folderA/table.html', 'w');

$_POST is a php superglobal. It is very often used in forms, and if you aren't sure how to use it, there are many great tutorials online.

If you wanted a php file, the code would be the same, except you would change the name of the file from table.html to table.php.

Let me know if that helped!

Sign up to request clarification or add additional context in comments.

5 Comments

But what if I dont want to change the entire code, but just a little piece of it? For example, just an array. That I can GET the array , show it on my screen and be able to edit it / change it. Or just add new stuff to it without deleting the rest of it. Sort of like a text editor but then for my code itself with a form. I'm not going to use it , like you said with the hackers and such, but i'm just really curious for these sort of things if it is possible and want to try it out on a local file.
@Theekopje so you mean you would like to be able to pinpoint specific parts you want to change? Well that's pretty simple too. All you would have is you would save templates of how the table code is in a string, then you would do something like: $new_colde = $template1.$variable1.$template2.$variable2.... where templates are predefind html tags like <tr></td>, </td></tr>, etc, and the variables are your form input. Does that help?
Thanks im going to try that out! Have a nice day
@Theekopje no problem, though remember the recommendation! If you don't have a database to work with there are so many better options than changing what is coded! CSV for example (Comma Separated Values) is a simple database structure that uses text files, and php has functions that make it super easy to work with!
And if you want something also light, there is XML, and if you want something even better but still light, there is SQLi which is just a file you save, however you can use sophisticated SQL queries just like a MySQL database, and if you decide to change to a real database, that would save you the trouble of re-coding everything.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.