javascript - my code won't write text to table cells -
second question today, it's not working. i've got shopping card table @ left , 1 @ right, left products can buy, right pruducts on card. code worked if clicked twice on product added 2 new rows edited code. somehow when click on won't result. (oh, , i've set code between tags, before today in onclick-function)
<script> function addtotable(productname, productprice){ var table = document.getelementbyid('cardtable'); var row = table.insertrow(-1); var rowcount = table.rows.length; alert(rowcount); for(var =0; < rowcount; i++) { var currentrow = table.rows[i]; var text = currentrow.cells[0].innertext; if(text == '$productnames[$i]') { var currentvalue = parseint(row.cells[3].innerhtml); var newvalue = currentvalue + 1; currentrow.cells[2].innertext = newvalue; }else { var cell1 = row.insertcell(0); cell1.id ='cardlefttd'; var cell2 = row.insertcell(1); cell2.id ='cardcentretd'; var cell3 = row.insertcell(2); cell3.id ='cardrighttd'; cell1.innerhtml = '$productnames[$i]'; cell2.innerhtml = '$iprice'; cell3.innerhtml = 1; } } } </script> the next code in php, , makes table info mysql database (it worked fine!)
for($i = 0; $i < sizeof($productnames); $i++) { $iprice = number_format((float)$productprices[$i], 2, ',', ''); echo ( "<tr id='producttablerow' onclick='addtotable($productnames[$i],$iprice)'> <td id=productlefttd>$productnames[$i]</td> <td id=productrighttd>$iprice</td> </tr>" ); }
so problem is, since set for-statement instead of what's in else there's nil running. know there's still error in code should run. (multiple new lines made if else case)
echo "... onclick='addtotable($productnames[$i],$iprice)' ... "
this produce invalid javascript code. have wrap strings ':
echo "... onclick=\"addtotable('$productnames[$i]',$iprice)\" ... " this valid javascript, invalid php(-usage). produce valid php: within "-string, can utilize variables - not method calls or array accesses. use:
echo "... onclick=\"addtotable('".$productnames[$i]."',$iprice)\" ... " javascript php
No comments:
Post a Comment