ss0901 Posted April 2, 2011 Share Posted April 2, 2011 HI,i am using java script to create a add row function in the php .but when the first row data can insert into database ,the 2nd row data cannot insert into database ,can help me to check my coding? thx a lot <SCRIPT language="javascript"> function addRow(tableID) { var table = document.getElementById(tableID); var rowCount = table.rows.length; var row = table.insertRow(rowCount); var cell1 = row.insertCell(0); var element1 = document.createElement("input"); element1.type = "checkbox"; cell1.appendChild(element1); var cell2 = row.insertCell(1); var element2 = document.createElement("input"); element2.type = "text"; cell2.appendChild(element2); var cell3 = row.insertCell(2); var element3 = document.createElement("input"); element3.type = "text"; cell3.appendChild(element3); var cell4 = row.insertCell(3); var element4 = document.createElement("input"); element4.type = "text"; cell4.appendChild(element4); var cell5 = row.insertCell(4); var element5 = document.createElement("input"); element5.type = "text"; cell5.appendChild(element5); } function deleteRow(tableID) { try { 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) { table.deleteRow(i); rowCount--; i--; } } } catch(e) { alert(e); } } </SCRIPT> THE PHP CODE <?php require_once ('../../../Connections/admin_db.php'); mysql_select_db("admin_db"); if ((isset($_POST["Submit"])) && ($_POST["Submit"] == "Submit")) { $i=0; foreach($_POST['abc'] as $value ) { $abc = $_POST['abc'][$i]; $level = $_POST['level'][$i]; $level_desc = $_POST['level_desc'][$i]; $pc_desc = $_POST['pc_desc'][$i]; //Insert Data into Instructor Profile Info $q = "INSERT INTO plo_pc(p_name,plo_id,plo_criteria,plo_level,level_dec,plo_desc) VALUES ('$list','$plo_id','$abc','$level','$level_desc','$pc_desc') " ; mysql_query($q) or die(mysql_error()) ; $i=$i+1; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/232478-insert-data-with-add-row-function-in-php/ Share on other sites More sharing options...
sunfighter Posted April 2, 2011 Share Posted April 2, 2011 PHP is a server side language used to make the HTML page a user sees. Javascript is a user side language used to change the HTML page. They normally do not talk to each other. It sounds as if your collecting data and inserting it into a DB. Use a form for that. Your 'add a table row' works and can be adapted for the form. Quote Link to comment https://forums.phpfreaks.com/topic/232478-insert-data-with-add-row-function-in-php/#findComment-1195957 Share on other sites More sharing options...
sasa Posted April 3, 2011 Share Posted April 3, 2011 in your script you didn't define names of input fields Quote Link to comment https://forums.phpfreaks.com/topic/232478-insert-data-with-add-row-function-in-php/#findComment-1196138 Share on other sites More sharing options...
ss0901 Posted April 4, 2011 Author Share Posted April 4, 2011 hi,sasa can help me to check what is wrong with my coding ~ thx <SCRIPT language="javascript"> function addRow(tableID) { var table = document.getElementById(tableID); var rowCount = table.rows.length; var row = table.insertRow(rowCount); var cell1 = row.insertCell(0); var element1 = document.createElement("input"); element1.type = "checkbox"; cell1.appendChild(element1); var cell2 = row.insertCell(1); var element2 = document.createElement("input"); element2.type = "text"; cell2.appendChild(element2); var cell3 = row.insertCell(2); var element3 = document.createElement("input"); element3.type = "text"; cell3.appendChild(element3); var cell4 = row.insertCell(3); var element4 = document.createElement("input"); element4.type = "text"; cell4.appendChild(element4); var cell5 = row.insertCell(4); var element5 = document.createElement("input"); element5.type = "text"; cell5.appendChild(element5); } function deleteRow(tableID) { try { 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) { table.deleteRow(i); rowCount--; i--; } } } catch(e) { alert(e); } } </SCRIPT> //<form action="" method="post" enctype="multipart/form-data" id="form1"> <div id="apDiv13"> <table width="334" height="72"> <tr> <td width="111">Program :</td> <td width="163"><select name="list" id="list"> <option value="0">---SELECT----</option> <?php do { ?> <option value="<?php echo $row_Recordset1['p_id']?>"><?php echo $row_Recordset1['p_name']?></option> <?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); $rows = mysql_num_rows($Recordset1); if($rows > 0) { mysql_data_seek($Recordset1, 0); $row_Recordset1 = mysql_fetch_assoc($Recordset1); } ?> </select></td> </tr> <tr> <td>PLO ID :</td> <td><input name="plo_id" type="text" id="plo_id" value="PLO" size="16" /></td> </tr> </table> </div> <p> </p> <p> </p> <table width="560" height="88" border="1" id="dataTable"> <tr style="background-color: #60F; text-align: left"> <td width="20"> </td> <td width="144" id="dataTable"> Criteria</td> <td width="144" id="dataTable">Level</td> <td width="144" id="dataTable">Level Description</td> <td width="144" id="dataTable">Statement </td> </tr> <tr> <td height="55"><input type="checkbox" name="chk[]"/></td> <td id="dataTable"><input type="text" name="abc[]" id="cr" /></td> <td id="dataTable"><input name="level[]" type="text" id="level" /></td> <td id="dataTable"><input name="level_desc[]" type="text" id="level_desc" value="" /></td> <td id="dataTable"><input name="pc_desc[]" type="text" id="pc_desc" value="" size="25" /></tr> </table> <p> <input type="button" value="Add Row" onClick="addRow('dataTable')" /> <input type="button" value="Delete Row" onClick="deleteRow('dataTable')" /> <label> <input type="submit" name="Submit" id="Submit" value="Submit" onClick="Submit('dataTable')" /> </label> </p> <p> </p> <p> </p> <p> </p> //</form> <?php require_once ('../../../Connections/admin_db.php'); mysql_select_db("admin_db"); if ((isset($_POST["Submit"])) && ($_POST["Submit"] == "Submit")) { $i=0; foreach($_POST['abc'] as $value ) { $list =$_POST['list']; $plo_id =$_POST['plo_id']; $abc = $_POST['abc'][$i]; $level = $_POST['level'][$i]; $level_desc = $_POST['level_desc'][$i]; $pc_desc = $_POST['pc_desc'][$i]; //Insert Data into Instructor Profile Info $q = "INSERT INTO plo_pc(p_name,plo_id,plo_criteria,plo_level,level_dec,plo_desc) VALUES ('$list','$plo_id','$abc','$level','$level_desc','$pc_desc') " ; mysql_query($q) or die(mysql_error()) ; $i=$i+1; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/232478-insert-data-with-add-row-function-in-php/#findComment-1196463 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.