Welcome Guest, Not a member yet? Register   Sign In
problems with set_select
#1

Hello all, I have problems with set_select() in an edit form.

I load data for an entity call edition and then call the view

Code:
$model = model('App\Models\EditionModel');
$data['edition'] = $model
->select('editions.*, cfg_courses.name as name, cfg_courses.certificationduration as certificationduration')
->join('cfg_courses', 'editions.cfgcourse_id = cfg_courses.id')
->where('editions.id',$edition_id)
->first();

if (strtolower($this->request->getMethod()) == 'get')
{
return view('edition/edit', ['data' => $data]);
}

In the view i put the code:

Code:
<div class="col-2">
<label for="opensubscriptions">Iscrizioni</label>
<select class="form-select form-select-sm text-uppercase" name='opensubscriptions' required>
<option value="NO" <?= set_select('opensubscriptions','NO')?>>NO</option>
<option value="SI" <?= set_select('opensubscriptions','SI')?>>SI</option>
</select>
</div>

This works well if there is a validation error. But i need, when i load the form to edit it, that the dropdown shows the value that was stored on the database.
If for instance on the database i have the value SI, it shows NO. How can i change the code so when i load the form to edit the record, the correct value is selected in the dropdown?
Reply
#2

(This post was last modified: 03-21-2024, 11:23 PM by luckmoshy.)

(03-21-2024, 11:17 AM)sunchaser Wrote: Hello all, I have problems with set_select() in an edit form.

I load data for an entity call edition and then call the view

Code:
$model = model('App\Models\EditionModel');
$data['edition'] = $model
->select('editions.*, cfg_courses.name as name, cfg_courses.certificationduration as certificationduration')
->join('cfg_courses', 'editions.cfgcourse_id = cfg_courses.id')
->where('editions.id',$edition_id)
->first();

if (strtolower($this->request->getMethod()) == 'get')
{
return view('edition/edit', ['data' => $data]);
}

In the view i put the code:

Code:
<div class="col-2">
<label for="opensubscriptions">Iscrizioni</label>
<select class="form-select form-select-sm text-uppercase" name='opensubscriptions' required>
<option value="NO" <?= set_select('opensubscriptions','NO')?>>NO</option>
<option value="SI" <?= set_select('opensubscriptions','SI')?>>SI</option>
</select>
</div>

This works well if there is a validation error. But I need, when I load the form to edit it, the dropdown shows the value stored on the database.
If for instance on the database I have the value SI, it shows NO. How do I change the code so that the correct value is selected in the dropdown when I load the form to edit the record?

I don't think you know much about form helpers, yes of course that form helper will help you to trigger if something goes wrong or does not validate your data so it is correct if it works as one in validation errors

However, if you need your data to remain for the sake of future usage, use the session library to store it after successfully removing that data from the session.
Codeigniter First, Codeigniter Then You!!
yekrinaDigitals

Reply
#3

(This post was last modified: 03-24-2024, 02:35 AM by sunchaser.)

[...]

Quote:I don't think you know much about form helpers, yes of course that form helper will help you to trigger if something goes wrong or does not validate your data so it is correct if it works as one in validation errors

However, if you need your data to remain for the sake of future usage, use the session library to store it after successfully removing that data from the session.
What you mean i don't know much about the form helper? don't you see i am passing in the controller the $data variable where are the info i loaded from the database?
I need an example where when i load the edit form, the dropdown shows the value that has been stored in the database, and if i change it and press SUBMIT and there is a validation error,
the dropdown shows the value that i have inputed.

I solved it with this solution, is there any better way to do it?

Code:
<select class="form-select form-select-sm text-uppercase" name='opensubscriptions' required>
<option value="NO"
<?
if ($data['method']=='get')
{
if ($data['edition']['opensubscriptions']=='NO')
  echo "selected";
}
else
{
echo set_select('opensubscriptions','NO');
}
?>>NO</option>
<option value="SI"
<?
if ($data['method']=='get')
{
  if ($data['edition']['opensubscriptions']=='SI')
  echo "selected";
}
else
{
echo set_select('opensubscriptions','SI');
}
?>>SI</option>
</select>
Reply




Theme © iAndrew 2016 - Forum software by © MyBB