hi..

im dynamicelly creating text box in a html file using javascript..

<script>
function create_input_boxes()
{ 
if(document.getElementById("name0"))
{
	return true;
} 
var boxes=""; 
var num_boxes=document.getElementById("num_boxes").value; 
if(num_boxes) 
{  
	for(var i=0;i<num_boxes;i++)  
	{   
		boxes+="<input id='name"+i+"' name='name"+i+"' value=''><br />";   
	}  
	document.getElementById("textbox_container").innerHTML=boxes;  
} 
return false; 
}
</script>
</head>

<body>
<form id="theForm" action="dynamic.php" method="post" onsubmit="return create_input_boxes();">
<div id="textbox_container"></div>
Enter number input boxes: <input name="num_boxes"><br />
<div id="submit_button"><input type="submit" name="submit" value="Submit"></div>
</form>
</body>

while im trying to retrieve the values in php..

i get parse error in line 17. in my php file..

<html>
<head>

</head>
<body>
<?php

if(isset($_POST["submit"]))
{ 	
	for($i=0;$i<count($_POST);$i++) 
    {  
    	$name[]=!empty($_POST["name".$i])?$_POST["name".$i]:""; 
        // All dynamically created inputs are being stored again in $name array  
    } 
    for($i=0;$i<count($name);$i++) 
    (  
    	echo $name[$i];  
    } 
}
?>
</body>
</html>

plz help me in correcting the code.. Thank u

Dani AI

Generated

Good catch by — the parse error came from using the wrong block delimiter in the second PHP loop, and the page needed an id on the num_boxes input so the script can read its value. Beyond those fixes, a few practical points and safer patterns complete the solution.

Returning false from an onsubmit handler stops the form from submitting; creating inputs inside the submit handler then returning false will prevent any data from reaching the server. Either create the fields before submit (for example when num_boxes changes), or let the handler return true (or call form.submit() after creating inputs) so the browser posts the form. Also, naming inputs as an array (name="name[]") simplifies server code: the server receives $_POST['name'] as an array and can loop with foreach instead of trying to build names numerically.

Server-side handling should not rely on count($_POST) to determine how many dynamic fields exist (that count includes other form fields). Use the posted array ($_POST['name']) or a dedicated num_boxes value to control loops. Always validate and sanitize posted values before output; functions like htmlspecialchars prevent XSS when echoing back user input. See the PHP docs on the $_POST superglobal and htmlspecialchars for details and examples: PHP $_POST superglobal htmlspecialchars.

Quick debug checklist: check the exact PHP parse error message and line, view the generated HTML (View Source) to confirm input names/ids, use var_dump($_POST) to inspect submitted data, and enable error display in a dev environment. Also prefer the full PHP opening tag <?php to avoid short-tag issues on some servers.

Recommended Answers

All 4 Replies

What parse error?

It will usually give you something to go by, e.g. 'expecting x' or 'unexpected x'

hi..

im dynamicelly creating text box in a html file using javascript..

<script>
function create_input_boxes()
{ 
if(document.getElementById("name0"))
{
	return true;
} 
var boxes=""; 
var num_boxes=document.getElementById("num_boxes").value; 
if(num_boxes) 
{  
	for(var i=0;i<num_boxes;i++)  
	{   
		boxes+="<input id='name"+i+"' name='name"+i+"' value=''><br />";   
	}  
	document.getElementById("textbox_container").innerHTML=boxes;  
} 
return false; 
}
</script>
</head>

<body>
<form id="theForm" action="dynamic.php" method="post" onsubmit="return create_input_boxes();">
<div id="textbox_container"></div>
Enter number input boxes: <input name="num_boxes"><br />
<div id="submit_button"><input type="submit" name="submit" value="Submit"></div>
</form>
</body>

while im trying to retrieve the values in php..

i get parse error in line 17. in my php file..

<html>
<head>

</head>
<body>
<?php

if(isset($_POST["submit"]))
{ 	
	for($i=0;$i<count($_POST);$i++) 
    {  
    	$name[]=!empty($_POST["name".$i])?$_POST["name".$i]:""; 
        // All dynamically created inputs are being stored again in $name array  
    } 
    for($i=0;$i<count($name);$i++) 
    (  
    	echo $name[$i];  
    } 
}
?>
</body>
</html>

plz help me in correcting the code.. Thank u

Check line 16 in your code '(' has to be "{ "

Apart from that you need to provide the Id to 'num_boxes' like

<?
if(isset($_POST["submit"]))
{ 	
	for($i=0;$i<count($_POST);$i++) 
    {  
    	$name[]=!empty($_POST["name".$i])?$_POST["name".$i]:""; 
        // All dynamically created inputs are being stored again in $name array  
    } 
    for($i=0;$i<count($name);$i++) 
    { 
    	echo $name[$i];  
    } 
}
?>
<html>
<head>
<script>
function create_input_boxes()
{ 
if(document.getElementById("name0"))
{
	return true;
} 
var boxes=""; 
var num_boxes=document.getElementById("num_boxes").value; 
if(num_boxes) 
{  
	for(var i=0;i<num_boxes;i++)  
	{   
		boxes+="<input id='name"+i+"' name='name"+i+"' value=''><br />";   
	}  
	document.getElementById("textbox_container").innerHTML=boxes;  
} 
return false; 
}
</script>
</head>

<body>
<form id="theForm" action="" method="post" onsubmit="return create_input_boxes();">
<div id="textbox_container"></div>
Enter number input boxes: <input type="text" id="num_boxes" name="num_boxes"><br />
<div id="submit_button"><input type="submit" name="submit" value="Submit"></div>
</form>
</body>
</html>

hi..

thank you Network18.. i got it now..

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.