1

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

5 Answers 5

1

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>
Sign up to request clarification or add additional context in comments.

3 Comments

it works buddy but how can i name the textbox so that i can retrieved the posted value of these textboxes in php
as i need two textboxes one for book and second for author
use the name="" attribute. For example: <input type="text" name="t1" id="t1"> More details here: homeandlearn.co.uk/php/php4p6.html
0

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.

Comments

0

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.

Comments

0

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.

6 Comments

Thanks for reply as i mentioned in my question that i need to create two fields and then save them into database but it just create one textbox and how should i get the post value from html page in php
hi,that you can get by using $_REQUEST['field_name'].this textboxes are post as array.so you will get array of textbox on your php page.
i could't get your point newibi in php so could you please help me deeply i mean could you give me the example how can i get my problem is i have two textfields(book,author) if i click add another book two textfield created put the data in these field and then click save button the data of all these the 4 field posted to php script.but how can i get this i am having a problem with this could you please give me any example i shall be very thankful to you.
what exactly problem you are facing..? after click on save button or before that,..? please explain me.
i have facing problem from the scratch i have just two textboxes on my page i have explained my problem in last comment
|
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.