How to handle forms in a wordpress plugin? - php

In my Plugin I want to work with the posted form data. My form input button looks like this:
<form action="<?php echo plugins_url('plugin_directory/my-plugin.php'); ?>"method="post">
<input type="submit" name="button1" class="button1" id="button1" value="button 1">
</form>
By clicking that button the plugin script will be executed again. The only thing that changed in my script is the value of a variable:
if (isset($_POST['button1'])) {
$file=plugins_url('file_1.txt');
var_dump('button clicked');
}
elseif(isset($_POST['button2'])){
$file=plugins_url('file_2.txt');
}
...
else {
$file=plugins_url('file_1.txt');//default value of variable when no button is clicked
}
But there seem to be some conflicts between wordpress and the plugin script because I got following error: "Call to undefined function add_action()"
So how to handle forms in a wordpress plugin correctly? The form action redirect is correct but the script cant be executed again. I also tried it with form action="#" and the absolute URL path I copied from the browser. The Page will display then but without the form if statements working.
Anyone knows what to do?

Simply load the wp-load.php in your plugin file then you can use add action

Related

PHP Form not posting the submit button correctly

I was doing an application form and the form is not working. When I click the submit button it doesn't even run the if statement to POST the variables.
My form tag looks like this:
<form method="post" enctype="multipart/form-data">
My submit button looks like this:
<button name="submeter" type="submit" class="btn btn-gfort">Submeter</button>
And the if statement in my form looks like this:
if(isset($_POST['submeter'])) {
I even tried to run a JS alert just to see if it actually enters the if statement but it doesn't. No console errors as well.Any help is appreciated
Have you added method="post" to the <form>-tag?
Well your codes looks fine. it seems you are submitting the form using post method on some php file. Please share the complete form element and the codes of the target file that you have defined in the action attribute of form tag.
the submit button sends the data to server and there you can process the data using any server side scripting language. your codes must look something like
<form action="process.php" method = "post">
<!-- your form controls -->`
<button name="submeter" type="submit" class="btn btn-gfort">Submeter</button>
</form>
then create another file on same location where html/php file containing this form is saved with name process.php, in which you can use following codes.
if(isset($_POST['submeter'])) {
// php codes
}
in case you want to subimt the form on the same page. make sure your file is php use action="#" in form tag and place the file of your webserver as you can not run php files directly from your filesystem but you need to run it through your webserver

Basic form in Wordpress template

I need to simply get a search term from a form into a variable. I have set up a basic form within a template file, that template file is then linked to a page within Wordpress admin. The problem I am getting is that the form doesn't submit so I am unable to use the variable. If I remove get_header(); from the template then the form will submit but obviously it break wordpress stuff.
Here is my form:
<form action="<?php the_permalink(); ?>" method="post" autocomplete="off" >
<label>
<input placeholder="Search…" name="qcsearch" type="text">
</label>
<input type="submit" name="submit" value="Submit">
</ul>
</form>
I have tried leaving out the action, using the template name which is qccerts.php and using $_SERVER['PHP_SELF']
Here is then what I am trying to do with the output:
if(isset($_POST['submit'])){
$searchterm = $_POST["qcsearch"];
}else{
$searchterm = '';
}
Its basically a simple search which tells the users if there is a file by the name they search. So I need to populate $searchterm so I can use it later down the page.
Any help appreciated.
It's difficult to determine what your exact problem is without a reproducible scenario. For example, without seeing your problem, I'm not sure whether the form is really not submitted at all, or submitted, but you did not see it being executed, or there is some Javascript which prevents your form from submitting. There is a possibility that the form is submitted to the wrong action as well.
However, if you intend to keep your search term accross the pages, you could add it into session. Let's imagine these functions:
function storeSearchTerm($searchTerm) {
$_SESSION["searchterm"] = $searchTerm;
}
function getSearchTerm() {
return isset($_SESSION["searchterm"]) ? $_SESSION["searchterm"] : "";
}
By calling these functions you can manage the search term, initializing it via storeSearchTerm($_POST["qcsearch"]) or something.
As about your actual form, if it does not work, then you can submit the form in Javascript, such as
document.getElementById("myForm").submit();
and make sure that this is triggered either via an onclick attribute, or a click event listener on the button created via addEventListener.
EDIT
It turns out that a class name was not well formed (case-sensitivity issue).

WordPress plugin custom admin page - form result shows on blank page

I am currently writing a WordPress plugin with the wppb wireframe. For the first time I need a custom settings page. I implemented it and also created a custom action hook to fire a function that gets some data as a result.
For now, I want to show this result in the admin page. But when I click on the button, the result is shown on a blank page rather than on the admin page.
How can tell my form and function to show the data in the admin dashboard and not on a blank page? I didn't find any help over google...
What I have so far:
On the admin settings page
<form action="http://myurl.com/wp-admin/admin-post.php" method="post">
<input type="hidden" name="action" value="search_api">
<input type="hidden" name="data" value="test">
<input type="submit" value="Submit">
</form>
The action hook
$this->loader->add_action('admin_post_search_api', $plugin_admin, 'admin_search_api');
The function
public function admin_search_api() {
echo $_POST['data'];
}
So, when I submit the form, a new page is loading and there is the expected result. But how can I frame it into the custom settings page? I am clueless...
So for clarification: I want to load the result of the form on the same page as the form. I am not really familiar with the wp-ajax, but might this be the answer?
Thanks in advance!
As said by Klaassiek, the codex has the right answer written in it.
Here is his comment:
You should indeed use wordpress’s ajax functionality for that. Plus, you will have to write a javascript function that does the ajax call and displays the result when the call is finished. Here is the documentation: codex.wordpress.org/AJAX_in_Plugins

Polymer iron-form - paper-button submission not working

I am trying to use the Polymer element Iron-Form to submit information into the $_POST array. However my submit button (a paper-button) - which should run the script to submit the form - does not seem to submit the form when pressed.
I'm new to Polymer and to PHP, so I'm not sure what is going wrong.
Form script
<form is="iron-form" method="post" id="insert-project-form" action="/form/handler">
<paper-input label="Project Title" name="title"></paper-input>
<paper-input label="Client ID" name="clientid"></paper-input>
<paper-input label="Working Hours" name="workhours"></paper-input>
<paper-button raised onclick="submitForm()">Submit</paper-button>
<script>
function submitForm() {
document.getElementById('insert-project-form').submit();
}
</script>
</form>
I have been having the same problem, and have been doing it the same way you do it. According to the Documentation it should work. But I have found a work around for this problem
Add a normal button for the submission and style its visibility to hidden
<button type="submit" id="SubmitButton" name="submit" style="visibility:hidden;"></button>
And in your javascript code change the submitForm function to this
function submitForm(){
document.getElementById('SubmitButton').click();
console.log("Submitted!")
}
And keep the paper button line the way it is.
<paper-button raised onclick="submitForm()">Submit</paper-button>
What it does is when the paper button is clicked, it triggers a click event on the normal submit. I'm pretty sure there are more efficient ways than this, but I will be using this for now.
<form is="iron-form" method="post" id="insert-project-form" action="/form/handler">
the attribute at the very end "action" needs to pass it to your php file. Assuming your php file is in a folder called "php". the solution to this would be
<form is="iron-form" method="post" id="insert-project-form" action="php/yourphpfile.php">
and your logic would be contained in the php file that submits it to a database if needed.

WordPress Custom Page Submit Form

I'm trying to write a simple script to write some data to mysql then read it. My code is working without a problem alone but I'm trying to use it inside a WordPress page, this is the point problem starts.
I have created a template file for WordPress, and using this template to create the page. Page shows up without a problem but whenever I try to submit the form inside it (my custom php form) it forwards me to index.php .
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<span>Enter Your Code : </span><br/>
<input type="text" name="sha256"><br/>
<p align = right><input type="submit" name="shaSubmit" value="Submit" /></p>
</form>
this is my form (inside custom php), and as you can see it posts the data to itself. At the the start of my custom php code I have
if(isset($_POST['Submit']))
But it doesn't matter, as soon as I click on button, it forward me to domain.com/index.php
Btw, this custom php is on a page with such url domain.com/custompage/
How can make this form work ?
ps. Code above is for reading from mysql.
You're using the following conditional statement if(isset($_POST['Submit'])) along with the submit button's named element name="shaSubmit".
You need to make those match.
Either by changing the name of your submit button to name="Submit"
or by changing your conditional statement to if(isset($_POST['shaSubmit']))
which is why your code is failing because of the conditional statement you've set is relying on a form element named "shaSubmit".
You need to change 2 things.
(1)make action=""
don't need to use action=" echo htmlspecialchars($_SERVER["PHP_SELF"])"
(2)if(isset($_POST['shaSubmit'])){
the code ....
}

Categories