Welcome Guest, Not a member yet? Register   Sign In
how to use set_select on a form to show the old value?
#1

(This post was last modified: 05-14-2024, 12:26 AM by kcs.)

Hi,
I am not managing to make the set_select work on a form.
I am following the documentation, I call my form using form_open inside my view:
PHP Code:
<?= form_open('admin/bookings/update/'.$booking->id'id="formBookings"'?>
<?= $this
->include('/admin/bookings/form_fields'); ?>
<button class="submit">Update</button> 

Inside the form, here's a select:
PHP Code:
    <label>Status <?= $booking->status?></label>
    <select name="status" id="status">
        <option>Select</option>
        <option value="pending" <?= set_select('status'$booking->status?>>Pending</option>
        <option value="option" <?= set_select('status'$booking->status?>>Option</option>
        <option value="confirmed" <?= set_select('status'$booking->status?>>Confirmed</option>
    </select> 
 
I added the booking status inside the label for testing, so I can see that it returns a value. But which ever value is returned, the select does not take it, it always shows 'select'. 
What am I doing wrong?
Reply
#2

https://codeigniter.com/user_guide/helpe...m_dropdown
option must contain "selected=selected". Learn HTML syntax
Simple CI 4 project for beginners codeigniter-expenses
Reply
#3

(This post was last modified: 05-14-2024, 12:25 AM by kcs.)

(05-13-2024, 03:50 PM)ozornick Wrote: https://codeigniter.com/user_guide/helpe...m_dropdown
option must contain "selected=selected". Learn HTML syntax

I know it must contain selected, I am expecting the set_value to do that for me. 

You are pointing to another part of the documentation that is not the one I am referring to. In the one I am mentionning, here's what is said and the example:
Code:
set_select($field[, $value = ''[, $default = false]])

If you use a <select> menu, this function permits you to display the menu item that was selected.

The first parameter must contain the name of the select menu, the second parameter must contain the value of each item, and the third (optional) parameter lets you set an item as the default (use boolean true/false).

Example:

<select name="myselect">
    <option value="one" <?= set_select('myselect', 'one', true) ?>>One</option>
    <option value="two" <?= set_select('myselect', 'two') ?>>Two</option>
    <option value="three" <?= set_select('myselect', 'three') ?>>Three</option>
</select>

It's what I am doing, so what is missing?


// Edit 
I confirm after multiple attempts, the example is correct, and the set_select function should return selected (which is enough in HTML to make the option selected). But In my case, it looks like it returns nothing. So if any one has an idea what's happening?
Reply
#4

No. You're looking at the wrong place. I pointed to "form_dropdown" so you don't need to write the <option> yourself Inside, he will choose the current "selected" himself.
Second, no, you don't need to write selected. This is what the function outputs.
PHP Code:
        if ($input === null) {
            return ($default === true) ? ' selected="selected"' '';
        

You have error in request set_select('request', $booking->status) Why 'request'?
Simple CI 4 project for beginners codeigniter-expenses
Reply
#5

(05-14-2024, 12:24 AM)ozornick Wrote: You have error in request set_select('request', $booking->status) Why 'request'?
You're right, typo while retyping the code, I corrected it on the post.

For you're other suggestion to change the way I am calling the options.. I have just 3 of them, they work already for creating bookings in my code, so I was expecting to use them with the set_value when I edit the booking following the example that should work no?
Reply
#6

(This post was last modified: 05-14-2024, 12:34 AM by ozornick.)

HTML before output:
Code:
<form action="http://ci-demo/" method="post" accept-charset="utf-8">
                <select name="myselect">
                    <option value="one" selected="selected">One</option>
                    <option value="two">Two</option>
                    <option value="three">Three</option>
                </select>
                <button type="submit">Send</button>
</form>

HTML after output:
Code:
<form action="http://ci-demo/" method="post" accept-charset="utf-8">
                <select name="myselect">
                    <option value="one">One</option>
                    <option value="two" selected="selected">Two</option>
                    <option value="three">Three</option>
                </select>
                <button type="submit">Send</button>
</form>

Run code: 
PHP Code:
<?= form_open() ?>
                <select name="myselect">
                    <option value="one" <?= set_select('myselect''one'true?>>One</option>
                    <option value="two" <?= set_select('myselect''two'?>>Two</option>
                    <option value="three" <?= set_select('myselect''three'?>>Three</option>
                </select>
                <button type="submit">Send</button>
<?= form_close() ?>

Check value dump($booking->status);
Simple CI 4 project for beginners codeigniter-expenses
Reply
#7

(This post was last modified: 05-14-2024, 12:59 AM by kcs.)

I will try that, in the meantime, here's what I did for testing:
PHP Code:
    $myValue $booking->status;
    $testSetSelect set_select('status'$booking->status);
    $testSetValue set_value('status'$booking->status);
    $testSetCheckbox set_checkbox('status'$booking->status);
    $testSetRadio set_radio('status'$booking->status);
    $showResults = [$myValue,$testSetSelect,$testSetValue,$testSetCheckbox,$testSetRadio];

    dd($showResults); 
 
The result is quite strange:
Code:
$showResults array (5)
⇄0 => string (7) "request"
⇄1 => string (0) ""
⇄2 => string (7) "request"
⇄3 => string (0) ""
⇄4 => string (0) ""

(05-14-2024, 12:31 AM)ozornick Wrote:
HTML after output:
Code:
<form action="http://ci-demo/" method="post" accept-charset="utf-8">
                <select name="myselect">
                    <option value="one">One</option>
                    <option value="two" selected="selected">Two</option>
                    <option value="three">Three</option>
                </select>
                <button type="submit">Send</button>
</form>

No, html output has not changed, the default value one is still the one selected.


Quote:Check value dump($booking->status);
It is not empty, it contains the value that should be reassigned to the option (see my test... actually where it looks like only the set_checkbox function works :S).
Reply
#8

(This post was last modified: 05-14-2024, 01:02 AM by ozornick.)

You have values as "pending option confirmed"  Where is the "request" from?
The code is working, the error is somewhere in your data

Quote:No, html output has not changed, the default value one is still the one selected.
Yes, Because there is no such value in the POST response, it is selected by default.
Simple CI 4 project for beginners codeigniter-expenses
Reply
#9

(This post was last modified: 05-14-2024, 01:49 AM by kcs.)

I am starting to make sense of what you are saying I think.
My values come from this method:
PHP Code:
    public function edit($id) {
        $this->viewData['booking'] = $this->bookingModel->select('bookings.id as id,status,mode,date,start_time,location,location_zipcode,location_address,presentations_id,clients.id as clients_id, clients.client_name,presentations.pres_name')
            ->where('bookings.id'$id)
            ->join('presentations''presentations_id = presentations.id''inner')
            ->join('clients''clients_id = clients.id''inner')
            ->first();;

            //dd($this->viewData['booking']);
      return view('admin/bookings/edit'$this->viewData);
    

So my edit view is a get request. Do you mean that those functions set_value, set_input, set_checkbox and set_radio are intended to only work after a post? 
So basically when I do have a form I use for edits, that takes data from a call to the database, I cannot use those.  Sleepy  Okay it took me some time but I am getting it now. I thought I could use them here, but that's not the way to go. Thanks for helping me.

May I ask still something though: why do I get a result in my test for the set_checkbox?

Looking at the form_helper, I can see that first all those functions try to get an old value – which is what I was expecting to achieve.
PHP Code:
        // Try any old input data we may have first
        $input $request->getOldInput($field); 

The set_checkbox seems to find it. Not sure exactly then what happens there?
Reply
#10

(This post was last modified: 05-14-2024, 07:48 AM by demyr.)

(05-13-2024, 11:38 AM)kcs Wrote: Hi,
I am not managing to make the set_select work on a form.
I am following the documentation, I call my form using form_open inside my view:
PHP Code:
<?= form_open('admin/bookings/update/'.$booking->id'id="formBookings"'?>
<?= $this
->include('/admin/bookings/form_fields'); ?>
<button class="submit">Update</button> 

Inside the form, here's a select:
PHP Code:
    <label>Status <?= $booking->status?></label>
    <select name="status" id="status">
        <option>Select</option>
        <option value="pending" <?= set_select('status'$booking->status?>>Pending</option>
        <option value="option" <?= set_select('status'$booking->status?>>Option</option>
        <option value="confirmed" <?= set_select('status'$booking->status?>>Confirmed</option>
    </select> 
 
I added the booking status inside the label for testing, so I can see that it returns a value. But which ever value is returned, the select does not take it, it always shows 'select'. 
What am I doing wrong?

So, Why don't you just use :

PHP Code:
<option value="pending" <?php if($booking->status == "pending"){echo 'selected';} ?>>Pending</option> 

By the way, keep your status as tiny_int in your database instead of varchar.

0 = pending
1 = option
2 = confirmed
etc

Additionally, in your method be careful with double semicolon
Code:
->first();; and try to place "where" after "join"s.
I enjoy web design   d-templates.com
Reply




Theme © iAndrew 2016 - Forum software by © MyBB