Live check availability inside dynamic form - php

I have 2 forms, static and dynamic form. Dynamic is optional, depends on user if they want to submit more than one inputs. The insertion process works fine. Then, I have a function to check the availability for the input that user want to insert. The thing is, the live check availability only works in static form. How I want to call back the live check availability function inside the dynamic form? Your help is truly appreciate :)
index.php
//static form
<form action="" method="post" enctype="multipart/form-data">
<div class="field_wrapper">
<div class="row">
<div class="col-md-1">
<a rel="tooltip" title="Add" class="btn btn-info btn-icon add_button" href="javascript:void(0);"><i class="ti-plus"></i></a>
</div>
<div class="col-md-2">
<div class="form-group">
<label class="control-label pull-left">Category<star>*</star>
</label>
<input class="form-control" type="text" name="cat[]" id="cat" title="This field needed" required="true" placeholder="Student"/><span id="usercheck3" class="help-block pull-left"></span>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<input class="btn btn-warning btn-fill btn-wd pull-right" name="submit2" id="submit2" type="submit" value="Add">
</div>
</div>
</div>
<br>
</form>
<script type="text/javascript">
$(document).ready(function(){
//live check availability
$('#cat').keyup(function() {
var usercheck3 = $(this).val();
$.post("check.php", {user_name3: usercheck3} , function(data)
{
if (data.status == true)
{
$('#usercheck3').parent('div').removeClass('has-error').addClass('has-success');
$('#submit2').attr("disabled", false);
} else {
$('#usercheck3').parent('div').removeClass('has-success').addClass('has-error');
$('#submit2').attr("disabled", true);
}
$('#usercheck3').html(data.msg);
},'json');
});
//dynamic form
var addButton = $('.add_button');
var wrapper = $('.field_wrapper');
var fieldHTML = '<div class="form-group"><div class="row"><div class="col-md-1"><a rel="tooltip" title="Delete" class="btn btn-info btn-icon remove_button" href="javascript:void(0);"><i class="ti-minus"></i></a></div><div class="col-md-2"><label class="control-label pull-left">Category<star>*</star></label><input class="form-control" type="text" name="cat[]" id="cat" title="This field needed" required="true" placeholder="Student"/><span id="usercheck3" class="help-block pull-left"></span></div></div>';
var x = 1;
$(addButton).click(function(){
x++;
$(wrapper).append(fieldHTML);
});
$(wrapper).on('click', '.remove_button', function(e){
e.preventDefault();
$(this).parents('.form-group').remove();
x--;
});
});
</script>
check.php
//db connection
if (isset($_POST['user_name3']) && $_POST['user_name3'] != '') {
$response = array();
$cat = $_POST['user_name3'];
$sql = "select cat from cat_tables where cat='" . $cat . "'";
$res = mysqli_query($con, $sql);
$count = mysqli_num_rows($res);
if ($count > 0) {
$response['status'] = false;
$response['msg'] = 'Not available';
} else {
$response['status'] = true;
$response['msg'] = 'Available';
}
}
echo json_encode($response);

Related

Insert multiple values in row php

I can`t find a solution for this formular, so i am asking you guys how i can solve this: (1)I have this formular:
<form id="formRetur" action="" method="POST" novalidate="novalidate">
<div id="mail-status" style="color: black;text-align: center;"></div>
<h3><strong>
Detalii client
</strong></h3>
<div class="row">
<div class="col-md-4">
<div class="form-group">
<input type="text" class="form-control" name="nume_prenume" id="nume_prenume" placeholder="Nume Prenume">
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<input type="text" class="form-control" name="nr_tlf" id="nr_tlf" placeholder="Numar telefon">
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<input type="text" class="form-control" name="adresa_email" id="adresa_email" placeholder="Adresa email">
</div>
</div>
</div>
<h3><strong>
Detalii comanda
</strong></h3>
<div class="row">
<div class="col-md-4">
<div class="form-group">
<input type="text" class="form-control" name="nr_comanda" id="nr_comanda" placeholder="Numar comanda">
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<input type="text" class="form-control" name="data_comanda" id="data_comanda" placeholder="Data comanda">
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<input type="text" class="form-control" name="nr_factura" id="nr_factura" placeholder="Numar factura">
</div>
</div>
</div>
<h3><strong>
Detalii produse
</strong></h3>
<div class="row">
<i class="fa fa-plus" aria-hidden="true" style="position: absolute;z-index:999;"></i>
<div class="col-md-4">
<div class="form-group">
<input type="text" class="form-control" name="den_prod" id="den_prod" placeholder="Denumire produs">
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<input type="text" class="form-control" name="culoare_prod" id="culoare_prod" placeholder="Culoare produs">
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<input type="text" class="form-control" name="cantitate" id="cantitate" placeholder="Cantitate">
</div>
</div>
</div>
<div class="maimulte">
</div>
<h3><strong>
Motiv retur
</strong></h3>
<div class="row">
<div class="col-md-12">
<textarea name="motiv" id="motiv" class="form-control" cols="30" rows="10"></textarea>
</div>
</div>
<div class="row">
<div class="col-md-12" style="margin-top:10px;margin-bottom:10px;"><div class="g-recaptcha" data-sitekey="<?php echo SITE_KEY; ?>"></div> </div>
<div class="col-md-12">
<button type="submit" id="send-message" class="btn btn-default">Trimite </button>
</div>
<div id="loader-icon" style="display:none;"><img src="images/loader.gif"></div>
</div>
</form>
I use ajax to insert data in DB, but i have an option where you can add more products when user press + adding +1 row with 3 fields name,color, qty and i want to insert also these in db, here is my ajax:
<script>
$(document).ready(function (e){
$("#formRetur").on('submit',(function(e){
e.preventDefault();
$("#mail-status").hide();
$('#send-message').hide();
$('#loader-icon').show();
$.ajax({
url: "valid_retur.php",
type: "POST",
dataType:'json',
data: {
"nume_prenume":$('input[name="nume_prenume"]').val(),
"nr_tlf":$('input[name="nr_tlf"]').val(),
"adresa_email":$('input[name="adresa_email"]').val(),
"nr_comanda":$('input[name="nr_comanda"]').val(),
"data_comanda":$('input[name="data_comanda"]').val(),
"nr_factura":$('input[name="nr_factura"]').val(),
"den_prod":$('input[name="den_prod"]').val(),
"culoare_prod":$('input[name="culoare_prod"]').val(),
"cantitate":$('input[name="cantitate"]').val(),
"motiv":$('textarea[name="motiv"]').val(),
"g-recaptcha-response":$('textarea[id="g-recaptcha-response"]').val()},
success: function(response){
// $( '#frmContact' ).each(function(){
// this.reset();
// });
$("#mail-status").show();
$('#loader-icon').hide();
if(response.type == "error") {
$('#send-message').show();
$("#mail-status").attr("class","alert alert-danger");
} else if(response.type == "message"){
// grecaptcha.reset();
$('#send-message').show();
$("#mail-status").attr("class","alert alert-success");
}
$("#mail-status").html(response.text);
},
error: function(){}
});
}));
});
</script>
Here is my db:enter image description here
And there is my php file who insert in db with values from ajax:
<?php
if($_POST)
{
include('config.php');
$nume_prenume = filter_var($_POST["nume_prenume"], FILTER_SANITIZE_STRING);
$nr_tlf = filter_var($_POST["nr_tlf"], FILTER_SANITIZE_STRING);
$adresa_email = filter_var($_POST["adresa_email"], FILTER_SANITIZE_STRING);
$nr_comanda = filter_var($_POST["nr_comanda"], FILTER_SANITIZE_STRING);
$data_comanda = filter_var($_POST["data_comanda"], FILTER_SANITIZE_STRING);
$nr_factura = filter_var($_POST["nr_factura"], FILTER_SANITIZE_STRING);
$den_prod = filter_var($_POST["den_prod"], FILTER_SANITIZE_STRING);
$culoare_prod = filter_var($_POST["culoare_prod"], FILTER_SANITIZE_STRING);
$cantitate = filter_var($_POST["cantitate"], FILTER_SANITIZE_STRING);
$motiv = filter_var($_POST["motiv"], FILTER_SANITIZE_STRING);
if(empty($nume_prenume)) {
$empty[] = "<b>nume_prenume</b>";
}
if(empty($nr_tlf)) {
$empty[] = "<b>nr_tlf</b>";
}
if(empty($adresa_email)) {
$empty[] = "<b>adresa_email</b>";
}
if(empty($nr_comanda)) {
$empty[] = "<b>nr_comanda</b>";
}
if(empty($data_comanda)) {
$empty[] = "<b>data_comanda</b>";
}
if(empty($den_prod)) {
$empty[] = "<b>data_comanda</b>";
}
if(empty($culoare_prod)) {
$empty[] = "<b>data_comanda</b>";
}
if(!empty($empty)) {
$output = json_encode(array('type'=>'error', 'text' => implode(", ",$empty) . ' obligatoriu!'));
die($output);
}
// if(!filter_var($email2, FILTER_VALIDATE_EMAIL)){ //email validation
// $output = json_encode(array('type'=>'error', 'text' => '<b>'.$email2.'</b> is an invalid Email, please correct it.'));
// die($output);
// }
//reCAPTCHA validation
if (isset($_POST['g-recaptcha-response'])) {
require('component/recaptcha/src/autoload.php');
$recaptcha = new \ReCaptcha\ReCaptcha(SECRET_KEY, new \ReCaptcha\RequestMethod\SocketPost());
$resp = $recaptcha->verify($_POST['g-recaptcha-response'], $_SERVER['REMOTE_ADDR']);
if (!$resp->isSuccess()) {
$output = json_encode(array('type'=>'error', 'text' => '<b>Captcha</b> Validation Required!'));
die($output);
}
}
// bag in db
$sql0 = "INSERT INTO retur (nume_prenume, nr_tlf, email, nr_comanda, data_comanda, nr_factura, den_prod, culoare_prod, cantitate_prod, motiv_retur)
VALUES ('$nume_prenume', '$nr_tlf', '$adresa_email', '$nr_comanda', '$data_comanda', '$nr_factura', '$den_prod', '$culoare_prod', '$cantitate', '$motiv')";
mysqli_query($conn, $sql0);
}
So basically this is a formular for returns products and if he have more products to return i added that jquery to add more fields and i want to be inserted in databased then i will fetch data and display it in my CMS.
1.You don't have to write that much code in ajax. You can easily use serialize():
Reduce you ajax code with below code:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
var form=$("#your_form_id");
$("#Your_submit_id").click(function(){
$.ajax({
type:"POST",
url:"URL",
data:form.serialize(),
success: function(response){
console.log(response);
}
});
});
});
</script>
2.To add multiple product:
-You have to create every input field as array for product info like:
<input type="text" name="prod_name[]">
This array statement store you every product info index wise. If you
used normal variable then it will post only you last product
details.
In Php code you have to take the count of posted
product title & use for loop to get the every product information.
for($i=0;$i<count($prod_name);$i++)
{
//Insert/fetch All Values like : $prod_name[$i]
}
-When you click on add, just create clone of the prev available div.

Error messages for dynamic input fields using PHP

I have created dynamic add/remove input fields. I want to pass certain validations for it. If the user keeps it blank, a message should get displayed after submitting the form. I want PHP validations to be displayed.
<div class="panel panel-default">
<div class="panel-heading"><center><b>Allocation of Funds</b></center></div>
<div class="panel-body">
<div class="row">
<div class="col-md-5"><label>Allocation Items <b style="color:#FF0000;">*</b></label></div>
<div class="col-md-5"><label>Amount <b style="color:#FF0000;">*</b></label></div>
<div class="col-md-2"></div>
</div>
<div class="row">
<div class="col-md-5">
<div class="form-group">
<input type="text" class="form-control" name="allocate_items[]" placeholder=""></div>
</div>
<div class="col-md-5">
<div class="form-group">
<input type="text" class="form-control" name="allocate_amount[]" placeholder=""></div>
</div>
<div class="col-md-2">
<button type="button" class="btn btn-success" id="add-allocation-fields"><span class="glyphicon glyphicon-plus" aria-hidden="true"></span> Add</button>
</div>
</div>
<div id="fund-allocation-fields"></div>
<p class="help-block"><i>Total amount must be equal to the goal amount.</i></p>
</div>
</div>
<script type="text/javascript">
var i = 0;
jQuery(document).ready(function($) {
//fadeout selected item and remove
$(document).on('click', '#remove-allocation-fields', function(event) {
event.preventDefault();
$(this).parent().fadeOut(300, function() {
$(this).parent().empty();
return false;
});
});
var rows = '<div class="fund-fields"><div class="row"><div class="col-md-5"><div class="form-group"><input type="text" class="form-control" name="allocate_items[]" placeholder=""></div></div><div class="col-md-5"><div class="form-group"><input type="text" class="form-control" name="allocate_amount[]" placeholder=""></div></div><div class="col-md-2"><button type="button" class="btn btn-danger" id="remove-allocation-fields"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span> Remove</button></div></div><div class="clear"></div></div>';
//add input
$('#add-allocation-fields').click(function() {
$(rows).fadeIn("slow").appendTo('#fund-allocation-fields');
i++;
return false;
});
});
</script>
Thanks in advance!
Store one input value in a session variable or global session and compare it against required input value using a conditional statement. It's that simple.
like,
if(isset($_SESSION['A']) && empty(trim($_POST['RequiredField']))){
echo 'required field can\'t be empty';
}
I found the answer...
<?php
$errors = array();
if (isset($_POST['error_test'])) {
$allocate_items = $_POST['allocate_items'];
foreach ( $allocate_items as $key => $allocate_item ) {
if ( empty($allocate_item[$key])) {
$errors[$key] = '<p>Allocate Item field is required</p>';
}
}
if ( ! empty( $errors ) ) {
foreach ($errors as $key => $value) {
echo $value;
}
}
}
?>

Remove button removes all the dynamically created fileds using the data from database

This is my view with form where my data is dynamically adding fields using foreach:-
<div class="form-body1">
<div class="col-md-4">
<?php if(!empty($content['Items']))
{
$item = json_decode($content['Items'],true);
foreach ($item as $key => $object)
{ ?>
<?php if($key ==0)
{
for($i=0; $i<1 ; $i++ )
{?>
<label class="col-md-3 control-label"> Description</label>
<input type="text" value = '<?php echo $object['Description'];?>'placeholder="Description" name = 'description[]' maxlength="255" class="form-control input-circle">
<label class="col-md-2 control-label">Price</label>
<input type="number" value = "<?php echo $object['Price'];?>" placeholder="0.00" id="i-item_price_0" name = 'usd[]' autocomplete="off" class="form-control input-circle">
<?php}
}
else
{ ?>
<label class="col-md-3 control-label"> Description</label>
<input type="text" value = '<?php echo $object['Description'];?>'placeholder="Description" name = 'description[]' maxlength="255" class="form-control input-circle">
<label class="col-md-2 control-label">Price</label>
<input type="number" value = '<?php echo $object['Price'];?>' placeholder="0.00" id="i-item_price_0" name = 'usd[]' autocomplete="off" class="form-control input-circle">
<button href="#" id = "remove_field" class="btn btn-danger">Remove</button>
<br>
<?php }
}
}
else
{ ?>
<label class="col-md-3 control-label"> Description</label>
<input type="text" placeholder="Description" name = 'description[]' maxlength="255" class="form-control input-circle"> <label class="col-md-2 control-label">Price</label><input type="number" placeholder="0.00" id="i-item_price_0" name = 'usd[]' autocomplete="off" class="form-control input-circle">
<?php } ?>
</div>
</div>
As you can see in my view i have taken a foreach for data from database coming in the variable $content . But i have also included the add another item field which ignites my jquery and dynamically adds fields of "description" and price along with a remove option with it.
<div class="form-actions">
<div class="row">
<div class="col-md-offset-3 col-md-9">
<button class="btn btn-circle blue" id = 'add_item' >Add another Item</button>
<button type="submit" onclick="location.href='<?php echo base_url();?>/index.php/invoice_invoice/create_invoice'" class="btn btn-circle green">Create Invoice</button>
<button type="submit" name = 'draft' value = 'draft' onclick="location.href='<?php echo base_url();?>/index.php/invoice_invoice/create_invoice'" class="btn btn-circle grey-salsa btn-outline">Cancel</button>
</div>
</div>
</div>
Here is the jquery that runs this whole process. When I click the remove button of my dynmaically added fields with the data coming from database ,all the fields that are created gets removed.
<script>
$(document).ready(function() {
var max_fields = 9; //maximum input boxes allowed
var wrapper = $(".form-body1"); //Fields wrapper
var add_button = $("#add_item"); //Add button ID
var x = 1; //initial text box count
$(add_button).click(function(e){ //on add input button click
e.preventDefault();
if(x < max_fields){ //max input box allowed
x++; //text box increment
$(wrapper).append('<div class="col-md-4"><label class="col-md-3 control-label"> Description</label><input type="text" name = "description[]" placeholder="Description" maxlength="255" class="form-control input-circle" required> <label class="col-md-2 control-label">Price</label> <input type="text" name = "usd[]" placeholder="0.00" id="i-item_price_0" autocomplete="off" class="form-control input-circle" required><button href="#" id = "remove_field" class="btn btn-danger">Remove</button></div>'); //add input box
}
else{ alert('Maximum 9 Items!');}
});
$(wrapper).on("click","#remove_field", function(e){ //user click on remove text
e.preventDefault(); $(this).parent('div').remove(); x--;
})
});
</script>
You shouldn't use the same id for multiple elements, instead use class. Change:
<button href="#" id = "remove_field" class="btn btn-danger">Remove</button>
To:
<button href="#" class="btn btn-danger remove_field">Remove</button>
Then select the class instead of id, change:
$(wrapper).on("click","#remove_field", function(e){});
To:
$(wrapper).on("click",".remove_field", function(e){});
EDIT:
I dont know how could i assign different id's if they are created dynamically – Bhargav Trivedi
You can use the x variable you defined to increment the id:
<button href="#" id="remove_field_' + x + '" class="btn btn-danger">Remove</button>

Images not properly uploading

I tried to create a gallery using php admins can upload multiple images all the images are uploading correctly like the image is saving in the database as much as I can but the problem is that the image is saving once in the folder
Like loop runs images name saves in the database twice
Forexampl
img1.jpg
img2.jpg
img3.jpg
this is what comes in database but when i open the the folder i see
img1.jpg the rest are not coming
here is my code
$imggall = $_FILES["gallery_img"]["name"];
$imgtype = $_FILES["gallery_img"]["type"];
$imgtemp = $_FILES["gallery_img"]["tmp_name"];
foreach($imggall as $key => $imgname) {
$path = "./img/gallery/".$imgname;
foreach($imgtemp as $key1 => $imgtemp1) {
move_uploaded_file($imgtemp1, $path);
}
$confirm = mysqli_query($connect, "INSERT INTO gallery (image) VALUES ('$imgname')");
}
Here is the HTML form for the page post.php
<p>
<form method="POST" action="post.php?id=<?php echo $_SESSION["id"]; ?>" enctype="multipart/form-data">
<div class="form-group">
<label>Title</label>
<input class="form-control" name="title">
</div>
<div class="form-group">
<label>Category</label>
<div class="checkbox">
<?php
$query = mysqli_query($connection, "SELECT DISTINCT cat_name FROM category");
while($row = mysqli_fetch_assoc($query)) {
if(!empty($row["cat_name"])) {
?>
<label><input type="checkbox" name='catname' value="<?php echo $row["cat_name"]; ?>"><?php echo $row["cat_name"]; ?></label>
<?php } } ?>
<?php
$query1 = mysqli_query($connection, "SELECT DISTINCT parent FROM category");
while($row1 = mysqli_fetch_assoc($query1)) {
if($row1["parent"] !== "None") {
?>
<label><input type="checkbox" name='catname' value="<?php echo $row1["parent"]; ?>"><?php echo $row1["parent"]; ?></label>
<?php } } ?>
</div>
</div>
<div class="form-group">
<label>Featured Image</label>
<input type="file" name="ft_img">
</div>
<div class="form-group">
<label>Description</label>
<textarea class="form-control" name="descrip" rows="6"></textarea>
</div>
<div class="form-group">
<label>Featured</label>
<div class="checkbox">
<label><input type="checkbox" name='fetch' value="Featured">Featured</label>
</div>
</div>
<div class="form-group">
<label>Android Button</label>
<select class="form-control" name="android">
<option>Yes</option>
<option>No</option>
</select>
</div>
<div class="form-group">
<label>Iphone Button</label>
<select class="form-control" name="iphone">
<option>Yes</option>
<option>No</option>
</select>
</div>
<div class="form-group">
<label>Custom Button Text</label>
<input class="form-control" name="cbtn_text">
</div>
<div class="form-group">
<label>Custom Button Link</label>
<input class="form-control" name="cbtn_url">
</div>
<script type="text/javascript">
$(document).ready(function() {
var max_fields = 10; //maximum input boxes allowed
var wrapper = $(".input_fields_wrap"); //Fields wrapper
var add_button = $(".add_field_button"); //Add button ID
var x = 1; //initlal text box count
$(add_button).click(function(e){ //on add input button click
e.preventDefault();
if(x < max_fields){ //max input box allowed
x++; //text box increment
$(wrapper).append('<div class="form-group"><input type="file" name="gallery_img[]" style="float:left;" /><i class="fa fa-times"></i><div class="clear"></div></div>'); //add input box
}
});
$(wrapper).on("click",".remove_field", function(e){ //user click on remove text
e.preventDefault(); $(this).parent('div').remove(); x--;
})
});
</script>
<div class="input_fields_wrap form-group">
<div class="form-group">
<button class="add_field_button btn btn-default">Add More Fields</button>
</div>
<div class="form-group">
<label>Gallery Images</label>
<input type="file" name="gallery_img[]">
<div class="clear"></div>
</div>
</div>
<div class="form-group">
<input type="submit" class="btn btn-default purple-btn" value="Submit" name="posts" />
<input type="Reset" class="btn btn-default purple-btn" value="Reset Value" />
Cancel
</div>
</form>
</p>
looping twice may effect performance, this code works for me in my gallery
<?php
$files = $_FILES['gallery_img']['name'];
$fileCount = count($files);
for($i=0; $i<$fileCount; $i++) {
$tmpFilePath = $files[$i];
if ($tmpFilePath != ""){
$newFilePath = "./img/gallery/" . $files[$i];
if(move_uploaded_file($tmpFilePath, $newFilePath)) {
// if files are uploaded then only insert into database
$confirm = mysqli_query($connect, "INSERT INTO gallery (image) VALUES ('$files[$i]')");
}
}
}
?>
Hope this may work

Bootstrap: Using a disabled button as a form input

There may be a better way to do this, but I'm trying to create a button group where the left and right buttons are increment/decrement, and the middle button is a disabled button representing the value.
The field properly increments/decrements, but the value isn't getting passed to php.
I do like how the button group looks:
HTML
<form method="POST" action="send_email.php">
<div class="row"">
<div class="col-sm-4 col-xs-4">
<h5><b>Attendees</b></h5>
</div>
<div class="col-sm-8 col-xs-8">
<div class="btn-group">
<input type="button" class="btn btn-default" name="decrease" value="-" onclick="decreaseBtnOnclick('attendees')"/>
<input type="button" class="btn btn-primary" name="attendees" value="0" id="chairs" disabled/>
<input type="button" class="btn btn-default" name="increase" value="+" onclick="increaseBtnOnclick('attendees')"/>
</div>
</div>
</div>
<div class="text-right">
<input type="hidden" name="email" value="contact">
<a href="./index.php">
<button type="submit" class="btn-main">Finish!</button>
</a>
</div>
</form>
Javascript (works fine to change the value in the middle button)
<script>
function increaseBtnOnclick(item) {
document.getElementById(item).value = Number(document.getElementById(item).value) + 1;
}
function decreaseBtnOnclick(item) {
if(document.getElementById(item).value > 0){
document.getElementById(item).value = Number(document.getElementById(item).value) - 1;
}
}
PHP (works fine for other types of inputs, e.g., text)
<?php
if(isset($_POST['email'])) {
$attendees = $_POST['attendees']; // required
echo "Attendees: ".$attendees;
}
?>
Try adding a hidden input then add the value of your disabled button to it with JS.
JS
function increaseBtnOnclick(item) {
var newValue = Number(document.getElementById(item).value) + 1;
document.getElementById(item).value = newValue;
document.getElementById("hidden_input").value = newValue;
}
function decreaseBtnOnclick(item) {
if(document.getElementById(item).value > 0){
var newValue = Number(document.getElementById(item).value) - 1;
document.getElementById(item).value = newValue;
document.getElementById("hidden_input").value = newValue;
}
}
HTML:
<form method="POST" action="send_email.php">
<div class="row"">
<div class="col-sm-4 col-xs-4">
<h5><b>Attendees</b></h5>
</div>
<div class="col-sm-8 col-xs-8">
<div class="btn-group">
<input type="button" class="btn btn-default" name="decrease" value="-" onclick="decreaseBtnOnclick('attendees')"/>
<input type="button" class="btn btn-primary" name="attendees" value="0" id="chairs" disabled/>
<input type="button" class="btn btn-default" name="increase" value="+" onclick="increaseBtnOnclick('attendees')"/>
<input type="hidden" name="hiddenvalue" id="hidden_input" value="0">
</div>
</div>
</div>
<div class="text-right">
<input type="hidden" name="email" value="contact">
<a href="./index.php">
<button type="submit" class="btn-main">Finish!</button>
</a>
</div>
</form>
PHP:
<?php
if(isset($_POST['email'])) {
$attendees = $_POST['hiddenvalue']; // required
echo "Attendees: ".$attendees;
}
?>
You could switch to a regular input field with button addons.
Disabled input fields do not get posted, however you can set it to readonly.
Please have a look at this thread,
Disabled form inputs do not appear in the request
You could use a hidden input field:
<input type="hidden" id="h_attendees" name="h_attendees" value="0"/>
In your increaseBtnOnclick and decreaseBtnOnclick you could set the value of the hidden field.
function increaseBtnOnclick(item) {
document.getElementById(item).value = Number(document.getElementById(item).value) + 1;
document.getElementById("h_attendees").value = document.getElementById(item).value;
}
function decreaseBtnOnclick(item) {
if(document.getElementById(item).value > 0){
document.getElementById(item).value = Number(document.getElementById(item).value) - 1;
document.getElementById("h_attendees").value = document.getElementById(item).value;
}
}
And access it in PHP
$_POST['h_attendees'];

Categories