I have a <select>
with only one option, i want to get the rest of the options from a database using PHP and then populate said <select>
with the PHP result using AJAX.
Unfortunately my code is not working, im not surprised as im new to both AJAX and jQuery but i dont get any errors to guide myself through the issue.
The PHP works as expected because its used in another part of the site and i have no issues with it so my error must be in the AJAX (no big surprise here).
Below my HTML:
<select class="custom-select my-1 mr-sm-2" id="producto" name="producto" required>
<option disabled selected value>Elegir...</option>
</select>
Below my AJAX code:
<script type="text/javascript">
$(document).ready(function() {
$("#producto").click(function() {
$.ajax({
url: 'fetch_lista_productos_compra.php',
type: 'get',
success: function(data) {
$("#producto").append(data);
}
});
});
});
</script>
Below my PHP code:
<?php
require $_SERVER['DOCUMENT_ROOT'].'/testground/php/db_key.php';
$conn = mysqli_connect($servername, $username, $password, $dbname);
$sql_query = mysqli_query($conn, "SELECT * FROM productos WHERE disponibilidad = 'disponible'");
while ($row = mysqli_fetch_assoc($sql_query)) {
$titulo = $row['titulo'];
echo <<<EOT
<option value="$titulo">$titulo</option>n
EOT;
}
?>
As always any kind of help is greatly appreacited and thanks for your time.
IMPORTANT EDIT
<select>
has a duplicated id, perhaps i should i have stated this from the start as i know think thats whats causing my issue.
The fact is that in this <form>
the selects are created dynamically on user request. They click on Add product and a new select with the products avaiable is created, i know the id/name of said input must have [ ] (id="producto[]"
) for it to be converted into an array but i dont know how to make the AJAX deal with this dynamically created reapeated selects issue. I apologise for this belated aclaration i realise now it is of the utmost importance.
4
Answers
As it turns out the code in the question was actually working properly, the problem was not the code itself but the fact that, as i stated (sadly on a later edit and not right from the start) in the question, there where more than one
<select>
using the same id, therefore everytime the AJAX was executed the result was appended to the first<select>
only, and it was appended over and over again everytime i clicked it, my solution was unifiying both the AJAX that populated the<select>
and the jQuery that created the said dynamic<select>
's all in one script thus solving all of my issues at once.Below my jQuery/AJAX code:
I want to thank everyone that took the time to help me out with this Swati, Serghei Leonenco and Scaramouche, this community is truly amazing.
You have to edit your response from this:
To this:
But i suggest to use
JSON
to get a response and parse it on client side.You can do this by pushing all values in to response array and use
json_encode()
. At the end you get straight response from the server with just a values, and append thous values whatever you need on client side.Update
Also you append data to your existing
select
options. You can just add thous by editing this:Test result:
Please don’t construct your html on the server, try to just send the raw data and then construct the html client-side, it’s better that way and it leaves room for changing the view later without much coupling.
That said, instead of doing:
just do
then in the client
Regarding your edit about several
select
sI don’t have access to your server of course so I’m using a mock service that returns JSON. IDs are dynamically generated and all data loading occurs asynchronously after you click on the button.
Try using your url and modify the success function according to your html.
HIH