Action="" or action="?", how do they really differ?

Hi, there, sorry for the maybe too basic question.
When submitting a form, using method=“POST”, action=“” causes the very same form page to be reloaded and the INSERT command is not executed in the controller (index.php) and so, no alteration is done to the database. (This I understand: once the parent directory was not reloaded and so, index.php was not called, the code into it was not executed either and the query INSERT couldn’t do its job). But I read in a book (here at SitePoint) that action=“” causes the parent directory to be reloaded
Instead, if I use action=“?”, the parent directory is reloaded, consequentely index.php is called, the INSERT query is executed and data is stored in the data base.
Could you please explain to me what"s the real difference between action=“” and action=“?” and what they really do, each one of them, in more technical terms?
Thanks in advance!
Umberto

1 Like

If your page is index.php?action=dostuff and form method = POST:
action="" will call index.php?action=dostuff and send POST data.
action="?" will call index.php? and send POST data.

“?” essentially truncates any url parameters when the form is submitted.

3 Likes

Hi, thanks for your reply.
As I read in a book: “leaving the action attribute blank […] tells the browser to
submit the form back to the same URL from which it received the form”.
In the same book there’s a part that says: “setting the action to “?” strips that query string off the URL when submitting the form.”
Would you please expand a little bit more on those two types of action, and, when it is recommended to use one or the other?
Thank you very much
Umberto

I dont know how to explain/expand further than I have above. The above post gives you an example of blank, and an example of ?.

When do you use one over the other? If you need to strip off the query string parameters from your last pageload (or if you MIGHT need to), then you use ?, if you don’t, you use blank. They are different use cases, so you use them when you’re in the appropriate use case.

1 Like

Hi Umberto,

In cases like this where it’s confusing, it can help to build a little demo which tests the different scenarios.

Here’s a page which demonstrates what happens when you set the “?” as your action.

I’ve put 4 buttons on the page. One button has the action set to "?color=blue". Another has the action set to "?color=red", another has it set empty "", and the last has it set to "?".

In each case, when you click the button, the PHP code will print out at the top what it saw for the query string.

I suggest that you play around with clicking the different buttons in different orders, and watch how the query string changes in your browser’s address bar. I hope that will make clear the difference between using "?" and "".

5 Likes

9 posts were split to a new topic: Displaying GET values as a learning tool

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.