submit html form on self page

2019-01-21 21:34发布

问题:

I want html form submit on self page, how to use action atribute

<form action="">

or

<form action="#">

or

<form action="here write addres this page">

or just simple not use action atribute ?

what is much better way ?

回答1:

In 2013, with all html5 stuff, you can just ommit the 'action' attribute to self-submit a form

<form>

Actually, the Form Submission subsection of the current HTML5 draft does not allow action="" (empty attribute). It is against the spec.

From this other Stackoverflow answer



回答2:

You can leave action attribute blank. The form will automatically submit itself in the same page.

<form action="">

UPDATE #1

According to the w3c specification, action attribute must be non-empty valid url in general. There is also an explanation for some situations that action attribute may be leave empty.

The action of an element is the value of the element’s formaction attribute, if the element is a Submit Button and has such an attribute, or the value of its form owner’s action attribute, if it has one, or else the empty string.

So they both still valid and works:

<form action="">
<form action="FULL_URL_STRING_OF_CURRENT_PAGE">

UPDATE #2

If you are sure about your audience is using html5 browsers then you can even omit action attribute:

<form>


回答3:

If you are submitting a form using php be sure to use:

action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"

for security.



回答4:

Use ? ,

 <form action="?" method="post">

it will help you to stay on the same page



回答5:

You can do it using the same page on the action attribute: action='<yourpage>'



回答6:

try this same form action page url, it will direct the same page

form action="/cgi-bin/html5.cgi" method="get"