how to use set_select on a form to show the old value? |
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"') ?> Inside the form, here's a select: PHP Code: <label>Status <?= $booking->status; ?></label> 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?
https://codeigniter.com/user_guide/helpe...m_dropdown
option must contain "selected=selected". Learn HTML syntax (05-13-2024, 03:50 PM)ozornick Wrote: https://codeigniter.com/user_guide/helpe...m_dropdown 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]]) 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?
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) { You have error in request set_select('request', $booking->status) Why 'request'? (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?
HTML before output:
Code: <form action="http://ci-demo/" method="post" accept-charset="utf-8"> HTML after output: Code: <form action="http://ci-demo/" method="post" accept-charset="utf-8"> Run code: PHP Code: <?= form_open() ?> Check value dump($booking->status);
I will try that, in the meantime, here's what I did for testing:
PHP Code: $myValue = $booking->status; The result is quite strange: Code: $showResults array (5) (05-14-2024, 12:31 AM)ozornick Wrote: 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).
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.
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) { 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. 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 The set_checkbox seems to find it. Not sure exactly then what happens there? (05-13-2024, 11:38 AM)kcs Wrote: Hi, 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. |
Welcome Guest, Not a member yet? Register Sign In |