hunter
hunter

Reputation: 1101

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

Upvotes: 1

Views: 24573

Answers (5)

wouter
wouter

Reputation: 346

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>

Upvotes: 1

Hacker
Hacker

Reputation: 7906

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

Upvotes: 0

Chandresh M
Chandresh M

Reputation: 3828

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.

Upvotes: 0

Caner
Caner

Reputation: 59198

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.

Upvotes: 0

Dan Grossman
Dan Grossman

Reputation: 52372

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.

Upvotes: 0

Related Questions