WordPress.org

Ready to get started?Download WordPress

Forums

Trying to get back the url for form action (8 posts)

  1. erikvlie
    Member
    Posted 3 years ago #

    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 http://url.com/form and the "echo basename(__FILE__)" thing returns http://url.com/form/form".

    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>
        </form>
    
        <?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>
        <table>
            <tr>
                <th>Algorithm</th>
                <th>Hashed value of <em>your text</em></th>
            </tr>
            <?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>
            </tr>
        <?php $on = !$on; endforeach; ?>
        </table>
  2. erikvlie
    Member
    Posted 3 years ago #

    Nobody can help me with this basename(__FILE__) question? Or willing?

  3. Darrell Schauss
    Member
    Posted 3 years ago #

    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="http://url.com/form/form">
    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'];

  4. erikvlie
    Member
    Posted 3 years ago #

    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:

    http://www.url.com/hash-key-generator/
    changes into
    http://www.thinck.com/hash-key-generator/hash-page.php

    But it should return http://url.com/hash-key-generator/ 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...

  5. erikvlie
    Member
    Posted 3 years ago #

    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.

  6. Darrell Schauss
    Member
    Posted 3 years ago #

    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

  7. Darrell Schauss
    Member
    Posted 3 years ago #

    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()); ?>">

    http://codex.wordpress.org/Data_Validation#URLs

  8. erikvlie
    Member
    Posted 3 years ago #

    Thanks darrellonsite!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.