forms - How to run a php file on Visual Studio Code?

I am trying to run a basic php script that should take some info from a form and then create a file with the input content. I am using VS code, and I guess I spent more than 6 hours to figure out why my php script isn't working.
I have done the followings:
I have xampp installed, and my Apache server running. In settings.json, I have configured the executablepath key.
"php.validate.executablePath": "C:\\xampp\\php\\php.exe",
In my folder, I have an HTML file that consists the following form:
<form action="test.php" onsubmit="file_handler()">
Name: <input type="text" id="name" name='name'><br>
<div class="s">Text</div> <br><textarea maxlength="800" id="suggestion" name='text'></textarea><br>
<div class="span-ch"><span id="charNum"> </span> <span id="charText"></span></div><br>
<input type="submit" value="Send">
</form>
In the same folder, I have a test.php file which contains the following script:
<?php
if(isset($_GET['name']) && isset($_GET['text'])) {
$filename = preg_replace('#[^A-Za-z0-9_-]#', '', $_GET['name']);
$file = $_SERVER['DOCUMENT_ROOT']."/textfiles/$filename.txt";
$f = fopen($file, 'w');
fwrite($f, $_GET['text']);
fclose($f);
echo 'Success.';
} else {
echo 'Error.';
}
?>
When I hit the submit bottom, it redirects me to the.php
file, but instead of running the script and creating the file, and tell me if it was created or not, on that page, it simply shows me the php script, like a html page.
This is what is shown to me on browser, instead of "Success" or "Error".
<?php
if(isset($_GET['name']) && isset($_GET['text'])) {
$filename = preg_replace('#[^A-Za-z0-9_-]#', '', $_GET['name']);
$file = $_SERVER['DOCUMENT_ROOT']."/textfiles/$filename.txt";
$f = fopen($file, 'w');
fwrite($f, $_GET['text']);
fclose($f);
echo 'Success.';
} else {
echo 'Error.';
}
?>
It might be a newbie question, because I am a beginner. I searched for answers on internet, and nothing.
NOTE: If I run my script on my console, it's working.
[Running] php ".......\Project1\test.php"
Error.
[Done] exited with code=0 in 0.474 seconds
Which means that the executable is found.
Please, can somebody help me? I much appreciate your time! Have a nice day!
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: attempt to read property "id" on null
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.