Readers like you help support MUO. When you make a purchase using links on our site, we may earn an affiliate commission. Read More.

Hi there, I've read the amazing post by James Bruce the WordPress guy but I am just wondering why James used a hidden field in the form to call the PHP function.

How is this working? For a project, I have to provide some filters to customize the post query and I would like to return the result via Ajax. With this tutorial I am getting close to the solution but I would like to understand what is going on with that hidden field since I did not find any documentation about it...

MAKEUSEOF VIDEO OF THE DAY
SCROLL TO CONTINUE WITH CONTENT

Thanks guys!

James Bruce
2013-04-19 07:04:26
Hi Andrea. I guess I should Answer this ;) It's been a year since I wrote that, but I believe it's because the form action would normally be a URL of the PHP to process the file - in this case, we don't want that (unless you want to deal with users who don't have javascript enabled), so I left it blank. The hidden form field called "action" - though you could call it anything really, "task" or "functionToCall" - is simply to tell wordpress which function we want to process the form. This allows to have multiple functions for processing different forms, all going via WP. If I were to program the same thing nowadays, I would actually bypass the complications of Wordpress and just write my own PHP AJAX handler instead. The method outlined in this post would only be preferred if I were making a plugin for public distribution; on MakeUseOf for instance, we don't do that.