3

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>

2 Answers 2

1

You have to use just name of the text box which is added by dynamically.

 $('form').submit(function() {
 var data=($(this).serialize());
 return false;
 });

This function get all the elements value and create one string which is store in data, now data will pass in ajax call.

$('form').submit(function() {
 var data=($(this).serialize());
      $.ajax({
      type: "POST",
      url: "your_some.php",
      data: data,
      }).done(function( msg ) {
      alert( "Data Saved: " + msg );
     });
}); 
Sign up to request clarification or add additional context in comments.

Comments

0
<html>
<head>
<title>Dynamic Form</title>
<script language="javascript">
<?php $i = 1; ?>
function changeIt()
{
    //alert(i);
    //var i = 1;

my_div.innerHTML = my_div.innerHTML +"<br><input type='text' name='mytext[<?php echo $i;?>]'><input type='text' name='mytext[<?php echo $i+1;?>]'><input type='text' name='mytext[<?php echo $i+2;?>]'><br>";
<?php $i = $i+3; ?>
}
</script>
</head>
<body>

<form name="form" action="http://localhost/try.php" method="post">
<!--<input type="text" name=t1>-->
<input type="button" value="test" onClick="changeIt()">
<div id="my_div"></div>
<p class="submit"><button type="submit">Register</button></p>       
</body>

try.php(this is the file where you will be catching values and then inserting into sql )

<?php
    $var = $_POST['mytext'];
    echo $var[1].$var[2];
     ?>

8 Comments

Thanks for the reply. In your code I'm able to add only one textbox. But I need to add three textbox in one row. How will I do that? please do help me. Thanks!
shouldn't i be a global variable (used in function changeIt())
yes my bad, it needs to be global. @user1831375, i have edited the code, try using edited one it will add 3 text boxes in one go!
thank you Anshul Gupta for the wonderful codes it works very well!
Another question again.. I dont know how to call its value to save in mysql using PHP. I should get the three values from the three dynamic textbox. I got my codes here it seems to be not working. Im sorry Im new to PHP. Here is my PHP code: '<?php $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("my_db", $con); $name=$_GET['myname']; $N = count($myname); for($i=0; $i < $N; $i++) { mysql_query("INSERT INTO tbl1 (user, rp, at) VALUES ('$myname[$i]', '$myname[$i]', '$myname[$i]')"); } mysql_close($con); ?> '
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.