zipp Posted April 30, 2008 Share Posted April 30, 2008 I have a form that uses javascript to update the number of fields that they want. What I need is to have some sort of way for all the fields to be inserted into the database under their perspective field id's. The problem I am running into is that only the last field is read, and inserted into the database. Is there a way to insert multiple lines into the database maybe? How would do I read multiple lines and set them as variables? I'm so lost, any help would be much appreciated. Here is the form code: <html> <head> <title>test</title> <script language="javascript" src="scripts.js"> function makefields(num_clas) { document.getElementById('fields').innerHTML="" var b = num_clas for (a=1; a<=b; a++) { document.getElementById('fields').innerHTML+="<tr><td><input type='text' name='field1' id='field1' width='100' align='left' maxlength='25' /></td><td><input type='text' name='field2' id='field2' width='100' align='left' maxlength='20' /></td> <td><input type='text' name='field3' id='field3' width='100' align='left' maxlength='10' /></td> <td><select name='field4' id='field4'> <option value='1'>1</option> <option value='2'>2</option> <option value='3'>3</option> <option value='4'>5</option> <option value='5'>5</option> </select></td> <td><input type='text' name='field5' id='field5' width='100' align='left' maxlength='10' /></td> <td><input type='text' name='field6' id='field6' width='100' align='left' maxlength='15' /></td></tr>" } } </script> </head> <body> <table width="750" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td width="25%"> </td> <td width="50%" align="center"> <table width="350" border="0" align="center" cellpadding="0" cellspacing="0"> <tr height="30"> <td width="50"> </td> <td width="250"> </td> <td width="50"> </td> </tr> <tr align="center" height="50"> <td> </td> <td> <table width='800' border='0'> <tr> <td><strong>header:</strong></td> <td colspan='5'>Number of fields: <input type='text' name='num_clas' id='num_clas' size='2' maxlength='2' onKeyUp='makefields(this.value)' /></td> </tr> <tr> <td>field 1:</td> <td>field 2:</td> <td>field 3:</td> <td>field 4:</td> <td>field 5:</td> <td>field 6:</td> </tr> </table> <table width='800' border='0' id='fields' name='fields'> </table> <form id='info' name='info' method='post' action='create.php'> <div align='right'> <input type='submit' name='create_btn' id='create_btn' value='Create' /> <input type='reset' name='reset_btn' id='reset_btn' value='Reset' /> </div> </form> </td> <td></td> </tr> <tr height="30"> <td> </td> <td> </td> <td> </td> </tr> </table><br /></td> <td width="25%"> </td> </tr> </table> </body> </html> </body> </html> Here is create.php: <?php include("dbcon.php"); $field1 = htmlentities($_REQUEST["field1"]); if (empty($field1)) { echo "<script>alert('No value was entered for field1, please enter a value.');</script>"; } $field2 = htmlentities($_REQUEST["field2"]); if (empty($field2)) { echo "<script>alert('No value was entered for field2, please enter a value.');</script>"; } $field3 = htmlentities($_REQUEST["field3"]); if (empty($field3)) { echo "<script>alert('No value was entered for field3, please enter a value.');</script>"; } $field4 = htmlentities($_REQUEST["field4"]); if (empty($field4)) { echo "<script>alert('No value was entered for field4, please enter a value.');</script>"; } $field5 = htmlentities($_REQUEST["field5"]); if (empty($credith)) { echo "<script>alert('No value was entered for field5, please enter a value.');</script>"; } $field6 = htmlentities($_REQUEST["field6"]); if (empty($field6)) { echo "<script>alert('No value was entered for field6, please enter a value.');</script>"; } $info = mysql_query("INSERT INTO info (field1, field2, field3, field4, field5, field6) VALUES ('".$field1."', '".$field2."', '".$field3."', '".$field4."', '".$field5."', '".$field6."')"); echo "<script>window.location='index.php'</script>"; } //close connection mysql_close($con); ?> Link to comment https://forums.phpfreaks.com/topic/103644-php-and-sql-with-javascript-too/ Share on other sites More sharing options...
zipp Posted April 30, 2008 Author Share Posted April 30, 2008 Sorry, there was an error in the html code. Here is the new version... (is there a way to edit posts on here?) <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>test</title> <script language="javascript" src="scripts.js"> function makefields(num_clas) { document.getElementById('fields').innerHTML="" var b = num_clas for (a=1; a<=b; a++) { document.getElementById('fields').innerHTML+="<tr><td><input type='text' name='field1' id='field1' width='100' align='left' maxlength='25' /></td><td><input type='text' name='field2' id='field2' width='100' align='left' maxlength='20' /></td> <td><input type='text' name='field3' id='field3' width='100' align='left' maxlength='10' /></td> <td><select name='field4' id='field4'> <option value='1'>1</option> <option value='2'>2</option> <option value='3'>3</option> <option value='4'>5</option> <option value='5'>5</option> </select></td> <td><input type='text' name='field5' id='field5' width='100' align='left' maxlength='10' /></td> <td><input type='text' name='field6' id='field6' width='100' align='left' maxlength='15' /></td></tr>" } } </script> </head> <body> <form id='newuser' name='newuser' method='post' action='create.php'> <table width="750" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td width="25%"> </td> <td width="50%" align="center"> <table width="350" border="0" align="center" cellpadding="0" cellspacing="0"> <tr height="30"> <td width="50"> </td> <td width="250"> </td> <td width="50"> </td> </tr> <tr align="center" height="50"> <td> </td> <td> <table width='800' border='0'> <tr> <td><strong>header:</strong></td> <td colspan='5'>Number of fields: <input type='text' name='num_clas' id='num_clas' size='2' maxlength='2' onKeyUp='makefields(this.value)' /></td> </tr> <tr> <td>field 1:</td> <td>field 2:</td> <td>field 3:</td> <td>field 4:</td> <td>field 5:</td> <td>field 6:</td> </tr> </table> <table width='800' border='0' id='fields' name='fields'> </table> <div align='right'> <input type='submit' name='create_btn' id='create_btn' value='Create' /> <input type='reset' name='reset_btn' id='reset_btn' value='Reset' /> </div> </form> </td> <td></td> </tr> <tr height="30"> <td> </td> <td> </td> <td> </td> </tr> </table><br /></td> <td width="25%"> </td> </tr> </table> </body> </html> </body> </html> Link to comment https://forums.phpfreaks.com/topic/103644-php-and-sql-with-javascript-too/#findComment-530724 Share on other sites More sharing options...
zipp Posted May 1, 2008 Author Share Posted May 1, 2008 I'll pay $10(USD, through paypal) to whoever helps me out on this (whoever gives me the info i need to be able to figure this out)... its important. Link to comment https://forums.phpfreaks.com/topic/103644-php-and-sql-with-javascript-too/#findComment-530800 Share on other sites More sharing options...
haku Posted May 1, 2008 Share Posted May 1, 2008 Your problem lies in the innerHTML you are using to create the table rows. You have a loop, but it outputs the names field1, field2, field3, field4 and field5 each time it goes through the loop. That means that for each table row it outputs, it just re-creates these fields. So if the person calls the function makefields with a num_clas value of 3, you will have 3 rows in your table, each one with a field1, each one with a field2, each one with a field3 etc. When the value is posted to your php script, it goes through each of the field1s, but it only 'remembers' the value of the last one. The values of the first two field1s are lost. What you need to do is give each field name a unique value. Or you can set up the input names as arrays and loop through the array name in your php script. Link to comment https://forums.phpfreaks.com/topic/103644-php-and-sql-with-javascript-too/#findComment-530837 Share on other sites More sharing options...
zipp Posted May 1, 2008 Author Share Posted May 1, 2008 Would you be able to help me script that into my code? I understand what your saying, and the reasoning behind it, but javascript is my week point. I'll run over to w3schools and see what i can code up though. Link to comment https://forums.phpfreaks.com/topic/103644-php-and-sql-with-javascript-too/#findComment-530848 Share on other sites More sharing options...
zipp Posted May 1, 2008 Author Share Posted May 1, 2008 Alright, I coded it up, now I need to fix the PHP end I believe. We shall see what happens next. Link to comment https://forums.phpfreaks.com/topic/103644-php-and-sql-with-javascript-too/#findComment-530855 Share on other sites More sharing options...
haku Posted May 1, 2008 Share Posted May 1, 2008 And my 10 dollars? Link to comment https://forums.phpfreaks.com/topic/103644-php-and-sql-with-javascript-too/#findComment-530860 Share on other sites More sharing options...
zipp Posted May 1, 2008 Author Share Posted May 1, 2008 It's not solved yet, but if no one else helps me, then i guess its yours. Link to comment https://forums.phpfreaks.com/topic/103644-php-and-sql-with-javascript-too/#findComment-530865 Share on other sites More sharing options...
haku Posted May 1, 2008 Share Posted May 1, 2008 It's all good, I don't expect any money. I would have put up the same answer whether you had the money offer there or not. Link to comment https://forums.phpfreaks.com/topic/103644-php-and-sql-with-javascript-too/#findComment-530934 Share on other sites More sharing options...
zipp Posted May 1, 2008 Author Share Posted May 1, 2008 table format: id | f1 | f2 | f3 01 | a | a | 1 01 | b | b | 2 02 | a | a | 1 02 | b | b | 2 OK. I have a new problem. I'll try to explain it out here. I am trying to average all of the "f3 fields, where id = 01" Kinda stuck on how to go about this, anyone have any ideas? Link to comment https://forums.phpfreaks.com/topic/103644-php-and-sql-with-javascript-too/#findComment-531375 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.