Jump to content

Form action to same page


NotionCommotion

Recommended Posts

I've heard multiple recommendations when a form needs to post to itself.  Some say use PHP_SELF, others say leave it blank (even though it doesn't validate), etc.  Please provide the best practice, and reasons why.  Thank you

echo("<form method='post' action='{$_SERVER['PHP_SELF']}'>");
echo("<form method='post' action=''>");
Link to comment
Share on other sites

Using the raw value from PHP_SELF makes the form susceptible to XSS attacks. More information can be found here:

http://seancoates.com/blogs/xss-woes

 

To avoid this and still have the form direct the information back to itself, you can leave the action attribute blank. I tried validating a form with the action attribute blank and it passed. Have you tried validating your form here:

http://validator.w3.org/

 

If you don't want to leave the action attribute blank, you could also use your script's name. For example:

<form method="post" action="my-form-script.php">
Link to comment
Share on other sites

 

To avoid this and still have the form direct the information back to itself, you can leave the action attribute blank. I tried validating a form with the action attribute blank and it passed. Have you tried validating your form here:

http://validator.w3.org/

 

 

Yes, this is what I get.  You get something different?

 

 

  1. error.png Line 7, Column 38: Bad value for attribute action on element form: Must be non-empty.
            <form method='post' action=''>
    Syntax of URL: Any URL. For example: /hello, #canvas, or http://example.org/. Characters should be represented in NFC and spaces should be escaped as %20.
<!DOCTYPE html>
<html>
    <head>
        <title>xxx</title>
    </head>
    <body>
        <form method='post' action=''>
            <input type='submit' value='Save'>
        </form>
    </body>
</html>
Link to comment
Share on other sites

I usually just do 

<html>
<head><title>#</title></head>
<body>
<div id="form">
     <form action='' method='POST'>
     <td><b>Text:</b><input type='text'></td>
     </form>
</div>
</body>
</html>

Idk something like that i have always left it blank unless you want another page to process it & i have never had a problem.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.