Tuesday, 15 February 2011

php - Generate text box -



php - Generate text box -

how generate n number of input text box in javascript using select option. , how fetch variables in php.

below html form

<html> <head> <title>dynamically add together textbox in html form using javascript</title> <script language="javascript"> var numberofelementsadded; var arrayofinputs = new array(); function addtodatabase(numberofelementsadded) { numberofelementsadded = numberofelementsadded * numberofelementsadded; var alltext = ""; (var j = 1; j <= numberofelementsadded; j++) { var tvalue = document.getelementsbyname("text" + j)[0].value; arrayofinputs.push(tvalue); alltext = alltext + tvalue + " "; } alert(alltext); } function add(type, num) { numberofelementsadded = num; //create input type dynamically. var k = 0; (var j = 0; j < num; j++) { (var = 0; < 4; i++) { var element = document.createelement("input"); k = k + 1; //assign different attributes element. element.setattribute("type", type + k); element.setattribute("id", type + k); element.setattribute("name", type + k); element.setattribute('width', '30'); element.setattribute('border', '3px solid #ff0000'); var foo = document.getelementbyid("foobar"); //append element in page (in span). foo.appendchild(element); } var breakloop = document.createelement('br') foo.appendchild(breakloop) } var element = document.createelement("input"); element.setattribute("type", "button"); element.setattribute("id", "submit"); element.setattribute("name", "insert database"); element.setattribute("value", "insert database"); element.setattribute("onclick", "addtodatabase(numberofelementsadded)"); var breakloop = document.createelement('br') foo.appendchild(breakloop) foo.appendchild(element); } </script> </head> <body> <form> <h2>dynamically add together element in form.</h2> <select name="element"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> <option value="7">7</option> <option value="8">8</option> <option value="9">9</option> <option value="10">10</option> </select> <input type="button" value="add" onclick="add('text',document.forms[0].element.value)"/> <br/> <span id="foobar">&nbsp;</span> </form> </body> </html>

it generates text box. want fetch values of text box php variable.

assuming code generates textboxes properly, values in php, have amend this:

element.setattribute("name", "insert database");

to

element.setattribute("name", "myfieldname[]"); // [] brackets

then in php, way:

$fields = $_post['myfieldname']; // create single delimited field $fields = implode('|', $fields);

php javascript

No comments:

Post a Comment