how to create a plain html form in yii2? - html

I tried e.g
<form id='blah' action='myActionController' method='post'>
//fields here
<input type='submit' name='submit' value='button name' />
</form>
after clicking the button, it does land to the www.mydomain.com/site/myActionController
but then, the page content is
Bad Request (#400)
Unable to verify your data submission.
The above error occurred while the Web server was processing your request.
Please contact us if you think this is a server error. Thank you.
I just don't want to use the active form...
so how to do the form in a plain html version without using the built-in yii
active form?

You need to add CSRF data for your submission to be verified.
Easiest way is to use static method \yii\web\Html::beginForm() that will do that for you (and take care of generating proper form tag).
The code to generate your form will be something like:
<?= \yii\web\Html::beginForm('myActionController') ?>
<input type='submit' name='submit' value='button name'>
<?= \yii\web\Html::endForm() ?>
You can of course switch CSRF verification off but this is not recommended. Without this verification your plain form will work.

In yii2 if you want a link for myActionController you should use this notation
(assuming that you have a controller named site and an action named myActionController)
<form id='blah' action='my-action-controller' method='post'>
//fields here
could be you need proper url and for this could be useful the UrlHelper
this way
use yii\helpers\Url;
.....
echo "<form id='blah' action='" . Url::to(['/site/my-action-controller']) .
"' method='post'>";

The best way is to clone existing ContactForm model which is already using sendEmail function and add your own properties, rules and attributeLables. On sendEmail function, you can include all necessary fields that you want to receive in ->setTextBody as a string and using concatenation as below:
public function sendEmail($email)
{
return Yii::$app->mailer->compose()
->setTo($email)
->setFrom([$this->email => $this->name])
->setSubject($this->subject)
->setTextBody($this->name. ' ' .'has requested'. 'space here'. $this->customfield. ' etc.')
->send();
}
After customizing the contactForm model, you can copy the actionContact on site controller and add it to your Form controller as actionIndex or anything, also copy Captcha and error on public function actions()
Now we will create a plain html form in yii2 as follows:
In my example i am using registerForm as my model(take note on how i use that model on id and name fields for yii2 to send the form without problems)
<?php $form = ActiveForm::begin(); ?>
<div class="col-md-12">
<input type="text" id="registerform-name" placeholder="Institution's Name*" name="RegisterForm[name]" required>
</div>
<div class="col-md-12">
<select id="registerform-institution" name="RegisterForm[institution]">
<option value="" disabled selected hidden>Select Institution's Type*</option>
<option value="Secondary School">Secondary School</option>
<option value="High School">High School</option>
<option value="College">College</option>
<option value="University">University</option>
</select>
</div>
<div class="col-md-12">
<input type="tel" id="registerform-number" placeholder="Institution's Phone Number*" name="RegisterForm[number]" required>
</div>
<div class="col-md-12">
<input type="text" id="registerform-pname" placeholder="Principal's Name*" name="RegisterForm[pname]" required>
</div>
<div class="col-md-12">
<input type="email" id="registerform-email" class="form-control" name="RegisterForm[email]" placeholder="Principal's eMail Address*" name="email" required>
</div>
<div class="col-md-12">
<?= $form->field($model, 'verifyCode')->widget(Captcha::className(), ['captchaAction' => '/register/default/captcha', 'template' => '<div class="row"><div class="col-lg-4">{image}</div><div class="col-lg-8">{input}</div></div>',
]) ?>
</div>
<div class="form-group">
<?= Html::submitButton('Register', ['class' => 'theme-btn theme-btn4']) ?>
</div>
Back to my model: i had the following Properties and my form is sending well with proper validation, etc:
<?php
namespace register\models;
use Yii;
use yii\base\Model;
class RegisterForm extends Model
{
public $name;
public $institution;
public $number;
public $pname;
public $email;
public $verifyCode;

Related

Keep the date value in a html form after it has been submitted

I have created this form which I have placed in the Wordpress divi theme code module on a page. It is used to return the stock availability for my shop that is connected to a 3rd party api.
After the form is submitted how do I get it to keep displaying the values that were submitted.
<p>Enter your Event Dates</p><br>
<form class="Dates" action="" method="post">
<span>
<label for="date_from">Date From:</label>
<input type="date" id="date_from" name="date_from" value="dd-MM-YYY">
</span>
<span>
<label for="date_to">Date To:</label>
<input type="date" id="date_to" name="date_to" value="dd_MM-YYY"><br>
</span>
<input type="submit">
</form>
This way:
<input type="date" id="date_from" name="date_from" value="<?php echo isset($_POST['date_from'] ? $_POST['date_from'] : '' ?>">
You can try writing a new script under all javascript definitions. Page must .php
<script>
document.getElementById("date_from").value = '<?php echo isset($_POST['date_from'] ? $_POST['date_from'] : '' ?>';
If you want to use jquery you should use this format:
$(function(){
$('#date_from').val('submitted value');
});
but jquery doesn't know what was sent before. So you can update your script like this:
$(function(){
$('#date_from').on("keyup",function(){
window.localStorage.setItem('date',$(this).val());
});
$('#date_from').val(window.localStorage.getItem("date"));
});

Display Data From Selected Value On the Select Tag to another Text Field

I want to call 2 values from database, to be placed on select "dropdown" and on input type text.
This might be considered as illustration :
When I select a vendor ID, I want attribute of selected value to be shown in Vendor Name field.
How I should do?
This is the code that i have to call the value for the tag :
in model :
function getAllGroups(){
$query = $this->db->query('SELECT idvendor, vendorname FROM tbvendor');
return $query->result();
//echo 'Total Results: ' . $query->num_rows();
}
in Controller :
public function addshipment(){
$data['title']= 'Vendor ID';
$data['groups'] = $this->m_shipment->getAllGroups();
}
in View :
<div class="form-group">
<label>Vendor ID:</label>
<div class="input-group">
<div class="input-group-addon">
<i class="fa fa-institution"></i>
</div>
<select class="form-control input-box" name="idvendor">
<?php
foreach($groups as $row)
{
echo '<option value="'.$row->idvendor.'">'.$row->idvendor.'</option>';
}
?>
</select>
</div>
</div>
And this is table on my database
I'm already succeed to call the idvendor to place it on Select dropdown field, but i still can't get a way to place the vendorname into input type text when I select the idvendor.
Hope someone can gimme an answer.
Thanks
Basically imho this is a pretty simple task - since you've posted no code i'll give you an example
your select box
<select name="">
<option value="" data-vendor-name="your vendor name">Vendor Name</option>
</select>
your input field
<input type="text" name="data-vendor-name" value="" />
and the jquery code
$( document ).ready(function() {
$('option[data-vendor-name]').on('change', function() {
$('input[name="data-vendor-name"]').val($(this).attr('data-vendor-name'));
});
});

post request not working Laravel 5

I am trying to submit a form using post method, but it does not seem to be working. I have enabled error debug on but still no error is shown. After submitting the form the same page is loaded without any errors.
This is my route
Route::post('/home' , ['as' => 'store-post' , 'uses'=>'FacebookControllers\PostsController#save']);
And my form is
{!! Form::open(['route'=>'store-post' , 'class'=>'form'])!!}
<div class="form-group">
<label for="textarea"></label>
<textarea class="form-control" rows="5" name="textarea">What's on your mind?</textarea>
</div>
<div class="form-group col-sm-12">
<input type="submit" value="Post" class="btn btn-primary pull-right">
</div
{!! Form::close() !!}
This is my Controller
class PostsController extends Controller
{
public function save(PostsRequests $request){
$input = $request->all();
$post = new Post();
$post->user_id = Auth::user()->id;
$post->content =$input;
$post->user()->associate(1);
$post->save();
/*return redirect('home');*/
return ('something');
}
}
After hours of searching and trying, finally, I found the solution. I was using my own request class and path was not correct so I corrected the path of PostsRequests and now it works.

Need Help In Creating Yii2 ActionForm

This is how I would like it to be displayed:-
<form action="?r=site/abc" method="POST">
<input name="url" type="text" class="txt-fld_" placeholder="Enter the website">
<input name="submit" type="submit" class="btn-" value="Check it now">
</form>
And currently, I've done this:-
<?php $form = ActiveForm::begin(
[
'action' => ['?r=site/abc']
]
);?>
<?php ActiveForm::end(); ?>
I know I can use this line to generate text fields:-
<?= $form->field($model, 'url') ?>
But there is no model involved. Just a simple form that sends a url to the abc action. Also how can I generate the submit button?
Even if you don't need the model for anything else, you should still have one for your data, so that the form will perform validation. You can get it to check that a valid url is submitted, and prevent malicious attacks. It only needs to be a simple model. Once you have a model, then you can use all the features of Activeform to generate fields.
The submit button can be generated using Html::submitButton()

Wordpress Custom Registration Form

I have a client that needs a custom registration form.
I need to make a custom design on this page
I need to add custom fields like First Name, Company, Phone, etc.
Someone can help me with this?
A better place to ask WordPress questions is probably on WordPress Answers. Anyhoo, if you want to solve this without plugins, you need three things:
A custom WordPress theme
A Page Template
A WordPress Page that uses the Page Template
When you have these three parts in place, you can do the following in your Page Template:
<?php
/*
Template Name: Registration
*/
global $current_user;
wp_get_current_user();
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$company = $_POST['company'];
if (($firstname != '') && ($lastname != '') && ($company != '')) {
// TODO: Do more rigorous validation on the submitted data
// TODO: Generate a better login (or ask the user for it)
$login = $firstname . $lastname;
// TODO: Generate a better password (or ask the user for it)
$password = '123';
// TODO: Ask the user for an e-mail address
$email = 'test#example.com';
// Create the WordPress User object with the basic required information
$user_id = wp_create_user($login, $password, $email);
if (!$user_id || is_wp_error($user_id)) {
// TODO: Display an error message and don't proceed.
}
$userinfo = array(
'ID' => $user_id,
'first_name' => $firstname,
'last_name' => $lastname,
);
// Update the WordPress User object with first and last name.
wp_update_user($userinfo);
// Add the company as user metadata
update_usermeta($user_id, 'company', $company);
}
if (is_user_logged_in()) : ?>
<p>You're already logged in and have no need to create a user profile.</p>
<?php else : while (have_posts()) : the_post(); ?>
<div id="page-<?php the_ID(); ?>">
<h2><?php the_title(); ?></h2>
<div class="content">
<?php the_content() ?>
</div>
<form action="<?php echo $_SERVER['REQUEST_URI'] ?>" method="post">
<div class="firstname">
<label for="firstname">First name:</label>
<input name="firstname"
id="firstname"
value="<?php echo esc_attr($firstname) ?>">
</div>
<div class="lastname">
<label for="lastname">Last name:</label>
<input name="lastname"
id="lastname"
value="<?php echo esc_attr($lastname) ?>">
</div>
<div class="company">
<label for="company">Company:</label>
<input name="company"
id="company"
value="<?php echo esc_attr($company) ?>">
</div>
</form>
</div>
<?php endwhile; endif; ?>
Now, when you want to retrieve the stuff you've stored, you need to know whether the information is within the User object itself or in metadata. To retrieve the first and last name (of a logged-in user):
global $current_user;
$firstname = $current_user->first_name;
$lastname = $current_user->last_name;
To retrieve the company name (of a logged-in user):
global $current_user;
$company = get_usermeta($current_user->id, 'company');
That's the basic gist of it. There's still a lot of stuff missing here, like validation, error message output, the handling of errors occurring within the WordPress API, etc. There's also some important TODO's that you have to take care of before the code will even work. The code should probably also be split into several files, but I hope this is enough to get you started.
An advantage of using a custom registration form is that modifying the code according to the user's needs becomes easy. For a custom submit form you can make use of existing hooks in Wordpress like template_redirect and then map that hook to some function which will do the post-processing of the form, like validation and submitting data to the site's database. You can refer to an in-depth article here.
<div class="employee">
<input type="hidden" name="show_msg">
<form name="customer_details" method="POST" required="required" class="input-hidden">
Your Name: <input type="text" id="name" name="customer_name">
Your Email: <input type="text" id="email" name="customer_email">
Company: <input type="text" id="company" name="company">
Sex: <input type="radio" name="customer_sex" value="male">Male <input type="radio" name="customer_sex" value="female">Female
<textarea id="post" name="experience" placeholder="Write something.." style="height:400px;width:100%"></textarea>
<input type="submit" value="Submit">
<!--?php wp_nonce_field( 'wpshout-frontend-post','form-submit' ); ?-->
</form></div>
PHP function
function wpshout_frontend_post() {
wpshout_save_post_if_submitted();
}
add_action('template_redirect','wpshout_frontend_post', 2);
A custom WordPress registration form has two major advantages over the standard form.
The first is the integration with the overall look and feel of the website theme. Standard forms often don’t work well with custom themes and there is always a chance that the custom CSS files do not render well with the form. A custom form, on the other hand, can be easily set up to work with custom CSS.
The second and more popular reason of using a custom registration form is the option of custom fields that are not included on the standard form. A small custom registration form speeds up the process and collects all the necessary data from a neat interface.
function wordpress_custom_registration_form( $first_name, $last_name, $username, $password, $email) {
global $username, $password, $email, $first_name, $last_name;
echo '
<form action="' . $_SERVER['REQUEST_URI'] . '" method="post">
First Name :
<input type="text" name="fname" value="' . ( isset( $_POST['fname']) ? $first_name : null ) . '">
Last Name:
<input type="text" name="lname" value="' . ( isset( $_POST['lname']) ? $last_name : null ) . '">
Username <strong>*</strong>
<input type="text" name="username" value="' . ( isset( $_POST['username'] ) ? $username : null ) . '">
Password <strong>*</strong>
<input type="password" name="password" value="' . ( isset( $_POST['password'] ) ? $password : null ) . '">
Email: <strong>*</strong>
<input type="text" name="email" value="' . ( isset( $_POST['email']) ? $email : null ) . '">
<input type="submit" name="submit" value="Register"/>
</form>
';
}
This form can be inserted anywhere by using the shortcode [wp_registration_form]. Here is the code snippet for setting up the shortcode:
function wp_custom_shortcode_registration() {
ob_start();
wordpress_custom_registration_form_function();
return ob_get_clean();
}
I hope that by now you have a fair idea of creating a WordPress custom Registration form.Still any confusion kindly check Build Custom WordPress Registration Forms