Yii2.0 / Session INSIDE a formular - yii2

What ist yii2.0 analogy to following php-command?
<input type="text" name="zahl_down" id="zahl" value="<?php if (!empty($_SESSION['zahl_down'])) echo $_SESSION['zahl_down'];?>"></p>
All I achieved is code like this,which is not as it should be,of course:
<?= $form->field($model, 'zahl_up')->hint("Ihr zuvor eingegebener Wert:".$session['zahl_up']) ?>

The fact that this happens in a form does not affect how sessions work.
See How to retrieve the session values in Yii 2 for a quick how-to on Yii 2 session handling.

Related

How to change the field value in update form in Yii2

In my update form, I want to show the original value rather than it's id. Below is my _form.
<div class="mdc-meter-config-form">
<?php $form = ActiveForm::begin(['id'=>'con','options' => ['enctype' => 'multipart/form-data']]) ?>
<label class="control-label">Select Meters</label><br />
<input type="text" id="the-mter-id" class="form-control col-md-12" value="<?=$model->meter_id?>" />
<div style="clear: both;"></div>
<div id="selected_mters_container"></div>
<div style="clear: both;"></div>
<br/>
<?= $form->field($model, 'p_id')->dropDownList([''=>'Please Select']+\common\models\MdcProtocol::toArrayList()) ?>
<?= $form->field($model, 'time')
->dropDownList([''=>'Please Select','5' => '5', '10' => '10', '15' => '15','20'=>'20'])->label("Set Time (in seconds)") ?>
<div class="form-group">
<?= Html::submitButton(Yii::t('app', 'Save'), ['class' => 'btn btn-success']) ?>
</div>
<?php ActiveForm::end(); ?>
In above view I want to change the 2 value to its actual one which is 4A60193390662
Dont know why you are not using the relation here for the meter to display the meter serial rather than the id.
Easiet way to avoid any further work as you want to just display it, 2 things you need to do
Assign the value to the input manually via relation.
Make the field disabled to exclude it out of the model collection array. (in case you use active form for the field).
I assume you have a relation to the meter in your current model with name getMeter() and the number you are trying to display 4A60193390662 is in field serial_number. You can change accordingly.
If you havent defined a relation than define one now
public function getMeter(){
return $this->hasOne(Meter::class,['id'=>'meter_id']);
}
Although you are using a custom html field you can still use the active form and mark the field disabled so that the activeform wont submit it with the model collection array and there wont be errors when saving the records like
meter_id should be an integer.
echo $form->field($model, 'meter_id')->textInput(['disabled' => 'disabled', 'value' => $model->meter->serial_number]);
try add an entry also for the field with value 4A60193390662 eg: $model->meter_name
<input type="text" id="the-mter-name" class="form-control col-md-12" value="<?=$model->meter_name?>" />
and if you don't want see the entry for meter_id .. you could use hidden input for this so mantain the related avlue for the model loading and popolation
And as suggested in comment by Michal Hyncica (sorry for improper charset ) you could wrap the input inside a check fo new record
<?php
if(!$model->isNewRecord) {
echo '<input type="text" id="the-mter-name" class="form-control col-md-12" value="' . $model->meter_name . '" />';
}

Customize login cakephp3

I have a form like this:
email
[input]
password
[input]
Login-Button
I want to eliminate the "email" and "password" text and put them inside the inputs like placeholder and also change the "login" text to "LOGIN".
This is my login ctp.
<fieldset style="background:#F44336">
<?= $this->Form->create() ?>
<?= $this->Form->input('email') ?>
<?= $this->Form->input('password') ?>
<?= $this->Form->button('Login') ?>
<?= $this->Form->end() ?>
<div class="forgot">
<p>Forgot Password?</p>
</div>
Your question is confusing and vague.
But I believe you are asking you dont want the value or words "Email" and "Password" to be next to the input but rather inside the input textbox?
If so, you can add HTML code inside the HTML form
<input type="email" name="email_form" placeholder="Email">
Keep in mind that is HTML 5.
In HTML 4, the traditional way would to be adding an attribute to the HTML form to email or password as value="Email..."
Above code is correct but you should use helpers.
So a better way to achieve the same task with Html helpers is:
<?= $this->Form->input('email', array('label' => false)); ?>
<?= $this->Form->input('password', array('label' => false)); ?>
It's a suggestion to make your code better which we all should focus on.

Yii2: how to stop inbuilt id generation by yii2 for each field in form

When i am adding form to view & specifying parameters as
<?= $form->field($model, 'form_name', ['options' => ['id' => 'formName', 'name' => 'formName']])->textInput(); ?>
But, when i run in the browser & check for view page source, there it shows me
<input type="text" id="submitform-form_name" class="form-control" name="SubmitForm[form_name]">
this disturbs my javascript calling for field input. How to stop yii2 from generating its own id???
You are passing options to ActiveField. If you want override id and name attributes, pass them in textInput() options like so:
<?= $form->field($model, 'form_name')->textInput(['id' => 'formName', 'name' => 'formName']) ?>
Generated html output will be:
<input type="text" name="formName" class="form-control" id="formName">
Note that after that client validation for this attribute will stop working and that attribute won't be massively assigned.

fill a select html with mysql table rows

I'm writting a web app, I'm doing a html form with javascript to validate the data, I'm using a servlet to insert the data in a DB, but I need, when the form load, fill a select in the html form with the rows of a table in a MySql DB, I think I have to make a query in a ResultSet and then fill the select in the html form with this info, and then use the HttpServletResponse, but I have no idea how to make this
Use PHP to access the database and then put it into the form. Something like this:
$id = $_GET['id'];
$result = mysql_query("SELECT * FROM table_name WHERE id='$id'");
$info = mysql_fetch_array($result))
$name = $info['name'];
$email = $info['email'];
$anotherField = $info['another_field'];
In this, the info array has all of the database data in it of the id. But of course you would change the values and stuff. Change table_name to the table name, and if you are going to do something in the URL like ?id=373384 or something, then this will work. (I would assume you are doing it this way.) But, if it isn't 'id', just change WHERE id='$id' to what is in the database to identify a line.
$info['name']; would be the "name" column in your table. But, you can change 'name' and stuff to what you have in your database.
Here is the form code to pre-fill it with data:
<form>
<input type="text" name="name" value="<?php echo $name ?>" />
<input type="text" name="name" value="<?php echo $email ?>" />
<!--etc...-->
<input type="submit" value="Submit" />
</form>
You would need to change a lot of this, but I'm sure you get the point of it. :)
Note: This is not a simple copy+paste script. You will have to do a lot of charing because I don't know what your table values are and stuff.
If you have any questions, please do ask.

textarea post into mysql not working

I have a textarea in a form that im trying to POST into a mysql database. However, when i insert the values, i get about 5 lines in the database. 4 of them have no information or minimal information and one of the lines is how i want it. How can i get rid of the other four lines? thanks.
EDIT: actually none of the lines have all the right info in the right spots and i have double checked the inset statement to make sure everything lines up.
the code:
<form method='POST' action="index.php">
<textarea id ='answerbox' autocomplete='off' cols="80" rows="5" name='answer'></textarea>
<input type='submit' value='submit'>
<?php
include 'connect.php';
$date=date("Y-m-d");
$time=time();
$answer=['answer'];
$user=$_SESSION['username'];
$id=$_GET['id'];
$put=mysql_query("INSERT INTO solutions VALUES ('','0','$date','$time','$user', '$answer')");
?>
Try:
<?php
if($_POST['answer'] != '')
{
include 'connect.php';
$date=date("Y-m-d");
$time=time();
$answer= $_POST['answer'];
$user=$_SESSION['username'];
$id=$_GET['id'];
$put=mysql_query("INSERT INTO solutions VALUES ('','0','$date','$time','$user', '$answer')");
}
?>
Your textarea is named as "answer" while its ID is "answerbox". You should be able to retrieve its value through $_POST['answer'];