How to display data from database using JQuery Repeater and php - php

I have been tying to make JQuery repeater plugin works fetching data from database. I already have this working inserting the data into the database but I have not look retrieving the inserted data to edit it.
This is the code I have in php and html.
<div id="repeater">
<div class="repeater-heading" align="left">
<a role="button" class="btn btn-info repeater-add-btn" ><span class="glyphicon glyphicon-plus"> </span>Add Transactional Impact</a>
</div>
<div class="clearfix"></div>
<br>
<div class="items" data-group="impacts">
<div class="item-content">
<div class="form-group">
<?php
$query_raw_num = "SELECT * FROM revenue_impact_number WHERE revenue_impact_id='".$id."' ";
$results = $mysqli->query($query_raw_num);
while($impacts = $results->fetch_assoc()){
?>
<div class="row" data-repeater-item>
<div class="col-md-2">
<label>Region:</label>
<select data-skip-name="true" data-name="region_id[]" id="region_id" class="form-control">
<option value="">-- Select --</option>
<?php
$query_raw = "SELECT regions_id, regions_name FROM regions ";
$result = $mysqli->query($query_raw);
while($row = $result->fetch_assoc()){
?>
<option value="<?php echo $row['regions_id']; ?>" <?php echo ($row['regions_id']==$impacts['region_id'] ? 'selected' : ''); ?> ><?php echo $row['regions_name']; ?></option>
<?php } ?>
</select>
</div>
<div class="col-md-2">
<label>Product:</label>
<select data-skip-name="true" data-name="product_id[]" id="product_id" class="form-control">
<option value="">-- Select --</option>
<?php
$query_raw_products = "SELECT product_id, product_name FROM products ";
$result_products = $mysqli->query($query_raw_products);
while($products = $result_products->fetch_assoc()){
?>
<option value="<?php echo $products['product_id']; ?>" <?php echo ($products['product_id']==$impacts['product_id'] ? 'selected' : ''); ?> ><?php echo $products['product_name']; ?></option>
<?php } ?>
</select>
</div>
<div class="col-md-2">
<label>Impact:</label>
<input type="text" data-skip-name="true" data-name="impact_number[]" id="impact" value="<?php echo $impacts['impact_number'];?>" class="form-control" style="width:200px;"/>
</div>
<div class="col-md-2">
<label>Expected:</label>
<input type="text" data-skip-name="true" data-name="expected[]" id="expected" value="<?php echo $impacts['expected'];?>" class="form-control" style="width:200px;"/>
</div>
<div class="col-md-2" style="margin-top:24px;" align="center">
<a id="remove-btn" role="button" onclick="$(this).parents('.item-content').remove()" class="btn btn-danger" ><span class="glyphicon glyphicon-trash"> </span>Remove</a>
</div>
</div>
<?php } ?>
</div>
</div>
</div>
</di>
This is the script function I am using.
$(function(){
$('#repeater').createRepeater();
});
When the page load for the first time everything is displayed successfully. See below image
The issue I am having is that when I try to add new group of fields it is displaying the whole group again and when I click on remove button it is removing the whole data group as well. See below image.

While retrieving the existing data while edit there is an issue in repeater code. Comment below lines from "repeater.js" files
/*
if (key == 0) {
items.remove();
addItem(newItem, key);
}
*/

Related

How can I populate the room types for a resort (hotel) in a drop down menu if the rooms types are more than one for a particulate hotel (resort)?

I am developing an online travel bookings system. Infact i bought a an online booking system and I am modifying it to my needs. The problem here is with the check availability form for a particular resorts or hotels. I want to populate a dropdown menu with room types for that particular hotel but im not able to populate the dropdown menu if the hotel has more than one room types
Here is the particular code which asks to populate the dropdown menu
$result_room = $db->query("SELECT * FROM pm_room WHERE checked = 1 AND id_hotel = '.$hotel_id.' AND lang = ".LANG_ID);
The table for rooms is "pm_room" and in that table there is a column for hotel id which is "id_hotel"
The file im modifying is search-min.php
here below is the code in search-min.php
<?php
debug_backtrace() || die ("Direct access not permitted");
$max_adults_search = 5;
$max_children_search = 10;
if(!isset($_SESSION['destination_id'])) $_SESSION['destination_id'] = 1;
if(!isset($destination_name)) $destination_name = "";
if(!isset($_SESSION['num_adults']))
$_SESSION['num_adults'] = (isset($_SESSION['book']['adults'])) ? $_SESSION['book']['adults'] : 1;
if(!isset($_SESSION['num_children']))
$_SESSION['num_children'] = (isset($_SESSION['book']['children'])) ? $_SESSION['book']['children'] : 0;
$from_date = (isset($_SESSION['from_date'])) ? $_SESSION['from_date'] : "";
$to_date = (isset($_SESSION['to_date'])) ? $_SESSION['to_date'] : ""; ?>
<form action="<?php echo DOCBASE.$sys_pages['booking']['alias']; ?>" method="post" class="">
<?php
if(isset($hotel_id)){ ?>
<input type="hidden" name="hotel_id" value="<?php echo $hotel_id; ?>">
<?php
} ?>
<div class="form-group">
<label class="sr-only" for="from"></label>
<div class="input-group">
<div class="input-group-addon"><i class="fa fa-calendar"></i> <?php echo $texts['CHECK_IN']; ?></div>
<input type="text" class="form-control" id="from_picker" name="from_date" value="<?php echo $from_date; ?>">
</div>
</div>
<div class="form-group">
<div class="input-group">
<div class="input-group-addon"><i class="fa fa-calendar"></i> <?php echo $texts['CHECK_OUT']; ?></div>
<input type="text" class="form-control" id="to_picker" name="to_date" value="<?php echo $to_date; ?>">
</div>
</div>
<div class="form-group">
<div class="input-group">
<div class="input-group-addon"><i class="fa fa-tags"></i> <?php echo $texts['ROOM']; ?></div>
<select class="form-control" name="room_id">
<?php
$result_room = $db->query("SELECT * FROM pm_room WHERE checked = 1 AND id_hotel = '.$hotel_id.' AND lang = ".LANG_ID);
if($result_room !== false){
foreach($result_room as $i => $row){ ?>
<option value="<?php echo $row['id']; ?>"><?php echo $row['title']; ?></option>
<?php
}
} ?>
</select>
</div>
</div>
<div class="form-group">
<div class="input-group">
<div class="input-group-addon"><i class="fa fa-male"></i> <?php echo $texts['ADULTS']; ?></div>
<select name="num_adults" class="selectpicker form-control">
<?php
for($i = 1; $i <= $max_adults_search; $i++){
$select = ($_SESSION['num_adults'] == $i) ? " selected=\"selected\"" : "";
echo "<option value=\"".$i."\"".$select.">".$i."</option>";
} ?>
</select>
</div>
</div>
<div class="form-group">
<div class="input-group">
<div class="input-group-addon"><i class="fa fa-male"></i> <?php echo $texts['CHILDREN']; ?></div>
<select name="num_children" class="selectpicker form-control">
<?php
for($i = 0; $i <= $max_children_search; $i++){
$select = ($_SESSION['num_children'] == $i) ? " selected=\"selected\"" : "";
echo "<option value=\"".$i."\"".$select.">".$i."</option>";
} ?>
</select>
</div>
</div>
<div class="form-group">
<button class="btn btn-primary" type="submit" name="check_availabilities"><i class="fa fa-search"></i> <?php echo $texts['CHECK']; ?></button>
</div>
</form>
With this code it populates the drop down menu for rooms if the hotel has only one room type:
eg:
https://www.thesandsmaldives.com/resorts/niyama
But if the hotel has more than one room type it doesn't populate the drop down for the room types.
eg:
https://www.thesandsmaldives.com/resorts/oblu-by-atmosphere-at-helengeli
What am i doing wrong here?
I figured it out with the help of its original coders from who i bought it, its a Syntax error. I was using single quote AND double quotes. Use one or the other but not both ;) I just seen it's my bad,
All room types are stored in pm_room table but there is a hotel id column in that table to pinpoint for which hotel the room type is for
the correct code is
$result_room = $db->query("SELECT * FROM pm_room WHERE checked = 1 AND id_hotel = ".$hotel_id." AND lang = ".LANG_ID);

How do I disabled button submit on change form (input fields) php

I would like to disabled a button that will submit my form if the fields aren't changed.
So if a change a field, I would like to be able to submit the form by enabling the button, but only if a change a field (minimum).
<?php $listeToitures = $db->query("SELECT * FROM i10_toit where i10_toit.N_NUME_IMME = $choixImmeuble");
$toiture = $listeToitures->fetch() ?>
<br><br><button class="btn btn-primary" type="button" data-target="#toitures" data-toggle="collapse" aria-expanded="false" aria-controls="MonCollapse"><i class="glyphicon glyphicon-chevron-right"></i> Toitures</button>
<div id="toitures" class="collapse">
<h3 class="page-header">Toitures <button type="submit" name="enregistrerParkingAmenagement" class="btn btn-default">Enregistrer</button></h3>
<button class="btn btn-primary" type="button" data-target="#parking" data-toggle="collapse" aria-expanded="false" aria-controls="MonCollapse"><i class="glyphicon glyphicon-chevron-right"></i> Parking</button>
<div id="parking" class="collapse">
<div class="col-xl-6 col-lg-6 col-sm-6 col-xs-6">
<div class="form-group">
<label for="toiture_c_etat_ferb">Etat ferblanterie :</label><br>
<select name="toiture_c_etat_ferb" id="toiture_c_etat_ferb" class="form-control">
<?php
$cherche = chercheViews('VZ1085');
foreach ($cherche as $resultat){
$code = $resultat['C_CODE_SEQU'];?>
<option value="<?php echo $code ?>"<?php if($toiture['C_ETAT_FERB']==$code) echo 'selected="selected"'; ?>><?php echo $resultat['L_DESC_CODE']; ?></option>
<?php }?>
</select>
</div>
<div class="form-group">
<label for="toiture_c_type_ferb">Type de ferblanterie :</label><br>
<select name="toiture_c_type_ferb" id="toiture_c_type_ferb" class="form-control">
<?php
$cherche = chercheViews('VZ1143');
foreach ($cherche as $resultat){
$code = $resultat['C_CODE_SEQU'];?>
<option value="<?php echo $code ?>"<?php if($toiture['C_TYPE_FERB']==$code) echo 'selected="selected"'; ?>><?php echo $resultat['L_DESC_CODE']; ?></option>
<?php }?>
</select>
<br><br><br><br><br><br><br><br>
</div>
</div>
<label for="chaufferie_l_toit_comm">Commentaire :</label><br>
<textarea name="chaufferie_l_toit_comm" class="form-control" id="chaufferie_l_faca_comm"><?php echo $toiture['L_TOIT_COMM'];?></textarea>
</div>
</div>
</div>
</form>
<script>
$(function() {
var form_original_data = $("#myform").serialize();
$("#enregistrerFacadesToitures").click(function() {
if ($("#myform").serialize() != form_original_data) {
<button type="submit" name="enregistrerFacadesToitures" class="btn btn-default">Enregistrer</button>
}
});
});
</script>
</div>
</section>
Sorry for my english.
you can use prop('disabled' , true) and prop('disabled' , false) for button .. like so
$("#enregistrerFacadesToitures").click(function() {
if ($("#myform").serialize() !== form_original_data) {
$('button[type="submit"][name="enregistrerFacadesToitures"]').prop('disabled' , true);
}else{
$('button[type="submit"][name="enregistrerFacadesToitures"]').prop('disabled' , false);
}
});
give a default disabled property to the submit button DOM, then use jQuery to check if any changes happened in the form , if yes, then delete the disabled property of this submit button DOM, if not, then just keep it as disabled.

Code Igniter - Select tag option depending on another select tag value

I'm new in CodeIgniter and still junior in programming, so don't be angry at me.
PS: don't be angry for my English too. ;)
I am creating small CMS for one project. Inside that project its few select tags, first ones have just a few values but the next ones, have many values, which depends to the one of the values, from select tag that i mention before.
Its everything ok with my database tables dependencies, etc. but I dont know how to make dependencies to show up in select tags. I tried to make it with Ajax, but I am still not make it, and I read that I can make it with codeIgniter libraries, is it true? Can any body can help me?
This is my view file:
<?php echo validation_errors('<p class="alert alert-dismissable alert-danger">'); ?>
<form method="post" action="<?php echo base_url(); ?>admin/engines/add">
<div class="row">
<div class="col-lg-6">
<h1>Engine Type</h1>
</div>
<div class="col-lg-6">
<div class="btn-group pull-right">
<input type="submit" name="submit" class="btn btn-success" value="Save">
Close
</div>
</div>
</div><!-- /.row -->
<div class="row">
<div class="col-lg-12">
<ol class="breadcrumb">
<li><i class="fa fa-dashboard"></i>Dashboard</li>
<li><i class="fa fa-pencil"></i>Variklio Tipai</li>
<li class="active"><i class="fa fa-plus-square-o"></i>Pridėti Variklio tipą</li>
</ol>
</div>
</div>
<div class="row">
<div class="col-lg-12">
<h2>Engine type One</h2>
<div class="form-group">
<label>Engine Name</label>
<input class="form-control" type="text" name="engine" value="<?php echo set_value('engine'); ?>" placeholder="Engine Type" >
</div>
<div class="form-group">
<label>Category</label>
<select name="brand" class="form-control">
<option value="category">Choose category</option>
<?php foreach($categories as $category) : ?>
<option value="<?php echo $category->id; ?>"><?php echo $category->category_name; ?></option>
<?php endforeach; ?>
</select>
</div>
<!-- END Category-->
<!-- *** Brand *** -->
<div class="form-group">
<label>Brand</label>
<select name="brand" class="form-control">
<option value="">Choose Brand</option>
<?php foreach ($brands as $brand) : ?>
<option value="<?php echo $brand->id; ?>"><?php echo $brand->v_brand_name; ?></option>
<?php endforeach; ?>
</select>
</div>
<!-- **** END Brand ***-->
<!-- *** Model *** -->
<div class="form-group">
<label>Model</label>
<select name="model" class="form-control">
<option value="">Choose model</option>
<?php foreach ($models as $model) : ?>
<option value="<?php echo $model->id; ?>"><?php echo $model->v_model_name; ?></option>
<?php endforeach; ?>
</select>
</div>
<!-- **** END MODEL ***-->
</div>
</div>
</div>
</form>
for example this is my view
<select name='list1' id='list1'>
<option value='1'>id 1</option>
<option value='2'>id 2</option>
<option value='3'>id 3</option>
<option value='4'>id 4</option>
<option value='5'>id 5</option>
</select>
this is 2nd dropdown list
<select name='list2' id='list2'>
</select>
now jquery code is
$("#list1").on("change",function(){
var value = $(this).val();
$.ajax({
url : "example.com/welcome/get_data",
type: "post",
data: {"value":value},
success : function(data){
$("#list2").html(data);
},
});
});
now my controller function is
public function get_data()
{
$value = $this->input->post("value");
$data = $this->mymodal->get_data($value);
$option ="";
foreach($data as $d)
{
$option .= "<option value='".$d->id."' >".$d->name."</option>";
}
echo $option;
}
my model function is
public function get_data($value)
{
$this->db->where("id",$value);
return $this->db->get("table_name")->result();
}

Can not send Multiple choices from select2 with fromdata to upload file

I have html select2,it can selected multiple option:
<div class="form-group">
<label class="col-sm-1 control-label no-padding-right" for="form-field-recipient">Recipient:</label>
<div class="col-sm-6 col-xs-12">
<span class="input-icon block col-xs-12 no-padding">
<select name="for" id="for" class="for" multiple="multiple" placeholder="For" style="width: 92%">
<?php
foreach($this->forList as $dataFor){
?>
<option value="<?php echo $dataFor['ohp_id'];?>"><?php echo $dataFor['title'];?> Of <?php echo $dataFor['name'];?></option>
<?php } ?>
<?php
foreach($this->group as $group){
?>
<option value="<?php echo $group['object_group_id'];?>"><?php echo $group['object_group_name'];?></option>
<?php } ?>
</select>
<i class="ace-icon fa fa-user col-xs-0"></i>
</span>
</div>
</div>
$(document).ready(function() {
$(".refnd").select2({placeholder: "Reference ND"});
$(".for").select2({placeholder: "For"});
And this jquery:
But in PHP,it just show 1 option selected. How To fix it?
Change the dropdown name as an array:
<select name="for" id="for" class="for" multiple="multiple" placeholder="For" style="width: 92%">
<select name="for[]" id="for" class="for" multiple="multiple" placeholder="For" style="width: 92%">
JS:
var selectedOpt = $('#for').val(); // it will return an array of selected options
// Now use the selectedOpt array in your ajax call

Dropdownlist items disappears when I hit submit, when a value is already in DB

I'm just learning Mysql/PHP andI'm having some issues with my dropdownlist.
Problem : When I hit Sumbit, a query will run and check if the inserted number is availabe. When the inserted number is available, it will insert some values in the DB. Else, when the number is already been taken, it will show a errormsg. When this happens, my dropdownlist is suddenly empty, see screenshot below!
What I want : When I submit, I still want the error message to be shown whenever the number is already been taken and also to keep my dropdownlist intact.
Hope someone can help!
Code :
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
session_start();
include("/includes/connect.php");
if(isset($_SESSION['logged_in'])) {
$sql="SELECT id,model FROM `ipad-model`";
$stmt = $db->prepare($sql);
$stmt->execute();
$result = $stmt->fetchAll();
if(isset($_POST['btnAdd'])) {
$errMsg = '';
$nr=$_POST['nr'];
$sql2="SELECT * FROM ipad WHERE nr=$nr";
$stmt = $db->prepare($sql2);
$stmt->execute();
$result = $stmt->fetchAll();
if($result == 0) {
$nr=$_POST['nr'];
$model=$_POST['model'];
$serienummer=$_POST['serienummer'];
$capaciteit=$_POST['capaciteit'];
$uptodate=$_POST['uptodate'];
$sql="INSERT INTO ipads(uitgeleend,nr,model,serienummer,capaciteit,uptodate) VALUES ('Nee','$nr','$model','$serienummer','$capaciteit', '$uptodate')";
$result=$db->query($sql);
header("location:index.php");
} else {
$errMsg = 'Nummer is al in gebruik!';
}
}
include("/includes/get_header.php");
?>
<h1 class="page-title">Nieuwe iPad toevoegen</h1>
<ul class="breadcrumb">
<li>Home </li>
<li class="active">Nieuwe iPad</li>
</ul>
</div>
<form id="gegevensForm1" class="col-xs-4" form method="POST" action="ipad-toevoegen.php">
<div class="form-group">
<label>Nr</label>
<input type="text" class="form-control" name="nr" />
<?php
if(isset($errMsg)){
echo '<div style="color:#FF0000;text-align:center;font-size:12px;">'.$errMsg.'</div>';
}
?>
</div>
<div class="form-group">
<label>Model</label>
<select class="form-control" name="model">
<?php foreach($result as $row): ?>
<option value="<?= $row['model']; ?>"><?= $row['model']; ?></option>
<?php endforeach; ?>
</select>
<p class="text-right">Bewerken</p>
</div>
<div class="form-group">
<label>Serienummer</label>
<input type="text" class="form-control" name="serienummer" />
</div>
<div class="form-group">
<label>Capaciteit</label>
<select class="form-control" name="capaciteit">
<option value="16 GB">16 GB</option>
<option value="32 GB">32 GB</option>
<option value="64 GB">64 GB</option>
<option value="128 GB">128 GB</option>
</select>
</div>
<div class="form-group">
<label>Up-To-Date</label>
<select class="form-control" name="uptodate">
<option value="Ja">Ja</option>
<option value="Nee">Nee</option>
</select>
</div>
<button class="btn btn-primary pull-right" name="btnAdd" type="submit"><i class="fa fa-save"></i> Opslaan</button>
<input type="button" name="btnCancel" value="Annuleer" class="btn btn-primary pull-left">
<?php
include('../includes/get_footer.php');
?>
</form>
<?php
} else header("location: index.php")
?>
Screenshots :
Before submit & errormsg :
After Submit & errormsg :
It's because your $result variable is overwritten.
You should use different names for different result sets.
So for example, I'd do something like this:
$sql="SELECT id,model FROM `ipad-model`";
$stmt = $db->prepare($sql);
$stmt->execute();
$ipadModels = $stmt->fetchAll();
And below, in the "view" part, where you render the dropdown:
<?php foreach($ipadModels as $ipadModel): ?>
<option value="<?= $ipadModel['id']; ?>"><?= $ipadModel['model']; ?></option>
<?php endforeach; ?>
Hope this helps!
Radu

Categories