admin管理员组

文章数量:1122832

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 / and the form is on this link, after submitting form the url should be /?taskname=abc but it is redirecting to the main site page like this /?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();
 }

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();
 }
Share Improve this question asked Oct 24, 2017 at 6:58 mohsinmohsin 1772 silver badges9 bronze badges 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. – Jacob Peattie Commented Oct 24, 2017 at 7:26
Add a comment  | 

1 Answer 1

Reset to default 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/

本文标签: plugin developmentHow to Maintain url on form submit