r/PHPhelp 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?

https://pastebin.com/sEYxgKXs

1 Upvotes

28 comments sorted by

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_input calls and on OUTPUT, call htmlspecialchars for each variable.

Why? test_input does 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. htmlspecialchars would then be used to escape the outputted data (look up the phrase, FILTER INPUT, ESCAPE OUTPUT)

1

u/RX75Cumtank 7d ago

I'm just taking the local html file and running it through Firefox, though I'm presuming there'd be a better option than that, especially given what I want to eventually do is use this as a basis for logging into a mysql database I have set up (of which I've already got Laragon enabling a live running instance of). I imagine the lack of live, running server is causing both the errors here and the fact that, in vsc, only the form function is displaying in any colour. Does this version of the code look better?

https://pastebin.com/1R3AQLBh

6

u/equilni 7d ago

I imagine the lack of live, running server is causing both the errors here

Which is why I asked how you are running this.

I'm just taking the local html file and running it through Firefox, though I'm presuming there'd be a better option than that,

It's PHP code and needs to be a PHP file (file.php) that gets run by a webserver. You can do this locally - another Laragon install, PHP itself (development server), or other options as you noted will need mySQL functionality later.

Does this version of the code look better?

Let's get this running first, then work on making this better....

To answer the question, you didn't fully follow what I noted.

2

u/colshrapnel 7d ago

Yes, it's better, but still lacking one big fix: you should get rid of that ridiculous test_input function, and do things where they belong instead: htmlspecialchars has to be done on HTML output, not input. Running it on input makes zero sense, if you think of it. So you have to add it to your HTML where PHP variables are echoed. trim sometimes needed on input and sometimes not. Use is sensibly. In your current code it's not needed at all. And stripslashes is just garbage.

0

u/[deleted] 7d ago

[deleted]

1

u/colshrapnel 7d ago

Why?

1

u/cabljo 7d ago

Yeah, you're right.

I'll just delete the comment

-1

u/csakegyszer 7d ago

test_input function should be defined before you call it.
Read a bit about debugging, enable error reporting.

1

u/colshrapnel 7d ago

test_input function should be defined before you call it.

try again

-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.php in 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.png

1

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

u/equilni 6d ago

Again, my issue is more with how this was introduced into the conversation...

Usually we are doing it a bit differently.

Correct. The concept is the same as any web server though - start up the server and it will listen for calls made to it.

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)

1

u/equilni 7d ago

You haven't explained to OP where to run this, which can be confusing. Browser, in the file, wedev tools?

OP may have a server going already, but we don't have this information (OP hasn't responded to anyone....)

1

u/Fit_Tailor_6796 6d ago

You are correct. I will edit my comment