PHP - Inserting Data To Mysql Table From Bootstrap Dynamic Fields - Stack Overflow
PHP - Inserting Data To Mysql Table From Bootstrap Dynamic Fields - Stack Overflow
See what nearly 90,000 developers picked as their most loved, dreaded, and desired coding languages and more in
the 2019 Developer Survey.
fields
// close connection
Learn More mysqli_close($link);
?>
FORM CODE
$(document).ready(function() {
var titleValidators = {
row: '.col-sm-3', // The title is placed inside a <div class="col-xs-4"> element
validators: {
notEmpty: {
message: 'The IP Address is required'
},
ip: {
message: 'Please enter a valid IP address'
}
}
},
isbnValidators = {
row: '.col-sm-4',
validators: {
notEmpty: {
message: 'The Asset Tag is required'
},
stringLength: {
By using our site, you acknowledge that max:
you have
15, read and understand our Cookie Policy, Privacy Policy, and our Terms of Service.
message: 'Asset tag length should be 15'
},
regexp: {
regexp: /^[A-Za-z0-9 ]*$/,
message: "No special characters allowed",
}
// isbn: {
// message: 'The Asset Tag is not valid'
//}
}
},
priceValidators = {
row: '.col-sm-3',
validators: {
notEmpty: {
message: 'The Serial No is required'
},
// numeric: {
// message: 'The Serial No must be a alphanumeric number'
//}
}
},
bookIndex = 0;
$('#bookForm')
.formValidation({
framework: 'bootstrap',
live: 'enabled',
icon: {
valid: 'glyphicon glyphicon-ok',
invalid: 'glyphicon glyphicon-remove',
validating: 'glyphicon glyphicon-refresh'
},
fields: {
'book[0].title': titleValidators,
'book[0].isbn': isbnValidators,
'book[0].price': priceValidators
}
})
// Remove fields
$('#bookForm')
.formValidation('removeField', $row.find('[name="book[' + index + '].title"]'))
.formValidation('removeField', $row.find('[name="book[' + index + '].isbn"]'))
.formValidation('removeField', $row.find('[name="book[' + index + '].price"]'));
});
<!doctype html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Boostrap Validator</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js">
</script>
<!-- FormValidation plugin and the class supports validating Bootstrap form -->
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js">
</script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/bootstrap-
validator/0.4.5/js/bootstrapvalidator.min.js"></script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/formvalidation/0.6.1/js/formValidation.js">
</script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/formvalidation/0.6.1/js/framework/bootstrap.mi
</script>
</head>
<div class="col-sm-3">
<input type="text" class="form-control" name="book[0].title" placeholder="IP
Address" />
</div>
<div class="col-sm-4">
<input type="text" class="form-control" id="isbn" name="book[0].isbn"
placeholder="Asset Tag(KVBXXXX1DESXXXX)" />
</div>
<div
By using our site, you acknowledge thatclass="col-sm-3">
you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service.
<input type="text" class="form-control" name="book[0].price" placeholder="Serial
No" />
</div>
<div class="col-sm-1">
<button type="button" class="btn btn-default addButton"><i class="fa fa-plus"></i>
</button>
</div>
</div>
<div class="form-group">
<div class="col-xs-5 ">
<button type="submit" name="submit1" class="btn btn-default">Submit</button>
</div>
</div>
</form>
<!DOCTYPE html>
<html>
<head>
<title>BootstrapValidator demo</title>
<link
By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service.
href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css"
rel="stylesheet">
</head>
<body>
<div class="container">
<div class="row">
<h2>Form data</h2>
<hr/>
<p>This is a simple page showing the data you have just
submitted</p>
<pre><?php print_r($_POST); ?></pre>
</div>
</div>
</body>
</html>
Array
(
[book] => Array
(
[0] => 3X20586
)
[title] =>
[isbn] =>
[price] =>
[submit1] =>
)
By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service.
Have you tried making the codes? Perhaps, you can supply
your codes here :) – budiantoip Feb 23 '16 at 3:28
I have edited the code. I was just checking whether the code is
working without adding dynamic fields – Basati Naveen Feb
23 '16 at 3:39
It's a commercial library, I can't try it :( Could you put this code
at your first line, and let me know the result?
print_r($_POST['book']);die; – budiantoip Feb 23 '16 at 3:57
i am getting only the last textfield Array ( [0] => 3R87676 [1] =>
) – Basati Naveen Feb 23 '16 at 6:39
1 Answer
$(document).ready(function() {
0
$('#bookForm')
.formValidation({
framework: 'bootstrap',
icon: {
valid: 'glyphicon glyphicon-ok',
invalid: 'glyphicon glyphicon-remove',
validating: 'glyphicon glyphicon-refresh'
},
fields: {
'title[]': {
// The task is placed inside a .col-xs-6 eleme
row: '.col-sm-3', // The title is placed insid
element
validators: {
By using our site, you acknowledge that younotEmpty:
have read{and understand our Cookie Policy, Privacy Policy, and our Terms of Service.
message: 'The IP Address is required'
},
ip: {
message: 'Please enter a valid IP address'
}
},
'isbn[]': {
row: '.col-sm-4',
validators: {
notEmpty: {
message: 'The Asset Tag is required'
},
stringLength: {
max: 15,
message: 'Asset tag length should be 15'
},
regexp: {
regexp: /^[A-Za-z0-9 ]*$/,
message: "No special characters allowed",
}
},
'price[]': {
row: '.col-sm-3',
validators: {
notEmpty: {
message: 'The Serial No is required'
},
}
}
}
})
.insertBefore($template);
// Remove fields
$('#bookForm')
.formValidation('removeField', $row.find('[name="t
.formValidation('removeField', $row.find('[name="i
.formValidation('removeField', $row.find('[name="p
$row.remove();
})
});
<!doctype html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; ch
<title>Boostrap Validator</title>
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3
rel="stylesheet">
<link href="http://cdnjs.cloudflare.com/ajax/libs/boot
By using our site, you acknowledge that you have read and understand our Cookie Policy
validator/0.4.5/css/bootstrapvalidator.min.css" , Privacy Policy, and our Terms of Service.
rel="sty
<link href="http://netdna.bootstrapcdn.com/font-awesom
awesome.min.css" rel="stylesheet">
<link href="font-awesome-4.3.0/css/font-awesome.min.cs
<link href="css/styles.css" rel="stylesheet">
<link href="css/animate.min.css" rel="stylesheet">
<script src="https://ajax.googleapis.com/ajax/libs/jqu
</script>
<!-- FormValidation plugin and the class supports vali
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/
</script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/boo
validator/0.4.5/js/bootstrapvalidator.min.js"></script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/formvalidati
</script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/formvalidati
</script>
</head>
<div class="col-sm-3">
<input type="text" class="form-control" name="titl
</div>
<div class="col-sm-4">
<input type="text" class="form-control" name="isbn
Tag(KVBXXXX1DESXXXX)" />
</div>
<div class="col-sm-3">
<input type="text" class="form-control" name="pric
</div>
<div class="col-sm-1">
<button type="button" class="btn btn-default addBu
</button>
By using our site, you acknowledge</div>
that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service.
</div>
<!-- The template for adding new field -->
<div class="form-group hide" id="bookTemplate">
<div class="col-sm-3 ">
<input type="text" class="form-control" name="titl
</div>
<div class="col-sm-4">
<input type="text" class="form-control" name="isbn
Tag(KVBXXXX1DESXXXX)" />
</div>
<div class="col-sm-3">
<input type="text" class="form-control" name="pric
</div>
<div class="col-sm-1">
<button type="button" class="btn btn-default remov
</i>
</button>
</div>
</div>
<div class="form-group">
<div class="col-xs-5 ">
<button type="submit" class="btn btn-default">Subm
</div>
</div>
</form>
<?php
//var_dump($_POST);
print_r($_POST['title']);
echo '<br/>';
print_r($_POST['isbn']);
echo '<br/>';
print_r($_POST['price']);
By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service.
die;
?>
By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service.