I have here my Javascript code that adds dynamic textbox (row) my problem is how can I save the values from the dynamic textbox to database using PHP script? Hope you can help me guys.. Thanks!

<script type="text/JavaScript"> 
  function addRow(r){ 
  var root = r.parentNode;//the root 
  var allRows = root.getElementsByTagName('tr');//the rows' collection 
  var cRow = allRows[0].cloneNode(true)//the clone of the 1st row 
  var cInp = cRow.getElementsByTagName('input');//the inputs' collection of the 1st row 
  for(var i=0;i<cInp.length;i++){//changes the inputs' names (indexes the names) 
  cInp[i].setAttribute('name',cInp[i].getAttribute('name')+'_'+(allRows.length+1)) 
  } 
  root.appendChild(cRow);
  } 
  function shownames(){ 
  var allInp=document.getElementsByTagName('input'); 
  for(var i=0;i<allInp.length;i++){ 
  alert(allInp[i].name) 
  } 
  } 
  </script> 

My HTML code:

  <form method="POST" action="#"> <table width="1024" border="0" cellspacing="6" cellpadding="0"> <tr>
      <td width="195"><div class="user"><input type="text" name="user_a" id="user"  tabindex="6"/></div></td> 
    <td width="410"><div class="reported"><input type="text" name="user_b" id="reported" tabindex="7"/></div></td> 
    <td width="399"><div class="reported"><input type="text" name="user_c" id="reported" tabindex="8"/></div></td> 
    <td width="10"><input name="button" type="button" value="+" onclick="addRow(this.parentNode.parentNode)"></td> </tr> </table> </form>
Member Avatar for LastMitch

@miyotz

I have here my Javascript code that adds dynamic textbox (row) my problem is how can I save the values from the dynamic textbox to database using PHP script? Hope you can help me guys.. Thanks!

I don't see any PHP or MYSQL code in your script?

I hope you don't expect someone to write the PHP or MYSQL code for you?

I can tell you no one will write it!

If you don't understand PHP or MYSQL then you should learn it!

Member Avatar for diafol

You're adding dynamic textboxes - so I suggest that you use array in name attribute, e.g.

<td width="195"><div class="user"><input type="text" name="user_a[]" id="user"  tabindex="6"/></div></td> 
    <td width="410"><div class="reported"><input type="text" name="user_b[]" id="reported" tabindex="7"/></div></td> 
    <td width="399"><div class="reported"><input type="text" name="user_c[]" id="reported" tabindex="8"/></div></td>

Your ids are wrong and if you need to use them, they will have to be made unique. You can't have identical ids in a page. Also, I wonder about the tabindexes. These will also be duplicated? If so, could be a prob - you tab from user_a to user_a - not user_a to user_b

you should probalbly not be cloning the row though you should build a clean row and append it
document.createNewElement("tr")...

using allRows length input.tabIndex=allRows.length

Member Avatar for diafol

I'd wrap the add row into a function:

var rowno = 1; var tabindex = 8;
function addRow(){
    //increment rowno every time this runs - so you can make unique ids if req'd
    //concatenate the tablerow cells while -
    //increment tabindex for each textbox added
    //return the tablerow
}

How to remove that generated texboxes?
I have tried all possiblw ways.
Help me out please.
Thaks

Why not use a library like jquery or prototype.. You could accomplish the samething with few and robust inline code.

Member Avatar for diafol

Show your code first.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.