Creating a form in WordPress in a class-based plugin - php

I have a custom plugin that uses a class. In the class I add an admin dashboard menu item that displays a "Settings" form. Let's say the class looks like this:
class MyPlugin {
function __construct () { ... }
function show_menu_form () {
$url = admin_url("admin_post
echo <<<EOFORM
<form method="POST" action="$url"/>
Click submit to do stuff.
<input type="hidden" name="action" value="do_stuff"/>
<input type="submit/>
</form>
EOFORM;
add_action('wp_post_do_stuff', [$this, '_handle_stuff']);
}
function _handle_stuff () {
echo "Doing stuff...";
}
}
The form prints properly in the dashboard page. When I click submit, it goes to a completely blank screen. What I want it to do is:
Do something
Go back to the same page, showing a message like, "Stuff was done." above the same form, and the user can click the form submit again to do more stuff.
If I leave the action blank on the <form> tag it will go back to the same page, and I can manually process the get/post params myself... that seems like a bad solution (but the only one I can see so far).
Any tips?

You have 2 ways to do this:
Form submit to the same page and before rendering the form you check if the form already submitted. If yes then call a function to display "Stuff was done" and any further steps. If not, then display the form.
In this way, you won't need any javascript in your plugin and I IMOO will be simpler and straight forward.
Submit using AJAX and display the confirm message using javascript. You can read the docs AJAX in Plugins to see how to do this in the admin menu. This is might be a more elegant solution without requiring any page refresh.
Which one to use depends on the requirements of your plugin. Either way, don't forget to validate inputs

Related

How to show-up AJAX post data from WSForm Wordpress plugin form on the same page?

I have WSForm Pro form management plugin installed on my Wordpress site. The plugin utilizes AJAX as the only method to post a form's data so that only the form's area is updated on submit, not the whole page. On submit I need to show-up the form's data on the same page.
What I have done. Through adding a function to functions.php of my theme I'm able to get the form's data as a session variable:
// Add action to process WS Form form data
add_action('wsf_action_tag', 'wsf_action_function', 10, 2);
// My function for action
function wsf_action_function($form, $submit) {
// Get submit value (Change '123' to your field ID)
$submit_value = wsf_submit_get_value($submit, 'field_18');
session_start();
$_SESSION['form'] = $submit_value;
// Do something with $submit_value here
// ...
}
I also added the following php-code to the page with the form to show-up the session variable above:
<?php
session_start();
$form = $_SESSION['form'];
var_dump($form);
?>
The problem. When I submit the form, nothing changes except built-in success message (as supposed with AJAX), though on page reload the submitted data is shown-up successfully. If I could have my own AJAX script, I would want to modify it slightly to reload the whole page. But since I use a third-party plugin, it's makes the task too complicated.
The question is how would I change any part of my code to show-up the form's data on submit on the same page?
Found a workaround in WSForm functionality! From "Edit Form" choose "Actions", "Add", "Redirect" and point the page's redirect to itself.
Click to to see the screenshot

Submit form to different action with typo3 flow

I have a page with a form for creating users. A user has an hobby, which can be created on the same page by clicking on the second button which opens the page for creating a hobby. After creating the hobby, the previous user form should be shown with the user input inserted before going to the hobby page.
Is there a way to do something like with typo3 flow / fluid without using AJAX?
I tried to submit the input to a different action by clicking on the createHobby button --> The action redirects to the new hobby page, where the user can create the hobby and after creation it should redirect back to the user form with the already filled out input fields by the user .
I used...
<input type='submit' value='Create' formaction='/hobby/create' />`
to achive this, but it seems there are some problems with the uris... I get following error:
#1301610453: Could not resolve a route and its corresponding URI for the given parameters.
I think the using the attribute formaction is not a good solution for every case, as it is not supported by IE < 10 as you can see here. I think a JavaScript backport should also be considered (dynamically change the action attribute of the form when clicking on the second button, before actually submitting the form).
Concerning your error, you should not – and probably never – use direct HTML input, instead try to focus on Fluid ViewHelpers, which allow TYPO3 to create the correct HTML input.
Try this instead:
<f:form.submit value="Create" additionalAttributes="{formaction: '{f:uri.action(controller: \'hobby\', action: \'create\')}'}" />
You can make an $this->forward(...) in an initializeActiondepending on an param of your action.
Lets imagine your default Form action is "create". So you need an initializeCreateAction:
public function initializeCreateAction()
{
if ($this->arguments->hasArgument('createHobby')) {
$createHobby = $this->request->getArgument('createHobby');
if ($createHobby) {
$this->forward('create', 'Hobby', NULL, $this->request->getArguments());
}
}
}
Now you must name your input createHobby and assign your createAction this param:
In fluid:
<f:form.button type="submit" name="createHobby" value="1">Create Hobby</f:form.button>
In your Controller:
public function createAction($formData, $createHobby = false)
{
...
}
can you explain something more ... what you show has nothing to do with typo3, I don't know where you inserted that, what version of typo3 , using any extension extra ?

cakephp 3.0 submit part of form

is there way to submit part of a form without refreshing the whole page? Basically, I want to add a search box with a button in the view, when I click this button it runs a function / action in the controller. My apology if this has been asked before but I've searched for couple of hours and I couldn't understand the ones I came across.
This page will help you learn about how CakePHP handles AJAX:
http://book.cakephp.org/3.0/en/controllers/components/request-handling.html
Now, be aware the default behavior of a form is submit itself to a page or to the same page (reloads). If you want to prevent the form from submitting (reload the page) have something like this:
<form onsubmit = myFunc() >
..
function myFunc(){
//Send the ajax request. You can use JQuery
//Handle response
return false; //This will prevent the page reload...
}

Is it possible to submit your own admin forms in Wordpress?

I'm wondering if is possible and how could do it, to create my own page in Wordpress, after that I want to submit the form, do something with the info and then save it. It sounds simple, but for so much I find no results.
I saw that I can use do_settings_sections to create my custom form easily, but it auto-saves, what I want is to refer to a function and later show another screen.
Far at the moment, I am close, calling a function from y form with this:
function edit_university() {
echo " *test* ";
}
add_action( 'admin_post_edit_university', 'edit_university' );
I submit the form and I see the echo test, but I get out of the Wordpress admin page. It's just a simple page with echo test; how can I resend it later to another page?

Calling a php function with the help of a button or a click

I have created a class named as "member" and inside the class I have a function named update(). Now I want to call this function when the user clicks 'UPDATE' button.
I know I can do this by simply creating an "UPDATE.php" file.
MY QUESTION IS :-
Can I call the "update() function" directly without creating an extra file? I have already created an object of the same class. I just want to call the update function when the user clicks on update button.
an action.php example:
<?
if (isset($_GET[update])){
$id=new myClass;
$id::update($params);
exit;}
//rest of your code here
?>
Your button is in your view. Your method is in your class . You need a controller sitting in the middle routing the requests. Whether you use 1 file or many files for your requests is up to you. But you'll need a PHP file sitting in the middle to capture the button click (a POST) and then call the method.
As far as I know you can't do this without reloading your page, checking if some set parameters exist which indicate the button is clicked and than execute the button. If this is what you are looking for... yes it is possible on page reload. No as far as I know it is not possible directly because your php-function has to parse the results again.
Remember that a Model-View-Controller way is better and that this will allow you to ajax (or regular) requests to the controller-class.
You do it on the same page and have an if statement which checks for the button submission (not completely event driven) like so
if (isset($_POST['btn_update']))
{
update();
}
<input type="submit" name="btn_update" value="Update" />
That will have to be wrapped in a form.
Or you could do it with AJAX so that a full page refresh isn't necessary. Check out the jQuery website for more details.

Categories