hi there guys! how can i add textbox using button? i know that it can be done using javascript but i was required to use php. Is it possible to do it in php? thanks

8 Years
Discussion Span
Last Post by melodangdang

for this to work in php you would need to get the page to reload/refresh.

personally, if i ever wanted to do something like this i'd use Javascript. I'd more than likely use the Jquery Javascript framework to do it.


As I see it you have 3 options, in my opinion these are order worst to best solution.

1. On clicking the button transfer to the same/differnt page, which handles adding the textbox based on GET/POST paramaters and using the $_SESSION variable to hold stuff.

2. Make an AJAX call on the click of the button to a page where your server side logic knows to give back a textbox. This would be inserted into the page using javascript.

3. Use pure javascript to do it...

for the latter somthing like this might work:

	function AddTextBox(){
		document.getElementById('container').innerHTML='<input type="text">';
<button onclick="AddTextBox();">push me</button>
<div id="container"></div>

make the equals sign in 'innerHTML =' into a += and you can spam new textbox's to your hearts content.


ok i like your idea but im required to use php. im getting the point in reloading the page, but how? should i use function to display the textbox?


If you have to use PHP then simply reload your page with a GET param is probably the easyest way:

say you are doing this for index.php, simply put a link to index.php?withButton=true (you can link with a button using a variety of methods).

in ur php, where the textbox would be displayed put somthing to the effect of....

<?php if(isset($_GET['withButton'])){ ?>
<input type="text">
<?php } ?>

I cant remember with PHP if the isset() is essential but its there for safety sake.


how can I add the variable name? <name="id"> how to auto increment the text name? <name="id2"> and so on

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.