开发者

how to create textbox dynamically in php

hay, i have two textboxes i.e book_name and author and a button that save the data to database. my question is it possible to create textbox dynamically i.e when user click on add another book button two textboxes generated and when he click on save button both of rows(data of 4 texboxes) saved into database. Is 开发者_如何学运维it possible in php...? (in phpmyadmin) we can save multiple records in a single table on clicking at save button any help would be greatly appriciated


Not with PHP, but with Javascript. PHP is a server-side language, all the code is processed before your browser gets the HTML.

Try using this code:

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

my_div.innerHTML = my_div.innerHTML +"<br><input type='text' name='mytext'+ i>"
i++;
}
</script>
</head>
<body>

<form name="form" action="post" method="">
<input type="text" name=t1>
<input type="button" value="test" onClick="changeIt()">
<div id="my_div"></div>

</body>


If you want things to happen on a webpage without a new page load occurring, you must use JavaScript, which runs in the browser.

Your button that creates new textboxes will need to be attached to a JavaScript event you write to do so.

Your PHP script will have to be updated to look for these new inputs and save them the same way it saved the first one.


It is possible, but you need to use javascript. Check this for an example.

PHP is run on the server side, you have to do this on the browser side.


you can do that with the use of JavaScript.

try below code:

function addElement() {
  var ni = document.getElementById('myDiv');
  var numi = document.getElementById('theValue');
  var num = (document.getElementById('theValue').value -1)+ 2;
  numi.value = num;
  var newdiv = document.createElement('div');
  var divIdName = 'my'+num+'Div';
  newdiv.setAttribute('id',divIdName);
  newdiv.innerHTML = 'Element Number '+num+' has been added! <a href=\'#\' onclick=\'removeElement('+divIdName+')\'>Remove the div "'+divIdName+'"</a>';
  ni.appendChild(newdiv);
}

Thanks.


u need to add new textboxes using javascript and build ur sql based on the number of text boxes being added.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜