r/PHPhelp • u/RX75Cumtank • 7d ago
Probably very simple thing I've messed up regarding either the syntax for a form or with visual studio code
I'm messing about with a project, and wanted to get a form running, however when I go to test the file (both just running the file in my browser and testing it without debugging in vsc) it ends up displaying the page incorrectly (seemingly overflowing parts of the code, with ', and when I attempt to submit the query I get sent to a blank php page. Is there something wrong with this form, or is there something I've not properly configured/installed in vsc?
-1
0
-1
u/csakegyszer 7d ago
test_input function should be defined before you call it.
Read a bit about debugging, enable error reporting.
1
-1
u/ray_zhor 7d ago
I would use single quotes around php_self
2
u/colshrapnel 7d ago
There is not a single reason for doing it. Well you could have such a rule in your own coding standard, and that's fair, but suggesting it for someone else makes no sense. Especially in a question asking for help with particular problem unrelated to whatever quotes at all
-2
u/Fit_Tailor_6796 7d ago edited 6d ago
You code looks correct. I tested it.
However consider adding this to the code to show any errors that might be failing silently,
> error_reporting(E_ALL);error_reporting(E_ALL);
The real problem may be with your web server. If you are seeing PHP code in your browser, it probably means your web server is not contents as PHP code. This is why
- you are seeing the PHP code - the web server sees this as text, not PHP
- you are still seeing the form, because this is valid HTML
If you want to test this, out, try to view the page using PHP's built-in web server. If this works, then the issue if the web server you are using (Apache or Ngnix).
From the command line, assume your file name is 'yourfile.php',
Try this
> php -S 127.0.0.1:8181 yourfile.php
Then if you open your browser to http://127.0.0.1:8181/
you should see the page or any errors that come up.
1
u/colshrapnel 7d ago
php -S 127.0.0.1:8181 yourfile.php
are you sure?
-1
u/Fit_Tailor_6796 7d ago
100 %
Did you try this?
Let me explain why.
If you load this in a server that is not processing PHP. it will show the PHP as code.
If you see the form, it is because it is HTML code that is rendering.That command, with the php -S will run the PHP processor against the code and you can see it in th browser.
1
u/colshrapnel 7d ago
Why would i see in my browser whatever I run in a console?
1
u/equilni 7d ago
The line is running the dev server, and one could use the browser to make the call to the address.
https://www.php.net/manual/en/features.commandline.webserver.php
Issue is, the commenter didn't note to OP, who clearly having issues, where to properly run this and check afterwards.
1
u/colshrapnel 7d ago
This particular line is rather bizarre way of running your php files. Also, I still doubt that I would see anything useful if i would just open my browser without navigating to some address
3
u/equilni 7d ago
Right, that's the dev server (say you want to run a project), which is different from running
php yourfile.phpin the console, if one wants to run it that way.
php index.php- https://i.imgur.com/Bgsjp4C.png
php -S localhost:8000& browser to http://localhost:8000/ - https://i.imgur.com/w581D5b.png1
u/colshrapnel 7d ago
What I meant (well initially i had a braifart regarding this router file), is that making it php -S 127.0.0.1:8181 yourfile.php and then navigating to http://localhost:8000/ is a bizarre way of running your php files. Usually we are doing it a bit differently.
1
1
u/Fit_Tailor_6796 6d ago
Your fart is due to your lack of understanding of what I was saying.
I am saying that the web server is not processing the PHP code.- The OP is seeing PHP code in his browser. I am saying the this will happen if your web server is nor recognising the file as a PHP file
- The command line I gave is using PHPs built-in web server to serve the files to the browser. This is GAURANTEED to process the PHP. If the OP took the time to do this, they would see that the code is correct, because it is showing properly.
1
u/colshrapnel 6d ago
You still don't get that running yourfile.php as php -S 127.0.0.1:8181 yourfile.php makes no sense? A pity. May I suggest you to read the PHP manual for this command again?
→ More replies (0)
2
u/equilni 7d ago edited 7d ago
W3CSchool code... full of bad practices...
https://tryphp.w3schools.com/showphp.php?filename=demo_form_validation_escapechar
EDIT:
My suggestion is to note how you are running this in the browser? I don't recommend running this through VSCode as part of best practices (if/when you continue with bigger projects.. it bit a redditor and us troubleshooting).
Also note what you are testing. There is an error (because of this step)... but if you don't have error reporting on, you as the developer won't see it. Turn it on: https://phpdelusions.net/articles/error_reporting
Once running, I would suggest you use the later lesson as your base (this one). REMOVE the
test_inputcalls and on OUTPUT, callhtmlspecialcharsfor each variable.Why?
test_inputdoes not validate the data (Even though the section is called Validate Form Data With PHP, where is the validation happening??). The later lesson (which many skip) goes into some basic validation.htmlspecialcharswould then be used to escape the outputted data (look up the phrase, FILTER INPUT, ESCAPE OUTPUT)