Support » Fixing WordPress » Trying to get back the url for form action

  • Hi,

    Warning: I am absolutely a moron when it comes to scripting or programming…

    Nevertheless, I hope someone will be kind enough to explain what I need to change to make the below form work. Right now, as it is, the basename(__FILE__) thing in there returns a path WordPress can’t find (this form is located in and the “echo basename(__FILE__)” thing returns“.

    If I put this on a separate page, outside of WP, it doesn’t work at all.

    <form method="post" action="<?php echo basename(__FILE__) ?>">
            <p><label for="p">Enter the text to hash:</label><br /><input id="p" type="text" name="p" value="<?php echo $p ?>" /></p>
            <p><input type="submit" name="submit" value="Hash It" /></p>
        <?php /* If there is a posted value use it */ ?>
        <?php if ($p): ?>
        <hr />
        <h2>Table of hash values for <em><?php echo $p ?></em> based on algorithm</h2>
                <th>Hashed value of <em>your text</em></th>
            <?php /* Loop through each hash algorithm, colorizing every other row */ ?>
            <?php $on = false; foreach (hash_algos() as $algo): ?>
            <tr<?php if ($on): ?> class="on"<?php endif; ?>>
                <td class="algo"><?php echo $algo ?></td>
                <td class="hash"><?php echo hash($algo, $p) ?></td>
        <?php $on = !$on; endforeach; ?>
Viewing 7 replies - 1 through 7 (of 7 total)
  • Nobody can help me with this basename(__FILE__) question? Or willing?

    You want the form to submit to the page itself that the form is on and the form may be on any page?

    Is it actually returning this in your view source?
    <form method="post" action="">
    Or is it returning this causing it to be a relevant link, thus double /form/form?
    <form method="post" action="form">

    If its just returning “form” then
    <form method="post" action="/<?php echo basename(__FILE__) ?>">

    Though if you don’t always expect the path to start from root you can try $_SERVER[‘REQUEST_URI’];

    Hi darrellonsite,

    I copied the PHP code above onto a WP page template and renamed the template to something meaningful. I then created a page without content, based on the template. That gives me the form. When entering some text in the entry field and clicking the submit button, the page template name (hash-page.php) is added to the URL like so:
    changes into

    But it should return again –or am I missing something?

    The last part on the page (which will still have the form on it), should then show a table with hash keys.

    I’ll try your suggestion but I’m not even sure that what I’m trying to do, will work at all…

    OK, I found a way around it, but I wanted to do this ‘dynamically’. Perhaps it’s silly to want to do it that way…

    I just replaced the whole <?php echo basename(__FILE__) ?> by the actual URL, and this does execute the script properly.

    Of course, if I ever decide to change the name of the page I should change the code again. I don’t know if other people often use PHP outside the WP environment, but if there are some, it could be interesting to know how to do this by using a PHP statement or variable, or whatever this particular code snippet is called –told you I’m an absolute programming/scripting disaster; and most people don’t believe me when I tell them I have tried to learn (I even had a test to see how good I am at programming skills; it turns out I am very good at solving very simple problems, but can’t make it on my own when things get a tiny bit more complicated).

    Anyway, I am very grateful for your thoughts darrellonsite –also if you (and perhaps others) would decide to turn my ‘solution’ into a dynamic one.

    Did you try the REQUEST_URI? Perhaps I should have given example. It worked for me.

    <form method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>">

    I just did some playing around.

    Template Name: URL Test
    Template file: url_test.php
    Page created: “Test the URL” (with: /test-the-url/ as permalink)

    Here is what each gives. Although using it alone is not safe from hack attempts.

    $_SERVER[‘REQUEST_URI’]; returns: /test-the-url/
    $_SERVER[‘PHP_SELF’]; returns: /index.php
    basename(__FILE__); returns: url_test.php

    There are so many ways to sanitize a URL depending on purpose I finally come across the WordPress built in way. At least do this.

    <form method="post" action="<?php echo esc_url($_SERVER['REQUEST_URI']); ?>">

    Probably the safest way. Get it directly from WordPress.
    <form method="post" action="<?php echo esc_url(get_permalink()); ?>">

    Thanks darrellonsite!

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Trying to get back the url for form action’ is closed to new replies.