Jump to content

[resolved]appending a html element


elexion

Recommended Posts

hello everyone, I've been working on a form that creates a number of input fields based on a number entered in the form. Say someone wants to upload 6 items I want the code to generate 6 input fields for him to do so :). Obviously javascript was the perfect answer for doing this. So I crafted this little function below

 function question_input(number){for(i = 1; i < number; i++){var node=document.createElement("input");var textnode=document.createTextNode("type='text'");node.appendChild(textnode);document.getElementById("questions").appendChild(node);}}

this function works it adds the right amount of fields(please ignore the i= 1 we'll get to that ;)) Now the problem I'm having is actually getting this data from the table into the database, I know I should store it in an array but I don't know how to when I add a name=value[] attribute in the textnode it's not picked up succesfully.

//note that this is only a segment of my code that's relevant to the issue , this is the number of input fields that need to//be generated by the javascript function above<input type='text' onchange='question_input(this.value)' name='number'/>//next up I've written default input field for the number of items to be uploaded to the database//I found that the page ran more smoothly for the user with having a default input field to tab into and then see the change<td id='questions'><input type='text' name='vragen[]'/></td>

bottom line: How do I add the input from the fields generated by javascript to also add their value to the array?currently it only takes the value from the default input window into the array and thus into the databse.

Edited by elexion
  • Like 1
Link to comment
Share on other sites

Are talking about this? by the way textnode adds text, not type attribute form.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Form Page</title><script type="text/javascript">/*<![CDATA[*//*---->*/function question_input(number){for(i = 1; i < number; i++){var node=document.createElement("input");//var textnode=document.createTextNode("type='text'");//node.appendChild(textnode);node.type="text";node.name="vragen[]";document.getElementById("questions").appendChild(node);}}/*--*//*]]>*/</script></head><body><form method="post" action="response.php" name="myform"><table width="960" border="1" cellspacing="0" cellpadding="0">  <tr>    <td><input type='text' onchange='question_input(this.value)' name='number'/></td>    <td id='questions'><input type='text' name='vragen[]'/></td>  </tr></table><a href="javascript: document.myform.submit()">Submit</a></form></body></html> 

response.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>PHP Response</title></head><body><?php$postarray = array();$postarry = $_POST['vragen'];foreach($postarry as $postvalue){echo $postvalue.'<br />';}?></body></html>

  • Like 1
Link to comment
Share on other sites

Are talking about this? by the way textnode adds text, not type attribute form.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Form Page</title> <script type="text/javascript">/*<![CDATA[*//*---->*/function question_input(number){for(i = 1; i < number; i++){var node=document.createElement("input");//var textnode=document.createTextNode("type='text'");//node.appendChild(textnode);node.type="text";node.name="vragen[]";document.getElementById("questions").appendChild(node);}}/*--*//*]]>*/</script> </head><body><form method="post" action="response.php" name="myform"><table width="960" border="1" cellspacing="0" cellpadding="0">  <tr>	<td><input type='text' onchange='question_input(this.value)' name='number'/></td>	<td id='questions'><input type='text' name='vragen[]'/></td>  </tr></table><a href="javascript: document.myform.submit()">Submit</a></form></body></html>  

response.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>PHP Response</title> </head><body><?php$postarray = array();$postarry = $_POST['vragen']; foreach($postarry as $postvalue){echo $postvalue.'<br />'; }?></body></html>

Yes this is a perfect fix, thank you very very much
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...