Lucrez la un proiect personal in perioada asta si intampin o dificultate la javascript, sper ca ma puteti ajuta .
Am un formular dinamic care este paginat intr-un tabel dinamic, cu butoane de add row si remove row. Primul drop-down trimite prin javascript o variabila la fisierul getcateg.php si js-ul incarca drop-downul in pagina principala index2.php.
Scopul formularului este de a adauga cat mai multe row-uri in baza de date cu un singur click fara ca pagina sa faca refresh.
Formularul il gasiti la http://dev.storecheck.ro/index2.php
index2.php
Cod: Selectaţi tot
<input type="button" class="btn_rosu" onClick="addRow('dataTable')" value="Adauga" />
<input type="button" class="btn_rosu" onClick="deleteRow('dataTable')" value="Elimina" />
<p>(Se elimina numai randurile bifate)</p>
</p>
<table id="dataTable" class="bg" border="0">
<tbody>
<tr>
<p>
<td><input name="chk[]" type="checkbox" required class="btn_rosu" checked="checked" /></td>
<td>
<label>Brand</label>
<select name="BX_BRAND[]" class="btn_gri" required="required" onChange="showCateg(this.value)">
<?php
do {
?>
<option value="<?php echo $row_brand['brand']?>"><?php echo $row_brand['brand']?></option>
<?php
} while ($row_brand = mysql_fetch_assoc($brand));
$rows = mysql_num_rows($brand);
if($rows > 0) {
mysql_data_seek($brand, 0);
$row_brand = mysql_fetch_assoc($brand);
}
?>
</select>
</td>
<td>
<label for="BX_CATEG">Categ.</label>
<div id="showcateg"></div>
</td>
<td>
<label for="BX_REPER">Reper</label>
<select name="BX_REPER[]" class="btn_gri" id="BX_REPER" required="required">
<?php
do {
?>
<option value="<?php echo $row_reper['reper']?>"><?php echo $row_reper['reper']?></option>
<?php
} while ($row_reper = mysql_fetch_assoc($reper));
$rows = mysql_num_rows($reper);
if($rows > 0) {
mysql_data_seek($reper, 0);
$row_reper = mysql_fetch_assoc($reper);
}
?>
</select>
</td>
<td>
<label for="BX_PRET">Pret</label>
<input name="BX_PRET[]" type="text" required class="btn_gri" id="BX_PRET" size="5" /></td>
<td>
<label for="BX_PROMO">Promo</label>
<select name="BX_PROMO[]" class="btn_gri" id="select">
<option value="1">Da</option>
<option value="2">Nu</option>
</select></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<span class="register">
<input name="user" type="hidden" value="cristi" />
<input class="btn_verde" type="submit" value="Confirma »" />
</span>
</form>
script.js
(cel care adauga si elimina rowurile tabelului)
Cod: Selectaţi tot
function addRow(tableID) {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
if(rowCount < 30){
var row = table.insertRow(rowCount);
var colCount = table.rows[0].cells.length;
for(var i=0; i<colCount; i++) {
var newcell = row.insertCell(i);
newcell.innerHTML = table.rows[0].cells[i].innerHTML;
}
}else{
alert("Numarul maxim de repere este 30.");
}
}
function deleteRow(tableID) {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
for(var i=0; i<rowCount; i++) {
var row = table.rows[i];
var chkbox = row.cells[0].childNodes[0];
if(null != chkbox && true == chkbox.checked) {
if(rowCount <= 1) { // limit the user from removing all the fields
alert("Nu se pot sterge toate reperele.");
break;
}
table.deleteRow(i);
rowCount--;
i--;
}
}
}
JS getcateg
(Scriptul care trimite variabila de la BX_BRAND la getcateg.php)
Cod: Selectaţi tot
<script>
function showCateg(str)
{
if (str=="")
{
document.getElementById("showcateg").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("showcateg").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getcateg.php?brand="+str,true);
xmlhttp.send();
}
</script>
getcateg.php
(aici este dropdown-ul care ia variabila trimisa de BX_BRAND si o filtreaza cu baza de date, afisand numai categoriile fiecarui brand)
Cod: Selectaţi tot
<select required="required" class="btn_gri" name="BX_CATEG[]" id="select" title="<?php echo $row_categ['categorie']; ?>">
<option value="">Alege</option>
<?php
do {
?>
<option value="<?php echo $row_categ['categorie']?>"><?php echo $row_categ['categorie']?></option>
<?php
} while ($row_categ = mysql_fetch_assoc($categ));
$rows = mysql_num_rows($categ);
if($rows > 0) {
mysql_data_seek($categ, 0);
$row_categ = mysql_fetch_assoc($categ);
}
?>
</select>
Pentru a functiona acel JS pentru fiecare ROW din tabel fiecare dropdown incarcat din getcateg.php sa aibe un ID propriu si sa ia variabila numai de la dropdownul BX_BRAND care este pe acelasi rand cu el. Lucrul asta ma depaseste si sper din suflet ca pot sa gasesc o solutie impreuna cu voi.
Multumesc frumos de atentie,
Cristi.