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()
Related
I have this code:
<?
$page = $_GET['page'];
$find = $_GET['find'];
?>
<form method="post" action="#">
<input type="text" name="whatever" value="1">
<button class="btn btn-default" type="submit">Post this</button>
</form>
My initial URL is:
htttp://www.someplace.com?page=1&find=lookfor
When sending the post form I am getting back "page" and "find" vars along the "whatever" input value. Why? Is this happening because my form action is "#"?
By the way, this is what I want, this saves me the work of posting hidden input values. But I want to be sure it is valid.
Using action="#", you will submit the form to the current URL. Your GET vars are part of this URL so that is why you're getting them again.
More infos on this question.
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;
i am building online payment system for my project and i am new to yii2.
I have a simple html with the address http://localhost/ops/onlineStore/home.php with the following simple code
`
<form method="post" action="http://localhost/ops/frontend/web/index.php?r=site/payment">
<input type="hidden" value="35" name="price">
<input type="hidden" value="Breathney Spears Audio CD" name="product_name">
<input type="hidden" value="someSore" name="Store">
<button type="submit" name="redirect"><br>Breatheny Spears <br/>Audio CD<br/>35 Afs</button>
</form>
`
now i want to get these posted data from my yii2 app please guide me step by step what to do. Thank you
in your SiteController in PaymentAction:
you can simply get the values:
$_POST['price'];
...
or to use yii2 functions:
$post_arr = Yii::$app->request->post();
$price = $post_arr['price'];
...
If your form i realetd to a Yii2 model You can use Yii2 function load ..
$model = new MyModel();
$post = $model->load(Yii::$app->request->post();
then you can refer to each attribute as
$post->my_attribute;
http://www.yiiframework.com/doc-2.0/guide-db-active-record.html
http://www.yiiframework.com/doc-2.0/yii-base-model.html
http://www.yiiframework.com/doc-2.0/guide-structure-controllers.html
I have like 150+ forms with different input IDs. I need to collect all the data from the inputs and send them over through email directly without defining those fields. I see that the site https://formspree.io/ has that feature but I want to know how I can implement that myself. Thank you!
This how the site handles the form:
<form action="//formspree.io/your#email.com"
method="POST">
<input type="text" name="name">
<input type="email" name="_replyto">
<input type="submit" value="Send">
</form>
This allows all inputs to be sent directly to the given email.
Nevermind, got the answer.
foreach ($_POST as $key => $value)
$body .= $key . ' -> ' . $value . '<br>';
Im very confused and am not sure what to type into google to find out how to receive data that a user enters in an HTML text field. So that is why I am asking this question here. Any help would be really appreciated.
You can know which data has been entered with php, javascript, ajax,...
Your HTML text field:
<form action="process.php" name="myForm" id="idForm">
<input type="text" placeholder="Name" id="name"/>
<input type="submit" value="submit"/>
</form>
<!-- if you want to take it with javascript add that-->
<script type="text/javascript" src="test.js">
</script>
Get it with PHP:
process.php looks like that:
<?php
// Verify $_POST['name'] exist
if(isset($_POST['name']))
{
//verify $_POST['name'] is not empty
if(!empty($_POST['name']))
{
echo $_POST['name'];
}
}
?>
Get it with Javascript (This can able you not to refresh the page):
test.js:
var name = document.forms["myForm"]["name"].value;