Eduardo Fuerte
asked on
Could you point how to actualize a combo with the current value retrieved from the database ?
Hi Experts
Could you point how to actualize a combo with the current value retrieved from the database ?
Accordingly to
The status value retrieved from DB at the textbox is 3 correspondent to "Em Desenvolvimento"
How to make the combobox to assume the same value "Em Desenvolvimento"
The code I have:
Thanks in advance!
Could you point how to actualize a combo with the current value retrieved from the database ?
Accordingly to
The status value retrieved from DB at the textbox is 3 correspondent to "Em Desenvolvimento"
How to make the combobox to assume the same value "Em Desenvolvimento"
The code I have:
<div class="form-group">
<label for="nome">Status</label>
<input type="text" class="form-control" name="idstatus" placeholder="Status" value="<?php echo $viewVar['produto']->getIdStatus(); ?>" required>
</div>
<div class="form-group">
<label for="nome">Status</label>
<label for="idstatus" class="validar field select">
<select name="idstatus" id="idstatus">
<option value="">Selecione ...</option>
<option value="1">Pendente</option>
<option value="2">Em Desenvolvimento</option>
<option value="3">Em Teste</option>
<option value="4">Concluido</option>
</select>
</label>
</div>
Thanks in advance!
ASKER
Hi
Very good for now.
But to obtain the data from the database isn't necessary to send an AJAX requisition?
I mean retrive data from status table...
Very good for now.
But to obtain the data from the database isn't necessary to send an AJAX requisition?
I mean retrive data from status table...
Don't you already have the statusID from the Database in your code. You seem to be echoing it out in your textbox:
<?php echo $viewVar['produto']->getId Status(); ?>
<?php echo $viewVar['produto']->getId
ASKER
So Eduardo. I'm struggling to understand exactly what you need.
If you have a combo box on your page like this:
To set the text in the combobox, you only need to know the value, such as 1, 2, 3, 4. If you have the idStatus from your DB (such as 3), then setting the value of the combobox to 3 will automatically select Em Teste as the text of the combobox.
Inversely, if you select Pendente from the combo box and submit your form, then a value of 1 will be sent back to the server, which is what you would insert into the idStatus field of your produto table.
Hope that makes sense, but if I've misunderstood, please let me know.
If you have a combo box on your page like this:
<select name="idstatus" id="idstatus">
<option value="">Selecione ...</option>
<option value="1">Pendente</option>
<option value="2">Em Desenvolvimento</option>
<option value="3">Em Teste</option>
<option value="4">Concluido</option>
</select>
To set the text in the combobox, you only need to know the value, such as 1, 2, 3, 4. If you have the idStatus from your DB (such as 3), then setting the value of the combobox to 3 will automatically select Em Teste as the text of the combobox.
Inversely, if you select Pendente from the combo box and submit your form, then a value of 1 will be sent back to the server, which is what you would insert into the idStatus field of your produto table.
Hope that makes sense, but if I've misunderstood, please let me know.
ASKER
@Chris.
What I meant is
This way the values 1,2,3,4 are harded code in PHP the page.
So, if a new status are inserted at status table 5 - XXX f.e. it's not reflected in the combo for choose.
So the combo options must to be dinamically actualized with the id/ name that came from status table.
What I meant is
This way the values 1,2,3,4 are harded code in PHP the page.
So, if a new status are inserted at status table 5 - XXX f.e. it's not reflected in the combo for choose.
So the combo options must to be dinamically actualized with the id/ name that came from status table.
Ah right. Got you. Basically, you'd need to run a query against your DB and then loop through the values. I don't know how you currently have your database connection set up but here's an example using PDO:
<?php
$dsn = "mysql:dbname=yourDb;host=localhost";
$user = "username";
$pass = "password";
try {
$dbh = new PDO($dsn, $user, $pass);
$dbh->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
$dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$status = $dbh->query("SELECT id, nome FROM status ORDER BY id")->fetchAll();
} catch(PDOException $e) {
die($e->getMessage());
}
?>
<select name="idstatus" id="idstatus">
<option value="">Selecione ...</option>
<?php foreach ($status as $option): ?>
<option value="<?php echo $option->id ?>"><?php echo $option->nome ?></option>
<?php endforeach; ?>
</select>
ASKER
Almost perfect.
Just one thing more, it must to assume the existent status value retrieved from DB in editions...
Just one thing more, it must to assume the existent status value retrieved from DB in editions...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
@Chris
Perfect!
Thanks a lot!
Perfect!
Thanks a lot!
No worries Eduardo :)
ASKER
@Chris
Could you give a look at this question derivation?
Amazingly a similar code doesn't works at a differente page
New question
Could you give a look at this question derivation?
Amazingly a similar code doesn't works at a differente page
New question
** Note: In your code you have the combo and textbox with the same name (idstatus). These should be different otherwise the jQuery selector may behave unexpectedly.
// jQuery:
Open in new window
// PHP:
Open in new window