How do I make a PHP form that submits to self?
The proper way would be to use $_SERVER["PHP_SELF"]
(in conjunction with htmlspecialchars
to avoid possible exploits). You can also just skip the action=
part empty, which is not W3C valid, but currently works in most (all?) browsers - the default is to submit to self if it's empty.
Here is an example form that takes a name and email, and then displays the values you have entered upon submit:
<?php if (!empty($_POST)): ?>
Welcome, <?php echo htmlspecialchars($_POST["name"]); ?>!<br>
Your email is <?php echo htmlspecialchars($_POST["email"]); ?>.<br>
<?php else: ?>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
Name: <input type="text" name="name"><br>
Email: <input type="text" name="email"><br>
<input type="submit">
</form>
<?php endif; ?>
HTML Form Submit To Self
Aside from the fact the equals is missing from your action
attribute in your form element.
Your inputs need name attributes:
<tr>
<td>Username:</td>
<td><input id="loginUsername" name="loginUsername" type="text" /></td>
</tr>
Self Submit PHP Form Validation
NOTE : You have typo mistakes in your form tag.you used double quote inside double quote.
Insted of using this
<form method="post" action="<?php echo htmlspecialchars($_SERVER["REQUEST_URI"]); ?>">
and
if ($_SERVER["REQUEST_METHOD"] == "POST") {
Use
<form method="post" action="<?= $_SERVER['PHP_SELF'] ?>">
and
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['submitbutton'])) {
//SO IT WILL PERFORM ONLY WHEN SUBMIT BUTTON WAS PRESSED
For More You can Learn it here
Or Also Live Demo is available
submit form to self
I think you have a couple of options here.
Submit the form onto itself
<?php
if('POST' == $_SERVER['REQUEST_METHOD']) {
// process the form here
}
?>
<form action="" method="POST">
<input type="text" class="emailField" placeholder="email address" name="email" />
<button type="submit">
<div class="submit" />
</button>
</form>
Redirect back to the form
Add something like this in submit.php
where you want to show the form.
<?php
header('Location: http://example.org/form.php');
exit;
?>
php form action php self
How about leaving it empty, what is wrong with that?
<form name="form1" id="mainForm" method="post" enctype="multipart/form-data" action="">
</form>
Also, you can omit the action attribute and it will work as expected.
PHP - Self form submission: $_SERVER['PHP_SELF'] OR action= ?
You can use either (PHP_SELF or empty string). but why would you use FILTER_SANITIZE_STRING for this? You'd better to use htmlentities() instead of filter_var in this case, if your path contains filtered characters (e.g. <
), the form won't submit.
I prefer giving a string, <base href=>
can cause trouble when using empty values.
Example:
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']);?>" method="post">
</form>
Related Topics
Mock Private Method With PHPunit
How to Change Envelope from Address Using PHP Mail
Sanitizing Strings to Make Them Url and Filename Safe
How to Best Configure PHP to Handle a Utf-8 Website
How to Make This Preg_Match Case Insensitive
How to Clear Apc Cache Entries
Twitter API Returns Error 215, Bad Authentication Data
How to Find the Dates Between Two Specified Date
How to Delete All Cookies of My Website in PHP
PHP Fatal Error: Call to Undefined Function Json_Decode()
How to Generate a Screenshot of a Webpage Using a Server-Side Script
Elegant Way to Get the Count of Months Between Two Dates
Why Is This PHP Call to Json_Encode Silently Failing - Inability to Handle Single Quotes
Visual Studio Code PHP Intelephense Keep Showing Not Necessary Error
Do I Have to Guard Against SQL Injection If I Used a Dropdown