This tutorial helps to add or remove textbox dynamically using jQuery and PHP. It also describes how to read the values of dynamically created text boxes and save them to the database.
This screenshot shows the dynamically add input box using jQuery AJAX add more option. The checkboxes before the dynamically added input field rows are used to bulk select the rows to delete.
Run this CREATE statement while setting up this example in your local environment.
CREATE TABLE `item` (
`id` int(11) NOT NULL,
`item_name` varchar(255) DEFAULT NULL,
`item_price` double DEFAULT NULL
);
--
-- Indexes for table `item`
--
ALTER TABLE `item`
ADD PRIMARY KEY (`id`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `item`
--
ALTER TABLE `item`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
Save this code as input.php. This file will be loaded later using jQuery to append textbox HTML code to the target location.
<div class="product-item float-clear" style="clear: both;">
<div class="float-left">
<input type="checkbox" name="item_index[]" />
</div>
<div class="float-left">
<input type="text" name="item_name[]"/>
</div>
<div class="float-left">
<input type="text" name="item_price[]" />
</div>
</div>
The textbox markup will be added dynamically by using jQuery append() and load(). The code is,
function addMore() {
$("<div>").load("input.php", function() {
$("#product").append($(this).html());
});
}
We have the checkbox for each row dynamically added the text box for selecting rows to be removed from the delete event. And script for removing checked items is,
function deleteRow() {
$('div.product-item').each(function(index, item) {
jQuery(':checkbox', this).each(function() {
if ($(this).is(':checked')) {
$(item).remove();
$("#delete-button-required").html("Choose at least 1 row.").css("color", "red").hide();
}
else {
$("#delete-button-required").html("Choose at least 1 row.").css("color", "red").show();
$("#error").hide();
}
});
});
}
This is, as usual, what we have seen in several MySQL crud examples. The code is,
<?php
if (! empty($_POST["save"])) {
$connection = mysqli_connect("localhost", "root", "", "dynamic_textbox");
$itemCount = count($_POST["item_name"]);
for ($i = 0; $i < $itemCount; $i ++) {
if (! empty($_POST["item_name"][$i]) || ! empty($_POST["item_price"][$i])) {
$query = "INSERT INTO item (item_name,item_price) VALUES(?, ?)";
$statement = $connection->prepare($query);
$statement->bind_param('si', $_POST["item_name"][$i], $_POST["item_price"][$i]);
$result = $statement->execute();
}
if (! empty($result)) {
$response = array("type"=>"success", "message"=>"Added successfully.");
} else {
$response = array("type"=>"error", "message"=>"Please enter the data.");
}
}
}
?>
Hi
Thanks for this post,it is really helpful.
I wanna add selecte menu php ajax in this page. What can i do ?
hi
Hi Sudhakar :-)
working excellenty,thanks.
Welcome Dasmahapatra, happy to hear :-)
good article Thanks
Welcome Sarwar.
It’s really helpful. Thanks
Welcome Riya.