• Abhi raj

    (@theonethatstheone)


    i want the user to give some input and i want to use that input as arguments and fetch an API and print result after he clicks submit button, all this in the plugin

    i tried this, but now i don’t even know how to print something after user has clicked the submit button. i’m very new to php, help me guys

        function pwd()
        {
          
            $content = "<div>
            <h1>Enter the input</h1> </br>
            <form id='formid' action='' method='POST'>
            <input type='text' name='inputname' value='' />
            <input type='submit' name='submit' value='submit' />
            </form>
            </div>";
            
            return $content;
    
            if($_POST['submit']) {
    
              return "you have clicked submit";
            }
    
        }
        add_shortcode('abc','pwd');
Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    The action='' form tag attribute means the form data is sent to the page on which the form resides. If this page is an actual WP “page” post type, it’s template code could be altered to handle to form submission. In fact, the if($_POST['submit']) conditional of your code could also process the form submission before returning the message. But currently it will never execute because return $content; is always encountered first.

    If the code required to process the form data does not require WP resources, you can change the action attribute value to lead to a custom .php file which handles the form submission.

    If form handling requires WP resources and altering the source page’s code is not an option, either process the form as an Ajax request or route the submission through /wp-admin/admin-post.php. Either of these will require a hidden form field named “action”, whose value is used to create an action hook “handle” or name, to which you add a callback to process the form data and generate a response.

    Since you’re new to PHP, you might try the custom .php file approach first before dealing with any WP quirks. Then you’ll know at least the basics are working before adding WP complexity. If you need WP resources, you’ll need to temporarily hard code representative return values from WP function calls to simulate usage of WP resources.

    Thread Starter Abhi raj

    (@theonethatstheone)

    what if i want the form action to be done on the same post where i have added the shortcode? in that case what would action="" ?

    and how do i run a function after the submit button?

    Moderator bcworkz

    (@bcworkz)

    Yes, action="" form tag attribute submits to the same page the form is on. You can alter the page template code to handle form submission. Your code is more or less correct except you need to move return $content; to the end of the function declaration so the if($_POST['submit']) can be processed when appropriate.

    Of course you would add form data handling code above return "you have clicked submit";

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘how do i run a function after pressing a button in plugin’ is closed to new replies.