runnerjp Posted March 17, 2010 Share Posted March 17, 2010 I have added a function so i can add more textboxes to the form if needed <script language="javascript1.2" type="text/javascript"> function addRow() { var tbl = document.getElementById('thetable'); var lastRow = tbl.rows.length; var row = tbl.insertRow(lastRow); var cell1 = row.insertCell(0); var str1 = document.createTextNode("Distance: "); cell1.appendChild(str1); var input = document.createElement('input'); input.name = 'distance[]' + lastRow; input.id = 'distance' + lastRow; input.style.width = "187px" cell1.appendChild(input); var theBR = document.createElement('br'); cell1.appendChild(theBR); var str1 = document.createTextNode("Time: "); cell1.appendChild(str1); var input2 = document.createElement('input'); input2.value = '00'; input2.name = 'hours[]' + lastRow; input2.id = 'hours' + lastRow; input2.style.width = "25px" input2.setAttribute('maxlength', 1); cell1.appendChild(input2); var str2 = document.createTextNode(" Hours"); cell1.appendChild(str2); var input3 = document.createElement('input'); input3.value = '00'; input3.name = 'min[]' + lastRow; input3.id = 'min' + lastRow; input3.style.width = "25px" cell1.appendChild(input3); var str3 = document.createTextNode(" Minutes"); cell1.appendChild(str3); var input4 = document.createElement('input'); input4.value = '00'; input4.name = 'seconds[]' + lastRow; input4.id = 'seconds' + lastRow; input4.style.width = "25px" cell1.appendChild(input4); var str4 = document.createTextNode(" Seconds"); cell1.appendChild(str4); } function delRow() { var tbl = document.getElementById('thetable'); var lastRow = tbl.rows.length; if (lastRow > 2) tbl.deleteRow(lastRow - 1); } </script> The whole form is below <link rel = "stylesheet" href = "http://www.runningprofiles.com/dad/Form.Send/demo.css" type = "text/css"/> <script type = "text/javascript" src = "http://www.runningprofiles.com/dad/mootools.js"></script> <script type = "text/javascript" src = "http://www.runningprofiles.com/dad/Form.Send/demo.js"></script> <?php $getthreads = "Select * from dad where id='".$_GET["id"]."' ORDER BY date ASC"; $getthreads2=mysql_query($getthreads) or die("Could not get threads"); while ($getthreads3=mysql_fetch_array($getthreads2)) { ?> <center> <form id = "myForm" action = "include/addlog.php" method = "post"> <table style="BORDER-COLLAPSE: collapse"> <tbody> <tr> <td> <p align="right">Date:</p></td> <td> <p align="left"><?php echo $getthreads3['date'];?></p> <input type="hidden" name="date" value="<?php echo $getthreads3['date'];?>" > </td> </tr> <tr> <td> <br /> <p align="right">Am Run:</p></td> <td> <p align="left"><br /> <input style="WIDTH: 186px;" value="<?php echo $getthreads3['am'];?>" name="am" /></p></td> </tr> <tr> <td> <p align="right">Duration:</p></td> <td> <p align="left"> <input maxlength="2" style="WIDTH: 20px;" name="hoursam" /> Hours<input maxlength="2" style="WIDTH: 20px;" name="minam" /> Minutes<input maxlength="2" style="WIDTH: 25px; " name="secam" />Seconds</p></td> </tr> <tr> <td><br /> <p align="right">Pm Run:</p></td> <td> <p align="left"><br /> <input style="WIDTH: 186px;" value="<?php echo $getthreads3['pm'];?>" name="pm" /></p></td> </tr> <tr> <td> <p align="right">Duration:</p></td> <td> <p align="left"> <input maxlength="2" style="WIDTH: 25px; " name="hourspm" /> Hours<input maxlength="2" style="WIDTH: 25px;" name="minpm" /> Minutes<input maxlength="2" style="WIDTH: 25px;" name="secpm" /> Seconds</p></td> </tr> <tr> <td><br /> <p align="right">Weather:</p></td> <td> <p align="left"><br /> <input style="WIDTH: 187px;" name="weather" /></p></td> </tr> <tr> <td><br /> <p align="right">Comments:</p></td> <td> <p align="left"><br /> <input style="WIDTH: 187px;" name="comments" /></p></td> </tr> <tr> <td> </td> <td> <p align="center"> </p></td> </tr> </tbody> </table> <table id="thetable" align="center"> <tr> <td> </td> </tr> </table> <script language="javascript1.2" type="text/javascript"> function addRow() { var tbl = document.getElementById('thetable'); var lastRow = tbl.rows.length; var row = tbl.insertRow(lastRow); var cell1 = row.insertCell(0); var str1 = document.createTextNode("Distance: "); cell1.appendChild(str1); var input = document.createElement('input'); input.name = 'distance[]' + lastRow; input.id = 'distance' + lastRow; input.style.width = "187px" cell1.appendChild(input); var theBR = document.createElement('br'); cell1.appendChild(theBR); var str1 = document.createTextNode("Time: "); cell1.appendChild(str1); var input2 = document.createElement('input'); input2.value = '00'; input2.name = 'hours[]' + lastRow; input2.id = 'hours' + lastRow; input2.style.width = "25px" input2.setAttribute('maxlength', 1); cell1.appendChild(input2); var str2 = document.createTextNode(" Hours"); cell1.appendChild(str2); var input3 = document.createElement('input'); input3.value = '00'; input3.name = 'min[]' + lastRow; input3.id = 'min' + lastRow; input3.style.width = "25px" cell1.appendChild(input3); var str3 = document.createTextNode(" Minutes"); cell1.appendChild(str3); var input4 = document.createElement('input'); input4.value = '00'; input4.name = 'seconds[]' + lastRow; input4.id = 'seconds' + lastRow; input4.style.width = "25px" cell1.appendChild(input4); var str4 = document.createTextNode(" Seconds"); cell1.appendChild(str4); } function delRow() { var tbl = document.getElementById('thetable'); var lastRow = tbl.rows.length; if (lastRow > 2) tbl.deleteRow(lastRow - 1); } </script> <table style="WIDTH: 100%; BORDER-COLLAPSE: collapse"> <tr> <td> <p align="center"> <input type="button" value="Add Interval" onclick="addRow()" /> <input type="button" value="Subtract Interval" onclick="delRow()" /> </p> </td> </tr> </table> <center> <input type = "submit" name = "button" id = "submitter"/></center> <span class = "clr"> <!-- spanner --></span> </div> </form> <?php } ?> <center> <div id = "log"> <center><h3>Added diary</h3> <div id = "log_res"> <!-- spanner --></center> </div> </div> </center> Now when i come to try add the data to the db it only adds the 1st result from the loop...why?? <?php include '../../settings.php'; $mydate=$_POST['date']; $am=mysql_real_escape_string($_POST['am']); $minam=mysql_real_escape_string($_POST['minam']); $hoursam=mysql_real_escape_string($_POST['hoursam']); $secam=mysql_real_escape_string($_POST['secam']); $pm=mysql_real_escape_string($_POST['pm']); $minpm=mysql_real_escape_string($_POST['minpm']); $hourspm=mysql_real_escape_string($_POST['hourspm']); $secpm=mysql_real_escape_string($_POST['secpm']); $weather=mysql_real_escape_string($_POST['weather']); $comments=mysql_real_escape_string($_POST['comments']); if($am != ""){$amtime = $hoursam . ":" . $minam. ".". $secam;} if($pm != ""){$pmtime = $hourspm . ":" . $minpm. ".". $secpm;} mysql_query("INSERT INTO daddiary (`date`,`am`,`pmtime`,`pm`,`amtime`,`weather`,`comment`) VALUES ('$mydate','$am' , '$amtime','$pm','$pmtime' , '$weather','$comments') ") or die(mysql_error()); $i=-1; $limit=count($_POST['date']); while(++$i<$limit) { if ($_POST['distance'] != "") { $mydate=$_POST['date']; $distance=mysql_real_escape_string($_POST['distance'][$i]); $min=mysql_real_escape_string($_POST['min'][$i]); $hours=mysql_real_escape_string($_POST['hours'][$i]); $sec=mysql_real_escape_string($_POST['seconds'][$i]); $time = $hours . ":" . $min. ".". $sec; echo $time; mysql_query("INSERT INTO dadreps (`date`,`distance`,`time`) VALUES ('$mydate','$distance' , '$time') ") or die(mysql_error()); } } echo 'Your diary entry has been added'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/195547-script-not-adding-all-data-to-db/ Share on other sites More sharing options...
scvinodkumar Posted March 17, 2010 Share Posted March 17, 2010 You need to use array variable. for example, <input style="WIDTH: 186px;" value="<?php echo $getthreads3['am'];?>" name="am[]" /> replaced am to am[] so replace to all fields in the form as array.. and then try Quote Link to comment https://forums.phpfreaks.com/topic/195547-script-not-adding-all-data-to-db/#findComment-1027527 Share on other sites More sharing options...
runnerjp Posted March 17, 2010 Author Share Posted March 17, 2010 but im only wanting these to go through the array var cell1 = row.insertCell(0); var str1 = document.createTextNode("Distance: "); cell1.appendChild(str1); var input = document.createElement('input'); input.name = 'distance[]' + lastRow; input.id = 'distance' + lastRow; input.style.width = "187px" cell1.appendChild(input); var theBR = document.createElement('br'); cell1.appendChild(theBR); var str1 = document.createTextNode("Time: "); cell1.appendChild(str1); var input2 = document.createElement('input'); input2.value = '00'; input2.name = 'hours[]' + lastRow; input2.id = 'hours' + lastRow; input2.style.width = "25px" input2.setAttribute('maxlength', 1); cell1.appendChild(input2); var str2 = document.createTextNode(" Hours"); cell1.appendChild(str2); var input3 = document.createElement('input'); input3.value = '00'; input3.name = 'min[]' + lastRow; input3.id = 'min' + lastRow; input3.style.width = "25px" cell1.appendChild(input3); var str3 = document.createTextNode(" Minutes"); cell1.appendChild(str3); var input4 = document.createElement('input'); input4.value = '00'; input4.name = 'seconds[]' + lastRow; input4.id = 'seconds' + lastRow; input4.style.width = "25px" cell1.appendChild(input4); var str4 = document.createTextNode(" Seconds"); cell1.appendChild(str4); and as you can see they are allready in an array ver. Or do i need them ALL to be like this to have just these work? Quote Link to comment https://forums.phpfreaks.com/topic/195547-script-not-adding-all-data-to-db/#findComment-1027533 Share on other sites More sharing options...
scvinodkumar Posted March 17, 2010 Share Posted March 17, 2010 Yes you should make them all as an array Quote Link to comment https://forums.phpfreaks.com/topic/195547-script-not-adding-all-data-to-db/#findComment-1027559 Share on other sites More sharing options...
runnerjp Posted March 17, 2010 Author Share Posted March 17, 2010 I tried that and its still not looping through the data! Quote Link to comment https://forums.phpfreaks.com/topic/195547-script-not-adding-all-data-to-db/#findComment-1027585 Share on other sites More sharing options...
runnerjp Posted March 17, 2010 Author Share Posted March 17, 2010 i have even tired changing to distance as there is only 1 date but many distances! didnt work Quote Link to comment https://forums.phpfreaks.com/topic/195547-script-not-adding-all-data-to-db/#findComment-1027609 Share on other sites More sharing options...
sasa Posted March 17, 2010 Share Posted March 17, 2010 change line input.name = 'distance[]' + lastRow; to input.name = 'distance[' + lastRow + ']'; and so on Quote Link to comment https://forums.phpfreaks.com/topic/195547-script-not-adding-all-data-to-db/#findComment-1027617 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.