Bonjour avant toutes choses je tiens à m'excuser des possibles erreurs que je vais effectuer concernant ma requête sur le forum (c'est mon premier post). J'espère que vous ne m'en tiendrez pas rigueur.
Mon problème est le suivant. Je viens de mettre à l'ajax et à prototype.
Je souhaite inserer une ligne dans ma base de données avec prototype par l'intermediaire d'un formulaire que voici:
Code html : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13 <form action="artists2.php" method="post" enctype="multipart/form-data"> <table> <tr> <th>Ajouter Artiste </th> <td><input type="textarea" name="art_name" /></td> </tr> <tr> <th></th> <td><input type="submit" value="Valider" /></td> </tr> </table> </form>
Dans le <head> bien sur j'ai
A l'insertion de mon artiste un tableau doit se créer dynamiquement pour m'afficher mon insertion avec deux liens images (une image pour modifier, l'autre pour supprimer).
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2 <script type="text/javascript" src="lib/prototype.js"></script> <script type="text/javascript" src="scripts/artists2.js"></script>
Dans mon html je cree un <div id="artists"> ou le tableau devra être créé.
Voici mon artists2.js
Pour finir mon php pour inserer l'artiste (getArtistAdded.php)
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63 var artist = { response: null, responseArtist: null, init:function(){ var url = 'getArtistAdded.php'; new Ajax.Request(url, { method: 'get', onSuccess(transport){ artist.responseArtist = transport.responseText.evalJSON(); if (artist.responseArtist.artist[0] !=false){ artist.writeArtistTable(artist.responseArtists.artist); } else { $('artist').innerHTML = "Veuillez entrer un artiste"; } } }); writeArtistTable : function(response){ var table = document.createElement('table'); table.id="tbArtist"; var tr0 = document.createElement('tr'); var th0 = document.createElement('th'); var td0 = document.createElement('td'); th0.innerHTML = "ID"; tr0.appendChild(th0); th0 = document.createElement('th'); th0.innerHTML = "Artiste"; tr0.appendChild(th0); th0 = document.createElement('th'); th0.setColSpan(2); th0.innerHTML = Action; tr0.appendChild(th0); table.appendChild(tr0); response.each(function(elt){ var tr1 = document.createElement('tr'); var td1 = document.createElement('td'); td1.innerHTML = elt.art_id; var td2 = document.createElement('td'); td2.innerHTML = elt.art_name; var td3 = document.createElement('td'); var imgModif = document.createElement('img'); imgModif.src = "modifier.png"; imgModif.alt = "modifier"; imgModif.title = "modifier"; imgModif.href="artist2.php?modif=elt.art_id"; td3.appendChild(imgModif); var td4 = document.createElement('td'); var imgSuppr = document.createElement('img'); imgSuppr.src = "supprimer.png"; imgSuppr.alt = "supprimer"; imgSuppr.title = "supprimer"; imgModif.href="artist2.php?suppr=elt.art_id"; td4.appendChild(imgSuppr); tr1.appendChild(td1); tr1.appendChild(td2); tr1.appendChild(td3); tr1.appendChild(td4); table.appendChild(tr1); }); $('artist').appendChild(table); } Event.observe(window, 'load', artist.init);
Code php : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 <?php include_once("connexion.php"); ?> <?php mysql_select_db($database, $base); function remplaceBR($chaine) { $chaine = str_replace("\r\n","<br>",$chaine); $chaine = str_replace("\n","<br>",$chaine); return $chaine; } if(isset($_POST['art_name'])){ $name = strip_tags($_POST['art_name']); // Premiere insertion sans l'id $texte = remplaceBR($texte); $query01="INSERT INTO artists(art_id, art_name) VALUES ('NULL', '$name')"; $result01=mysql_query($query01); $id=mysql_insert_id(); // On recupere l'id de l'artiste pour l'afficher $query_artist = sprintf("SELECT * FROM artists WHERE art_id = $id"); $artist = mysql_query($query_artist, $base) or die(mysql_error()); $row_artist = mysql_fetch_assoc($artist); $result = array(); } ?> <?php do { $result[] = $row_artist; } while ($row_artist = mysql_fetch_object($artist)); echo '{"artist":'.json_encode($result).'}'; ?>
Le problème vient forcemment d'artists.js car quand je mets le lien du php pour inserer getArtistAdded.php dans l'action du formulaire l'insertion se passe sans problème.
Je vous remercie d'avance pour votre aide et surtout votre compréhension ajax et prototype sont tout nouveau pour moi.
Partager