0

In my case I have to submit form on same file which is a class without using any javascript or hook, the form action is action="<?php echo $_SERVER['PHP_SELF']; ?>". It is submitting the form but redirecting to the main page of the site. If the page link is http://sitename.com/my-link/ and the form is on this link, after submitting form the url should be http://sitename.com/my-link/?taskname=abc but it is redirecting to the main site page like this http://sitename.com/?taskname=abc. What I am missing here

class tasks{

     function search_form(){

        <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="GET" >
          <input type='text' name='taskname' >
        </form>

     }

   function search_form_submit(){   
        echo $_GET['taskname']; 
    }

}

 $tasks_obj = new tasks();

 if($_SERVER['REQUEST_METHOD'] == 'GET') {  
 $tasks_obj->search_form_submit();
 }
3
  • 4
    an empty action="" attribute will always post to the current URL
    – Z. Zlatev
    Commented Oct 24, 2017 at 7:04
  • hmmmm wise answer :)
    – mohsin
    Commented Oct 24, 2017 at 7:09
  • 2
    "without using any javascript or hook" Why can't you use a hook? Templates are not the place to process forms. Commented Oct 24, 2017 at 7:26

1 Answer 1

0

If your form is embedded in a shortcode or some other front end form, I would post to the permalink for the page. That's more reliable than using action="" in my experience. Then output something on that page to acknowledge the submission or show an error.

$global $post;
printf('<form method="post" action="%s">',get_permalink($post->ID));

or in for a form on the admin side:

printf('<form method="post" action="%s">',admin_url('admin.php?page=custom_form'));

Within admin_url, include the portion of the page url that comes after wp-admin/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.