I have error in tis code.any help will b appreciable.tanks in advance.


if(isset($_POST))
{
for($i=0;$i<count($_POST);$i++)
{
if(!empty($_POST["hdntxtRow".$i]))
{
echo $_POST["hdntxtRow".$i]."<br />";
}
}

$question=$_POST;
$choice1=$_POST;
$choice2=$_POST;
$choice3=$_POST;
$choice4=$_POST;
$choice5=$_POST;
$answer=$_POST;


$a=mysql_query("INSERT INTO question(questions,choice1,choice2,choice3,choice4,choice5,answer)
VALUES('$question','$choice1','$choice2','$choice3','$choice4','$choice5','$answer')");
}
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD>

<script>

function addRowToTable()
{
var tbl = document.getElementById('tblSample');
var lastRow = tbl.rows.length;
// if there's no header row in the table, then iteration = lastRow + 1
var iteration = lastRow;
var row = tbl.insertRow(lastRow);

// left cell
var cellLeft = row.insertCell(0);
var textNode = document.createTextNode(iteration);
cellLeft.appendChild(textNode);

// right cell
var cellRight = row.insertCell(1);
var el = document.createElement('input');
el.type = 'text';
el.name = 'txtRow' + iteration;
el.id = 'txtRow' + iteration;
el.size = 40;

cellRight.appendChild(el);

var elh = document.createElement('input');
elh.type = 'hidden';
elh.runat ='server'; //This need to check
elh.name = 'hdntxtRow1' + iteration;
elh.id = 'hndtxtRow1' + iteration;
cellRight.appendChild(elh);


var elhi = document.createElement('input');
elhi.type = 'hidden';
elhi.runat ='server'; //This need to check
elhi.name = 'hdntxtRow2' + iteration;
elhi.id = 'hndtxtRow2' + iteration;
cellRight.appendChild(elhi);

}

</script>
<BODY>
<form name="frmaddtxtbox" method="post" action="">
<div class="extraline" style="margin-left:620px;margin-top:105px;position:absolute;">
<input type="button" value="Add" onClick="addRowToTable();" />
Add one more choice

</div>


<table width="200" border="1" style="margin-left:300px">
<tr>
<td>Enter Question</td>
<td><input type="text" name="txtquestion" /></td>
</tr>
<tr>
<td>choice 1</td>
<td><input type="text" name="txtchoice1" /></td>
</tr>
<tr>
<td>choice 2</td>
<td><input type="text" name="txtchoice2" /></td>
</tr>
<tr>
<td>choice 3</td>
<td><input type="text" name="txtchoice3" /></td>

</tr>

<tr>

</tr>


<tr>

<td>1</td>

<td><input type="text" name="txtRow1"

id="txtRow1" size="40" /></td>
<td><input type="hidden" name="hdntxtRow1"
id="txtRow2" size="40" /></td>
<td><input type="hidden" name="hdntxtRow2"
id="txtRow3" size="40" /></td>


</tr>
<tr>
<td>Ans</td>
<td><input type="text" name="txtanswer" /></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input type="submit" name="btnsave" value="Save" /></td>
</tr>
</table>
</form>

</body>
</html>

Recommended Answers

All 4 Replies

You've said you have an error but you haven't stated what the problem is. Could you also please wrap your code in code-tags.

here i m not able to generate textbox dynamically.can u correct the code for me.any help will be appreciable.

<?php
$server="localhost";
$username="root";
$password="root";
$db_name="jonas";
$db=mysql_connect($server,$username,$password);
mysql_select_db($db_name,$db);


if(isset($_POST['btnadd']))
{
for($i=0;$i<count($_POST);$i++) 
{ 
if(!empty($_POST["hdntxtRow".$i]))
{
echo $_POST["hdntxtRow".$i]."<br />";
} 
} 



$question=$_POST['txtquestion'];
$choice1=$_POST['txtchoice1'];
$choice2=$_POST['txtchoice2'];
$choice3=$_POST['txtchoice3'];
$choice4=$_POST['hdntxtrow1'];
$choice5=$_POST['hdntxtrow2'];
$answer=$_POST['txtanswer'];


$a=mysql_query("INSERT INTO question(questions,choice1,choice2,choice3,choice4,choice5,answer)
VALUES('$question','$choice1','$choice2','$choice3','$choice4','$choice5','$answer')");
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD>
<script>

function addRowToTable()
{
var tbl = document.getElementById('tblSample');
var lastRow = tbl.rows.length;
// if there's no header row in the table, then iteration = lastRow + 1
var iteration = lastRow;
var row = tbl.insertRow(lastRow);

// left cell
var cellLeft = row.insertCell(0);
var textNode = document.createTextNode(iteration);
cellLeft.appendChild(textNode);

// right cell
var cellRight = row.insertCell(1);
var el = document.createElement('input');
el.type = 'text';
el.name = 'txtRow' + iteration;
el.id = 'txtRow' + iteration;
el.size = 40;

cellRight.appendChild(el); 

var elh = document.createElement('input');
elh.type = 'hidden';
elh.runat ='server'; //This need to check
elh.name = 'hdntxtRow1' + iteration;
elh.id = 'hndtxtRow1' + iteration;
cellRight.appendChild(elh); 


var elhi = document.createElement('input');
elhi.type = 'hidden';
elhi.runat ='server'; //This need to check
elhi.name = 'hdntxtRow2' + iteration;
elhi.id = 'hndtxtRow2' + iteration;
cellRight.appendChild(elhi); 


}



</script>
<BODY>
<form name="frmaddtxtbox" method="post" action="">
<div class="extraline" style="margin-left:620px;margin-top:105px;position:absolute;">
<input type="button" name="btnadd" value="Add" onClick="addRowToTable();" />
Add one more choice       
      
</div>




<table width="200" border="1" style="margin-left:300px">
  <tr>
    <td>Enter Question</td>
    <td><input type="text" name="txtquestion" /></td>
  </tr>
  <tr>
    <td>choice 1</td>
    <td><input type="text" name="txtchoice1" /></td>
  </tr>
  <tr>
    <td>choice 2</td>
    <td><input type="text" name="txtchoice2" /></td>
  </tr>
  <tr>
    <td>choice 3</td>
    <td><input type="text" name="txtchoice3" /></td>
    
  </tr>
  
  <tr>
    
  </tr>
  

<tr>

<td>1</td>

<td><input type="text" name="txtRow1"

id="txtRow1" size="40" /></td>
<td><input type="hidden" name="hdntxtRow1"
id="txtRow2" size="40" /></td>
<td><input type="hidden" name="hdntxtRow2"
id="txtRow3" size="40" /></td>


</tr>
  <tr>
    <td>Ans</td>
    <td><input type="text" name="txtanswer" /></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><input type="submit" name="btnsave" value="Save" /></td>
  </tr>
</table>
</form>

</body>
</html>

Borzoi can u help me to correct code.the textbox is not generating.any help will appreciable.thanks in advance.

You forgot to put the 'id=tblSample'. In javascript function var tbl = document.getElementById('tblSample'); , you took the element with 'id=tblSample', but there is no 'id=tblSample' in the HTML.

You want to generate the text box in the table, right? Set the 'id=tblSample' to the table.

Here it is <table width="200" border="1" style="margin-left:300px" id="tblSample">

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.