WordPress admin-post.php cannot handle form - php

I'm using admin_post_{$action} to handle the form. I just copy-paste the example from https://developer.wordpress.org/reference/hooks/admin_post_action/.
I created front-page.php in the theme directory and just paste in:
<form action="http://www.example.com/wp-admin/admin-post.php" method="post">
<input type="hidden" name="action" value="our_action_hook">
<input type="submit" value="Submit">
</form>
Then this in the functions.php :
add_action( 'admin_post_our_action_hook', 'am_our_action_hook_function' );
function am_our_action_hook_function() {
// do something
}
But I got an error in the console: 400 bad request . If I make this as a plugin, it works. But if I do it in a theme, it doesn't...

Error 400 shows your request is not valid from your side.
you can check the console-> network and you can find the your request.
anyway, replace the form action with this:
<form action="<?php echo admin_url( 'admin-post.php' ); ?>" method="post">
<input type="hidden" name="action" value="our_action_hook">
<input type="submit" value="Submit">
</form>

Related

admin-post.php not calling custom handler hook

I've got a basic email form that I want to use a custom handler on. I tried following this tutorial https://blog.osmosys.asia/2016/05/07/handling-form-submissions-in-wordpress/ but it seems that my custom hook is not being called.
<form class="" action="<?php echo admin_url('admin-post.php') ?>" method="post">
<input type="hidden" name="action" value="bb_submit_email">
<input type="email" id="email" name="email" value="example#email.com" required>
<?php wp_nonce_field('submit-email', '_mynonce'); ?>
<input type="submit" class="float-right btn btn-primary form-button" value="Submit">
</form>
<?php
add_action('admin_post_bb_submit_email', 'bb_handle_email_submit');
add_action('admin_post_nopriv_bb_submit_email', 'bb_handle_email_submit');
function bb_handle_email_submit() {
$logger = wc_get_logger();
$logger->add('submit-email-debug', 'triggered!');
if ( !isset($_POST['_mynonce']) || !wp_verify_nonce($_POST['_mynonce'], 'register-user')) {
return;
}
wp_remote_post('example.com', $_POST );
}
I tried using wp_ajax instead but that didn't work either. Anyone see something I'm missing here?
Update: I don't know if this helps but I checked the POST Request data and it has the following:
action: bb_submit_email
email: example#email.com
_mynonce: a2c0e80de7
_wp_http_referer: /
That all looks right to me?
I moved this
<?php
add_action('admin_post_bb_submit_email', 'bb_handle_email_submit');
add_action('admin_post_nopriv_bb_submit_email', 'bb_handle_email_submit');
function bb_handle_email_submit() {
$logger = wc_get_logger();
$logger->add('submit-email-debug', 'triggered!');
if ( !isset($_POST['_mynonce']) || !wp_verify_nonce($_POST['_mynonce'], 'register-user')) {
return;
}
wp_remote_post('example.com', $_POST );
}
to functions.php and it started working. Honestly not sure why?

How to keep values in re-opened form?

I have two PHP files: index.php with a form, and data.php for further data manipulation.
Here is index.php:
<?php
session_start();
require_once("../index.conf");
$language = new Language();
$lang = $language->getLanguage(#$_POST['lang']);
?>
...
<form name="myForm" action="data.php" method="post" onsubmit="return validateForm()">
<input type="text" name="title" placeholder="e.g.: my_title" value="<?php echo isset($_POST['title']) ? $_POST['title'] : '' ?>">
...
<button class="btn_r" name="submit" type="submit">
<?php echo $lang['submit-button']; ?>
</button>
Here is data.php:
// success message
echo sprintf('
<div class="success">Good job! Your file <em>'.$file.'</em> was successfully created with this HTML content:<br>
<form name="goto_preview" method="post">
<input type="hidden" name="img_title" value="'.$title.'">
<button class="btn_l" name="reset" type="submit" name="logout" formaction="preview.php">PREVIEW RESULTS</button>
<button class="btn_r" name="submit" type="submit" name="continue" formaction="index.php">CORRECT DATA</button>
</form>
</div>',$img_name);
I try to return the user to the form, with the original values filled in if correction is needed. But the form always opens empty. What is wrong with my code?
Nothing is wrong with your code. That's just the way php forms work, you're redirecting to a new page therefore the form isn't filled out. To change that you could pass the POST arguments that you receive in the data.php and pass them back to index.php where you set them as default values (if present)

PHP Submit button doesn't have any effect (PhpStorm)

I updated the question.
Since the last code was pretty complex and even after fixing the stuff it didn't work, I executed the below simple code to check if things work. Even this code doesn't work. Whenever I click on the submit button, it again returns a 404 error.
Yes, I placed the PHP code in the body as well to check if this work but it doesn't.
<?php
if(isset($_POST['submit'])) {
echo("Done!!!!");
} else {
?>
<html>
<head>
<title>Echo results!</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<input name="submit" type="submit" value="submit"/>
</form>
<?php
}
?>
</body>
</html>
Try giving the button_create as name of the submit button
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
if(isset($_POST['button_create'])) {
<td><input type="submit" name="button_create" id="button_create" value="Create Table!"></td>
change these lines see how you go from there
There are a couple of things wrong here, method should be POST instead of GET. The name attribute of text fields should be used when receiving the values. The submit button name should be used to check whether the button is clicked or not. See the example given below.
<?php
if (isset($_POST['submit'])) {
$ex1 = $_POST['ex1'];
$ex2 = $_POST['ex2'];
echo $ex1 . " " . $ex2;
}
?>
<form action="" method="post">
Ex1 value: <input name="ex1" type="text" />
Ex2 value: <input name="ex2" type="text" />
<input name="submit" type="submit" />
</form>
Echo results!
<?php
if(isset($_POST['submit'])) {
echo("Done!!!!");
} else {
?>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<input name="submit" type="submit" value="submit"/>
</form>
<?php
}
?>
this is for your updated question

need to do a query when button is pressed php

I want to so when a button is pressed on my page it runs a query in the database
View
<form method="POST" action="evcccontroller/query">
<input type="submit" name="nw_update" value="NW_Update"/>
</form>
Controller
public function query()
{
// load the index view
$this->load->view('querycheck');
}
querycheck.php
<?php
if(isset($_POST['nw_update']))
{
echo("You clicked button one!");
//and then execute a sql query here
}
else
{
echo" dhur";
}
An Error Was Encountered
The action you have requested is not allowed.
I think problem with your action attr of form try like
<form method="POST" action="<?php echo base_url(); ?>/evcccontroller/query">
<input type="submit" name="nw_update" value="NW_Update"/>
</form>
add base url to form action
action="<?php echo base_url(); ?>/evcccontroller/query"
Just try
<form method="POST" action="<?php echo site_url('evcccontroller/query')?>">
<input type="submit" name="nw_update" value="NW_Update"/>
</form>
code is looking fine. I think the form action url is wrong. try this:
<form method="POST" action="<?=base_url('index.php/evcccontroller/query')?>">
<input type="submit" name="nw_update" value="NW_Update"/>
</form>

send post request in joomla 2.5

I create a new module that send user information to the data base.
But i have trouble with post request!
<form action="send.php" id="send_c" method="post" >
<p>1</p>
<input type="text" name="pib_u" />
<p>2</p>
<input type="text" name="email_u" />
<p>3</p>
<input type="text" name="phone_u" />
<p>4</p>
<select>
<?php
foreach ($list as $item) {
echo '<option>'.$item->cat_name.'</option>';
}
?>
</select>
<p></p>
<input type="submit" value="Go" />
</form>
when i trying to send information from the form i get the 404 error with send.php
PS: send.php in module folder
please help me to resolve this problem! Thx!
try
<form action="<?php echo JURI::root().'modules/your_modname/send.php';?>" id="send_c" method="post" >
get request your with
$_POST['var_name'] or JRequest::getVar ('var_name');
If you send the request to the same page where the module is loaded leave the action attribute empty.
<form action="" id="send_c" method="post" >
The way you wrote it it looks for {current url}send.php and it doesn't find it.

Categories