Avant toutes choses meilleurs vœux pour cette nouvelle année.
Je suis novice en ajax.
Comme vous pouvez le deviner j'ai un problème avec mon code.
Je souhaite alimenter une liste déroulante via une une table mysql et ajax en utilisant la librairie prototype.
L'erreur que j'ai dans la console d'erreur firefox est une erreur de syntaxe que je n'arrive pas à déchiffrer.
Voici mon select
Code : Tout sélectionner
<?include "_debut.php";?>
<script type="text/javascript" src="lib/prototype.js"></script>
<script type="text/javascript" src="scripts/albums_ajax.js"></script>
<div id="Content">
<h1>Albums</h1>
<div class="block">
<form action="albums_ajax.php" method="post" enctype="multipart/form-data">
<table>
<tr>
<th>Selectionnez Votre Artiste</th>
<td><select id="artistes" name="artistes"></select></td>
</tr>
<tr>
<th>Entrez L'Album</th>
<td><input type="text" name="album" /></td>
</tr>
<tr>
<th>Pochette Album</th>
<td><input type="file" name="album_cover" size="50" /></td>
</tr>
<tr>
<th>Année de Sortie</th>
<td><input type="text" name="album_release_date" /></td>
</tr>
<tr>
<th></th>
<td><input type="submit" value="Valider" id="go" /></td>
</tr>
</table>
</form>
</div>
</div>
<?include "_fin.php";?>
Code : Tout sélectionner
<?php include_once("connexion.php"); ?>
<?php
mysql_select_db($database, $base);
$query_artists = "SELECT * FROM artists ORDER BY art_name ASC";
$artists = mysql_query($query_artists, $base) or die(mysql_error());
$row_artists = mysql_fetch_assoc($artists);
$result = array();
?>
<?php
do {
$result[] = $row_artists;
} while ($row_artists = mysql_fetch_object($artists));
echo '{"artists":'.json_encode($result).'}';
?>
Code : Tout sélectionner
var getArtist = {
response: null,
init: function() {
if ($('artistes')) {
while ($('artistes').options.length > 0) {
$('artistes').remove(0);
}
var opt = new Element('option');
opt.value = "";
opt.text = "";
try {
$('artistes').add(opt, null);
}
catch(ex) {
$('artistes').add(opt);
}
var url = 'getArtists.php';
new Ajax.Request(url, {
method: "get",
onSuccess: function(transport) {
getArtist.response = transport.responseText.evalJSON();
getArtist.fillInArtistSelect();
}
});
}
},
fillInArtistSelect: function() {
getArtist.response.artists.each(function(elt) {
var opt = new Element('option');
opt.value = elt.art_id;
opt.text = elt.art_name;
try {
$('artistes').add(opt, null);
}
catch(ex) {
$('artistes').add(opt);
}
}),
[color=#FF0000]},[/color]
};
document.observe('dom:loaded', getArtist.init);