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

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:

<script>
	function AddTextBox(){
		document.getElementById('container').innerHTML='<input type="text">';
	}
</script>
<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.

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

shorter

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.