posting from onClick generated fields

Reply

Join Date: Feb 2008
Posts: 44
Reputation: emhmk1 is an unknown quantity at this point 
Solved Threads: 0
emhmk1 emhmk1 is offline Offline
Light Poster

posting from onClick generated fields

 
0
  #1
Oct 7th, 2009
Hi i have another query i would like to run by you.

I have a javascript code which i would like to implement in my site but i have come across an error.

The JS generates a new input text field once a button is clicked but i'm uncertain of how to pull the data from the php side. there are 4 fields to start with and the button 'adds another field'. how do i get php to use $_POST on the generated fields when i don't know how many there are going to be? the code is below

  1. </head>
  2.  
  3. <script language="javascript" type="text/javascript">
  4.  
  5. function addField() {
  6. var tbody = document.getElementById("tblBody");
  7. var ctr = tbody.getElementsByTagName("input").length + 1;
  8. var input;
  9.  
  10. if ( ctr > 15 ) {
  11. alert ("If you want to tell the whole world, dont do it all at once please");
  12. }else{
  13.  
  14. if (document.all){ //input.name doesn't work in IE
  15. input = document.createElement('<input name="field_'+ctr+'"> x <input name="field_'+ctr+'">');
  16. }else{
  17. input = document.createElement('input');
  18. input.name = "marquee_"+ctr;
  19. }
  20.  
  21. input.id = input.name;
  22. input.type = "text";
  23. input.value = "";
  24. input.className = "textfield";
  25. var cell = document.createElement('td');
  26. cell.style.height = '30px';
  27. cell.appendChild(document.createTextNode(ctr+". "));
  28. cell.appendChild(input);
  29. var row = document.createElement('tr');
  30. row.appendChild(cell);
  31. tbody.appendChild(row);
  32.  
  33. window.document.the_form.count.value = ctr;
  34.  
  35. }
  36. }
  37. </script>
  38.  
  39.  
  40.  
  41. <body>
  42.  
  43.  
  44.  
  45.  
  46. <form name="the_form" id="the_form" method="post" action="">
  47.  
  48. <table width="100%" border="0" cellspacing="0" cellpadding="0">
  49. <tbody id="tblBody">
  50. <tr>
  51. <td height="30">
  52. 1. <input name="marquee_1" type="text" class="textfield" id="field_1" />
  53. </td>
  54. </tr>
  55. <tr>
  56. <td height="30">
  57. 2. <input name="marquee_2" type="text" class="textfield" id="field_2" />
  58. </td>
  59. </tr>
  60. <tr>
  61. <td height="30">
  62. 3. <input name="marquee_3" type="text" class="textfield" id="field_3" />
  63. </td>
  64. </tr>
  65.  
  66.  
  67. <tbody
  68. </table>
  69. <input name="count" type="hidden" id="count" value="4"/>
  70. <input name="add" type="button" class="button" id="add" value="Add Another" onClick="addField();"/>
  71. <input type="submit" name="submit" value="Get Components" />
  72.  
  73. </form>

The limit goes to 15 fields so should i just create the 15 $_POST['']; or is there a better way of achieving this??!!??
Thanks Guys
Chris
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,746
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 331
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster
 
-1
  #2
Oct 7th, 2009
You are almost there! I have modified your code, very small changes.
  1. <?php
  2. if(isset($_POST['submit'])) {
  3. for($i=1;$i<=$_POST['count'];$i++) {
  4. $textfieldname = "marquee_".$i;
  5. print $_POST[$textfieldname];
  6. print "<br />";
  7. }
  8. }
  9. ?>
  10. <html>
  11. <head>
  12.  
  13. <script language="javascript" type="text/javascript">
  14.  
  15. function addField() {
  16. var tbody = document.getElementById("tblBody");
  17. var ctr = tbody.getElementsByTagName("input").length + 1;
  18. var input;
  19.  
  20. if ( ctr > 15 ) {
  21. alert ("If you want to tell the whole world, dont do it all at once please");
  22. }else{
  23.  
  24. if (document.all){ //input.name doesn't work in IE
  25. input = document.createElement('<input name="field_'+ctr+'"> x <input name="field_'+ctr+'">');
  26. }else{
  27. input = document.createElement('input');
  28. input.name = "marquee_"+ctr;
  29. }
  30.  
  31. input.id = input.name;
  32. input.type = "text";
  33. input.value = "";
  34. input.className = "textfield";
  35. var cell = document.createElement('td');
  36. cell.style.height = '30px';
  37. cell.appendChild(document.createTextNode(ctr+". "));
  38. cell.appendChild(input);
  39. var row = document.createElement('tr');
  40. row.appendChild(cell);
  41. tbody.appendChild(row);
  42. document.getElementById('count').value = ctr;
  43. }
  44. }
  45. </script>
  46. </head>
  47.  
  48.  
  49. <body>
  50.  
  51.  
  52.  
  53.  
  54. <form name="the_form" id="the_form" method="post" action="">
  55. <input type="hidden" name="count" id="count">
  56. <table width="100%" border="0" cellspacing="0" cellpadding="0">
  57. <tbody id="tblBody">
  58. <tr>
  59. <td height="30">
  60. 1. <input name="marquee_1" type="text" class="textfield" id="field_1" />
  61. </td>
  62. </tr>
  63. <tr>
  64. <td height="30">
  65. 2. <input name="marquee_2" type="text" class="textfield" id="field_2" />
  66. </td>
  67. </tr>
  68. <tr>
  69. <td height="30">
  70. 3. <input name="marquee_3" type="text" class="textfield" id="field_3" />
  71. </td>
  72. </tr>
  73.  
  74.  
  75. </tbody>
  76. </table>
  77. <input name="add" type="button" class="button" id="add" value="Add Another" onClick="addField();"/>
  78. <input type="submit" name="submit" value="Get Components" />
  79.  
  80. </form>
  81. </body>
  82. </html>
(P.S. If count value is empty, then it means, 'Add another' button wasn't clicked and no fields were added).
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 44
Reputation: emhmk1 is an unknown quantity at this point 
Solved Threads: 0
emhmk1 emhmk1 is offline Offline
Light Poster
 
0
  #3
Oct 7th, 2009
Thanks for the quick reply, the code has just one flaw!

When i hit submit after adding a couple of fields it echo's out nothing, the spaces are there just no text from the form.

Please could someone take a quick gander as i'm pretty stuck!!

Cheers
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC