Yii2 post form can't execute the action - yii2

I have a form which I trigger with jQuery trigger and if I set the method to GET it works. But if the method is 'POST' - 400 Bad Request.
My view looks like:
<?php if (Yii::$app->session->hasFlash('success')): ?>
<div class="success-message text-center"><?= Yii::$app->session->getFlash('success') ?></div>
<?php else: ?>
<form action="/order/confirmation" method="post" style="display: none" id="confirm-order">
<input type="hidden" name="order_id" value="<?= $_GET['id'] ?>"/>
<input type="hidden" name="confirm" value="1"/>
</form>
<?php endif; ?>
<?php
$this->registerJs("
(function(){
let form = $('#confirm-order')
form.trigger('submit')
})()
", \yii\web\View::POS_LOAD)
?>
And my action is:
public function actionConfirmation()
{
$id = Yii::$app->request->post('order_id');
$confirm = Yii::$app->request->post('confirm');
if($confirm){
$order = Orders::findOne(['id' => $id]);
$order->confirmed = 1;
$order->update(false);
Yii::$app->session->setFlash('success', Yii::t('app', 'Your personal information was deleted'));
}
return $this->render('confirmation');
}
In my UrlManager the URL is set like this:
'order/confirmation' => 'order/confirmation',
Also tried to put behaviors to the controller action like:
public function behaviors()
{
return [
'verbs' => [
'class' => \yii\filters\VerbFilter::className(),
'actions' => [
'confirmation' => ['POST', 'GET'],
],
],
];
}
Nothing helps. Summary - GET works. POST do not. What is my mistake here?

You should add input with CSRF token manually, like below, into your form as you are not using the ActiveForm which add the hidden input automatically to submit the POST request and all the POST requests require the CSRF token for preventing CSRF attacks, so I don't recommend you to disable it but you should provide the input manually:
<form action="/order/confirmation" method="post" style="display: none" id="confirm-order">
<?= \yii\helpers\Html::hiddenInput(Yii::$app->request->csrfParam, Yii::$app->request->getCsrfToken());?>
<input type="hidden" name="order_id" value="<?= \yii\helpers\Html::encode($_GET['id']) ?>"/>
<input type="hidden" name="confirm" value="1"/>
</form>

Related

Wordpress. Insert form values to custom table

How can i on click on submit send values to my custom mysql table??
Form html:
<form action="/ekz.php" method="post">
<input type="text" name="jjkk1">
<input type="text" name="jjkk2">
<input type="text" name="jjkk3">
<input type="text" name="jjkk4">
<input type="submit" name="submit">
</form>
Table name is wp_ekz2020:
Try doing like this :
<?php
if(isset($_POST['submit']))
{
global $wpdb;
$a=$_POST['jjkk1'];
$b=$_POST['jjkk2'];
$c=$_POST['jjkk3'];
$d=$_POST['jjkk4'];
$wpdb->insert( 'wp_ekz2020', array( 'num1' => $a, 'num2' => $b,'num3' => $c,
'num4' => $d), array( '%s', '%s','%s', '%s' ) );
}
?>
please try to place both html and php in single file
here is code i have try and it works
<form action="<?php the_permalink(); ?>" method="post">
<input type="text" name="num1">
<input type="text" name="num2">
<input type="text" name="num3">
<input type="text" name="num4">
<input type="submit" name="submit">
</form>
if(isset($_POST['submit']))
{
function insertnumber(){
global $wpdb;
$num1 = $_POST['num1'];
$num2 = $_POST['num2'];
$num3 = $_POST['num3'];
$num4 = $_POST['num4'];
$table_name = "newtab";
$wpdb->insert( $table_name, array(
'num1' => $num1,
'num2' => $num2,
'num3' => $num3,
'num4' => $num4
) );
}
insertnumber();
}
put <?php the_permalink(); ?> in your form action
i hope above code will help you : )
change table name and also variable
in this case, whenever you submit the form with action attribute then the page gets reloaded, and '$wpdb' returns null. So this will cause error.
To prevent this you have to include 'wp-load.php' in your file or you can use
add_action('init', 'your_function_name')

HTML mailto form

Can anyone help me with my feedback form for a website I am building?
I am not receiving any errors when I run this, although it doesn't actually send any email at all.
The following is the code I am trying to use:
<form action="mailto:admin#example.com" enctype="text/plain" method="post">
<p>Name: <input name="Name" type="text" id="Name" size="40"></p>
<p>E-mail address: <input name="E-mail" type="text" id="E-mail" size="40"></p>
<p>Comment:</p>
<p><textarea name="Comment" cols="55" rows="5"
id="Comment"></textarea></p>
<p><input type="submit" name="Submit" value="Submit"></p>
</form>
Sending mail from HTML Form is not a right option to do, you are using an HTTP Method (POST), so you have to set a http/https link from your action value. Otherwise, you have to send the email using the href tag, including a subject and body parameters.
E.g: Send Message
The mail application installed in your machine would automatically opened when you click that link.(Outlook, Gmail,...) and you can choose which one you have to send the mail within it.
you should maken a .php file
eg.
<form method="post" action="mail.php">
then in your mail.php put something like this:
<?php
session_start();
$to = "contact#webmagico.be"; // this is your Email address
$from = htmlspecialchars($_POST['email']); // this is the sender's Email address
$naam = htmlspecialchars($_POST['naam']);
$email = htmlspecialchars($_POST['email']);
$messageText = $naam . " " . $email . " wrote:" . "\n\n" . htmlspecialchars($_POST['bericht']);
$message = array(
"ontvanger" => $to,
"zender" => $from,
"naam" => $naam,
"email" => $email
);
$valid = true;
foreach($item in $message)
{
if(!isset($item) || $item === "")
{
$valid = false;
}
}
if($valid)
{
mail($to, $naam, $message, "From:" . $email);
}
else
{
$_SESSION['error'] = "Forgot something!"
}
/*file that gives the response*/
header('Location: thankyou.php');
?>
You can find different alternatives about a post-form on the web.
Hope this helps.

Update multiple rows with same id using foreach loop

I echo 2 input-fields (2 db columns) foreach row in the database (where id=$id) with each row's data being inside the input field. Admin-user can change data inside input-field and click update.
Currently, when clicking the update-button, it does update the rows (where id=$id) but it updates each row with the input-data of the first input field instead of updating input-data foreach input field. Thank you in advance for help.
view:
<form action='<?= site_url("admin/do_edit_page"); ?>' method='POST'>
<?php foreach($link_data as $row) : ?>
Link:<br />
<input type='text' name='page_link_title' value='<?= $row->link_title; ?>'>
<input type='text' name='page_link_sub_title' value='<?= $row->link; ?>'><br />
<?php endforeach; ?>
<input type='submit' name='update_site' value='Update'>
</form>
controller:
public function do_edit_page(){
$id = $this->input->post('page_id', TRUE);
$this->content_model->update_links($id);
}
model:
public function update_links($id){
foreach($_POST as $update_rows){
$update_rows = array(
array(
'page_id' => $id,
'link_title' => $this->input->post('page_link_title', TRUE),
'link' => $this->input->post('page_link_sub_title', TRUE)
)
);
$this->db->update_batch('content_links', $update_rows, 'page_id');
}
}
you have to move your form tag between the foreach loop so that it can submit the data of single row. something like this
<?php foreach($link_data as $row) : ?>
<form action='<?= site_url("admin/do_edit_page"); ?>' method='POST'>
Link:<br />
<input type='text' name='page_link_title' value='<?= $row->link_title; ?>'>
<input type='text' name='page_link_sub_title' value='<?= $row->link; ?>'><br />
<input type='submit' name='update_site' value='Update'>
</form>
<?php endforeach; ?>

Posting HTML form to new wp database table

I'm trying to make a HTML form post the input values to a custom table in the WordPress database. I've managed to get something to show up in a new row, but almost all of my values return N; instead of the value from the form.
Here's the code I have in my page template:
<?php
global $wpdb;
global $current_user;
$userID = $current_user->ID;
$brand = serialize($_POST["brand"]);
$url = serialize($_POST["url"]);
$sector = serialize($_POST["sector"]);
$keywords = serialize($_POST["keywords"]);
if (
'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && $_POST['action'] == 'updateSearch' ) {
$ufDataUpdate = $wpdb->insert( 'wp_wct3', array(
'date' => current_time('mysql'),
'userid' => $userID,
'brand' => $brand,
'url' => $url,
'sector' => $sector,
'keywords' => $keywords ) );
}
?>
<form method="post">
<label for="brand">Brand/Product Name: </label>
<input type="text" id="brand" placeholder="eg: Spidr" class="clearfix" required />
<label for="website">Website address: </label>
<input type="url" id="url" placeholder="eg: www.spidr.co.uk" class="clearfix" required />
<label for="sector">Market sector: </label>
<input type="text" id="sector" placeholder="eg: Internet Marketing Tools" class="clearfix" required />
<label for="keyword">Keywords/Phrases:<br><span class="orange">(comma separated)</span></label>
<textarea cols="0" rows="8" class="light" id="keywords" required></textarea>
<input type="submit" id="submit" name="submit" class="button-65 mobile-button" value="release the spiders!">
<?php wp_nonce_field( 'updateSearch' ); ?>
<input name="action" type="hidden" id="action" value="updateSearch" />
</form>
Where wp_wct3 is the database name and each item in the array is the name of each column in that table.
I'm not sure if my issue lies in this code, or in the set-up of the database itself. I've used the Wordpress custom tables plugin to make the new table. The brand, url and sector simply use the text definition, while the keywords use enum('0','1').
Anyone have any ideas why the values aren't returning and I'm just getting N; ?
this is my custom-form template.. it works for me
<?php
/**
Template Name: Custom-Form
* The template for displaying all pages.
*
* This is the template that displays all pages by default.
* Please note that this is the WordPress construct of pages
* and that other 'pages' on your WordPress site will use a
* different template.
*
* #package WordPress
* #subpackage Twenty_Twelve
* #since Twenty Twelve 1.0
*/
get_header(); ?>
<div id="primary" class="site-content">
<div id="content" role="main">
<?php
if (!empty($_POST)) {
global $wpdb;
$table = wp_achord;
$data = array(
'name' => $_POST['yourname'],
'chord' => $_POST['chord']
);
$format = array(
'%s',
'%s'
);
$success=$wpdb->insert( $table, $data, $format );
if($success){
echo 'data has been save' ;
}
}
else {
?>
<form method="post">
<input type="text" name="yourname">
<textarea name="chord"></textarea>
<input type="submit">
</form>
<?php } ?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_footer(); ?>
Figured out the main issue. I'd missed out the name attribute from the form itself, so my PHP wasn't picking up the field values!

Checkbox form sends zeros to mysql, whether boxes are checked or not

I've setup a checkbox form (each question has only one checkbox). The form is submitting, but it only sends zeros to mysql--whether the box has been checked or not. How can I get the correct values (1 or 0) sent to mysql?
Built in codeigniter/mysql.
FORM
<?php echo form_open('addFoo'); ?>
<input type="checkbox" name="foo1" value="" /> //I tried this w/values incl; still zeros
<input type="checkbox" name="foo2" value="" />
<input type="checkbox" name="foo3" value="" />
<input type="checkbox" name="foo4" value="" />
<?php echo form_submit('submit', 'Save Changes'); ?>
<?php echo form_close(); ?>
CONTROLLER
function addFoo()
{
if ($this->input->post('submit')) {
$id = $this->input->post('id');
$foo1 = $this->input->post('foo1');
$foo2 = $this->input->post('foo2');
$foo3 = $this->input->post('foo3');
$foo4 = $this->input->post ('foo4');
$this->load->model('foo_model');
$this->foo_model->addFoo($id, $foo1, $foo2, $foo3, $foo4);
}
}
MODEL
function addFoo($id, $foo1, $foo2, $foo3, $foo4) {
$data = array(
'id' => $id,
'foo1' => $foo1,
'foo2' => $foo2,
'foo3' => $foo3,
'foo4' => $foo4
);
$this->db->insert('foo_table', $data);
}
<input type="checkbox" name="foo2" value="" /> <-- needs a value. Your DB inserts a zero if it's empty.
I think the issue is likely on your DB end, not the form-end.