jquery repeater taking last data record when added - php

using for each loop I have to retrieve data however when adding new record data-repeater-create is taking default value as the last record of data even the element id is different.
<div data-repeater-list="car">
<?php foreach ($user_portfolio as $p_details) {
$p_title = $p_details['portfolio_title'];
$p_desc=$p_details['portfolio_desc'];
$p_expert = $p_details['portfolio_expert_id'];
$p_id = $p_details['portfolio_id'];
?>
<div data-repeater-item>
<div class="form row">
<div class="form-group mb-1 col-sm-12 col-md-12">
<label for="email-addr">Title</label>
<br>
<input type="text" name="port_id" value="<?php if(!empty($p_id)) {echo $p_id; } else { echo "";}?>">
<input type="text" class="form-control" id="portfolio_title<?php echo $p_id;?>" placeholder="Title " name="portfolio_title" value="<?php if(!empty($p_title)) {echo $p_title;} else {echo ""; }?>">
</div>```
below using data-repeater-create i am adding empty field however its taking last record as default value and I able to see in view source
``` <?php } //end of foreach?>
</div>
<div class="form-group overflow-hidden">
<div class="col-12">
<button data-repeater-create class="btn btn-theme" type="button">
<i class="ft-plus"></i> Add
</button>
<button type="submit" class="btn btn-theme">
<i class="la la-check-square-o"></i> Save
</button>
</div>
</div>
</form>```

Related

Php form repeater post

I want to make a form using form repeater. As a result of my efforts somehow I could not register in the database.
html code:
<div class="m-portlet__body" id="myRadioGroup">
<div id="m_repeater_1">
<div class="form-group m-form__group row" id="m_repeater_1">
<div data-repeater-list="" class="col-md-12">
<div data-repeater-item class="form-group m-form__group row align-items-center">
<div class="col-md-12 m-form__group-sub">
<label class="form-control-label">Car plate</label>
<div class="input-group">
<input type="text" class="form-control" name="plate" placeholder="34 LAA 34" maxlength="10">
<div class="input-group-append">
<button data-repeater-delete="" class="btn btn-primary" type="button"><i class="la la-trash-o"></i></button>
</div>
</div>
</div>
</div>
</div>
<div class="m-form__group form-group row" style="padding-left: 48px; margin-top: -2rem;">
<div class="col-md-12">
<div data-repeater-create="" class="btn btn btn-sm btn-brand m-btn m-btn--icon m-btn--wide">
<span>
<i class="la la-plus"></i>
<span>add plate</span>
</span>
</div>
</div>
</div>
</div>
</div>
chrome looks like this in developer tools:
How should my database registration file be?
$plate = $_POST['plate'];
$sql = $db->prepare('INSERT INTO orders (plate) VALUES (?)');
$save = $sql->execute(array(
$plate,
));
The problem is in the html code, input name should be plate[1]. One possible cause is that you have id="m_repeater_1" declared twice. Make sure you are setting the template correctly first, and then you can search in all post data by using print_r($_POST); (with your current code no POST is being sent).
<input type="number" placeholder="Amount" name="repeat[0][amount]" class="form-control">
public function add()
{
$locations = $_POST['repeat'];
foreach ($locations as $key => $subValue) {
echo $subValue['amount'];
}

EDIT DATA in TABLE using php

**I want to edit data in my table using modal but i can only call ID=1 i cant edit the others and also i need to refresh my web before i can see the changes?
// my update code
<?php
$id= "1";
$mysqli = new mysqli('127.0.0.1','root','','books');
$query = $mysqli->query("select * from bookrecords where ID='$id' ");
$row = $query->fetch_assoc();
if(isset($_POST['update'])) {
$id = $_POST['id'];
$nm = $_POST['nm'];
$is = $_POST['is'];
$pb = $_POST['pb'];
$au = $_POST['au'];
$result = $mysqli->query("UPDATE bookrecords set BookName='$nm', ISBN='$is', Publisher='$pb', Author='$au' where ID='$id'");
if($result){
?>
<div class="alert-success" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="close"><span aria-hidden="true">&times</span></button>
<strong>Success!</strong> your data has been updated.
</div>
<?php
} else{
?>
<div class="alert-failed" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="close"><span aria-hidden="true">&times</span></button>
<strong>Failed!</strong> Error updating. Try Again!
</div>
<?php
}
}
?>
** and this is my Modal form....
<div class="form-group">
<label for="id">ID:</label>
<input type="text" class="form-control" id="id" name="id" value="<?php echo $row['ID']?>" readonly>
</div>
<div class="form-group">
<label for="nm">BookName:</label>
<input type="text" class="form-control" id="nm" name="nm" value="<?php echo $row['BookName']?>">
</div>
<div class="form-group">
<label for="is">ISBN:</label>
<input type="text" class="form-control" id="is" name="is" value="<?php echo $row['ISBN']?>">
</div>
<div class="form-group">
<label for="pb">Publisher:</label>
<input type="text" class="form-control" id="pb" name="pb" value="<?php echo $row['Publisher']?>">
</div>
<div class="form-group">
<label for="au">Author:</label>
<input type="text" class="form-control" id="au" name="au" value="<?php echo $row['Author']?>">
</div>
<button type="submit" name="update" class="btn-primary">Update</button>
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</form>
</div>
<div class="modal-footer">
</div>
</div>
**so how can i call any of my item/data in table? Please help me.
Your HTML form must have a hidden field with the id of book record, which will be send to the PHP script when pressing the "Update" button. The hidden field should look like this:
<input type="hidden" name="bookrecordId" value="ID_OF_BOOKRECORD_HERE" />
You can access the value of the transfered id with $_POST['bookrecordId'] in your receiving PHP script. Use it to build one or two prepared statements which will check if the row exists and then run an UPDATE query to update the values.

Dropzone sending all queue of uploaded files to the controller once

I am trying to send multiple files in one request using DropZone js.
<script type="text/javascript">
Dropzone.autoDiscover = false;
var file= new Dropzone(".dropzone",{
url: any url,
method:"post",
paramName:"PhotoFiles",
addRemoveLinks:true,
autoProcessQueue: false
});
//Upload file onsubmit
$('#UploadMultiFiles').submit(function(){
file.processQueue();
});
file.on("sending",function(a,b,c){
a.token=Math.random();
c.append("token",a.token);
});
The issue is I have other inputs in the same form that is why I used onsubmit, and these inputs are saved in a table and the images from dropzone are saved in another table with and both tables have one to many relationship
The Controller like:
public function university_offers($id){
$data['university_offer_name']=$this->input->post('university_offer_name');
$data['university_id_fk']=$this->input->post('university_id_fk');
$data['university_offer_details']=$this->input->post('university_offer_details');
if ($this->input->post('save')){
$data2['university_offer_id_fk'] = insertrecords('university_offers',$data);
$data2['university_id_fk'] = $data['university_id_fk'];
//when I put the next IF at the begining of the function it works but ofcourse without the other data fields that I need.
if(isset($_FILES) && $_FILES != null){
$data2['photo_name'] = upload_image('PhotoFiles');
insertrecords('university_offer_photos',$data2);
}
if (insertrecords('university_offer_photos',$data2)==true)
message('success','');
redirect('Admin/university_offers/0','refresh');
}
$data['view']="admin/universities/university_offers";
$this->load->view('index',$data);
}
What I need is sending al uploaded files and do a loop in the controller to save it right in the database, not sending each file to the controller and saving it.
Edit:
My view is like:
<form action="university_offers/<?=$id?>" id="UploadMultiFiles" autocomplete="off" method="post" enctype="multipart/form-data" class="m-t-5" novalidate>
<div class="form-body">
<h3 class="card-title">بيانات العرض</h3>
<hr>
<div class="row p-t-20">
<div class="col-md-6">
<div class="form-group">
<label class="control-label">إسم العرض<span class="text-danger">*</span></label>
<div class="controls">
<input type="text" autocomplete="off" name="university_offer_name" class="form-control" required value="<?php if(isset($result)) echo $result['university_offer_name']?>" data-validation-required-message="يجب إدخال إسم العرض" placeholder="إسم العرض">
</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label class="control-label">إسم الجامعة<span class="text-danger">*</span></label>
<div class="controls">
<select name="university_id_fk" id="select" class="form-control" required data-validation-required-message="يجب إختيار الجامعة" aria-invalid="false">
<option value="">إختر الجامعة</option>
<?php
foreach (selectrecords("*",'universities') as $university):
$select = '';
if(isset($result) && $result['university_id_fk'] == $university->university_id_pk)
$select = 'selected';
?>
<option <?=$select?> value="<?php echo $university->university_id_pk ?>"><?php echo $university->university_name ?></option>
<?php endforeach;?>
</select>
</div>
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<label class="control-label">تفاصيل العرض<span class="text-danger">*</span></label>
<div class="controls">
<textarea id="mymce" name="university_offer_details"><?php if(isset($result)) echo $result['university_offer_details'] ?></textarea>
</div>
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<label class="control-label">صور العرض<span class="text-danger">*</span></label>
<div class="row">
<?php
if(isset($result)) {
foreach (selectrecords('*','university_offer_photos',array('university_offer_id_fk'=>$result['university_offer_id_pk'])) as $photo):
echo'
<div class="form-group text-center" id="'.$photo->photo_id_pk.'">
<div class="col-md-12">
<img style="border: none; height: 150px; width: 150px; border-radius: 10px;" class="img-thumbnail" src="'.base_url().'public/uploads/images/'.$photo->photo_name.'">
</div>
حذف الملف
</div>
';
endforeach;
}
?>
</div>
<div class="dropzone">
<div class="dz-message">
<span> Drag and Drop your files here Or Click here to upload</span>
</div>
</div>
</div>
</div>
</div>
<div class="form-actions">
<input type="hidden" id="url" value="university_offers/<?=$id?>" />
<?php
if($id == 0)
echo '<button type="submit" id="butt" name="save" value="save" class="btn btn-primary"> <i class="fa fa-check"></i> حفظ</button>';
else
echo '<button type="submit" id="butt" name="edit" value="edit" class="btn btn-primary"><i class="fa fa-check"></i> حفظ</button>';
?>
<button type="reset" class="btn btn-inverse"><i class="fa fa-times"></i> إلغاء</button>
<?php
if($id == 0)
echo'<button type="button" id="backword" class="btn btn-second"><i class="fa fa-undo"></i> رجوع</button>';
else{
echo '<input type="hidden" name="university_offer_id_pk" value="'.$result['university_offer_id_pk'].'"/>
<button type="button" class="btn btn-second"><i class="fa fa-undo"></i> رجوع</button>';
}
?>
</div>
</div>
</form>
upload_image function, I put it in the Helper
function upload_image($file_name){
$CI =& get_instance();
$config['upload_path'] = 'public/uploads/images';
$config['allowed_types'] = 'gif|Gif|ico|ICO|jpg|JPG|jpeg|JPEG|BNG|png|PNG|bmp|BMP|WMV|wmv|MP3|mp3|FLV|flv|SWF|swf';
$config['max_size'] = '1024*8';
$config['encrypt_name']=true;
$CI->load->library('upload',$config);
if(! $CI->upload->do_upload($file_name)){
return false;
}else{
$datafile = $CI->upload->data();
thumb($datafile);
watermark($datafile);
return $datafile['file_name'];
}}

HTML form not passing checkbox value trough $_POST in php file

I have a form in HTML with some checkboxes and I want to send an email with the selected checkboxes in the body, the problem is that the "text" fields are passing to the PHP variables, but the checkboxes values are always set to uncheck when I test it on the PHP file:
HTML:
<div class="col-md-6">
<div class="c-checkbox-list">
<div class="c-checkbox">
<input type="checkbox" name="checkbox1citadina" id="checkbox1citadina" value="checkbox1citadina" class="form-control c-check" >
<label for="checkbox1citadina">
<span></span>
<span class="check"></span>
<span class="box"></span> Citadina
</label>
</div>
PHP:
$reservas_bicicletas = '';
if (isset($_POST['checkbox1citadina'])) {
$reservas_bicicletas .= "Citadina: checked\n";
} else {
$reservas_bicicletas .= "Citadina: unchecked\n";
}
echo $reservas_bicicletas;
$reservas_bicicletas always retrieves the else value.
Need help guys, thanks in advance.
remove c-check
<div class="col-md-6">
<div class="c-checkbox-list">
<div class="c-checkbox">
<input type="checkbox" name="checkbox1citadina" id="checkbox1citadina" value="checkbox1citadina" class="form-control " >
<label for="checkbox1citadina">
<span></span>
<span class="check"></span>
<span class="box"></span> Citadina</label>
</div>
Basically i have this (i shorted out the form because there are too many elements):
HTML:
<form name="quick_booking" id="quick_booking" method="post" action="assets/reserva-bicicletas.php">
<div class="col-md-6">
<div class="c-checkbox-list">
<div class="c-checkbox">
<input type="checkbox" name="checkbox1citadina" id="checkbox1citadina" value="checkbox1citadina" class="form-control " >
<label for="checkbox1citadina">
<span></span>
<span class="check"></span>
<span class="box"></span> Citadina</label>
</div>
<button type="submit" class="btn c-theme-btn c-btn-uppercase btn-lg c-btn-bold c-btn-square">Reservar</button>
</form>

If value already exists in array then issue an error in PHP

I have a code wherein it checks if a value already exists in the array. Basically, what the program does is that it first stores all the values in the array. Then it will be checked using count(array_keys) function. There are three inputs. If in those 3 inputs, a value occurs twice or thrice, it will issue an error. Now, my problem is that if INPUT A AND INPUT B IS THE SAME BUT INPUT C is different, it will still add to the database, BUT IF INPUT A AND C IS THE SAME BUT INPUT B is different then it will not add (which is correct).
Here is my php code:
<?php
include 'config.php';
if(isset($_POST['update_actual_lupong'])){
$id = isset($_GET['id'])? $_GET['id'] : "";
$id_hearing = $_POST['hearing_lup'];
$lupong = $_POST['act_lupong'];
$actual = array();
foreach($lupong as $aaa) {
$actual[] = $aaa;
}
if ((count(array_keys($actual, $aaa)) > 1)) {
echo '<br><br>this array contains DUPLICATE <br><br>';
}
else {
foreach ($lupong as $lup) {
$updateMemo = mysqli_query($conn, "INSERT INTO actuallupong(Hearing_idHearing, bar_pos2) VALUES ('$id_hearing', (SELECT idtable_position FROM table_position WHERE Person_idPerson = '$lup'));");
}
echo "ADDED ggg";
}
//header ('location: view_case_profile.php?id='.$id);
mysqli_close($conn);
}
?>
HTML code (it's in a modal):
<div class="modal fade bs-example-modal-lg" id="modal_lupong" data-backdrop="static" data-keyboard="false" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" >
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">
<span aria-hidden="true">×</span>
<span class="sr-only">Close</span>
</button>
<h4 class="modal-title" id="myModalLabel">Update Lupong</h4>
</div>
<form id="update_actual_lupong" class="form-horizontal form-label-left calender" name = "update_actual_lupong" enctype="multipart/form-data" method="post" role="form" novalidate>
<div class="modal-body">
<div class="d item form-group">
<label class="col-sm-3 control-label">Hearing Number</label>
<div class="col-sm-7">
<input type="number" class="form-control" id="hearing_lup" name="hearing_lup" readonly="readonly"/>
</div>
</div>
<div class="f item form-group" id = "act1">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Lupong 1 <span class="required">*</span></label>
<div class="col-sm-7">
<input name="actlupong[]" id="search_keyword_id_act" class="search_keyword_act form-control col-md-7 col-xs-12" required="required" placeholder ="Search first name or last name... " type="text">
<input type="hidden" name="act_lupong[]" id="act_lup1"/>
<div id="result3"></div>
</div>
</div>
<div class="f item form-group" id = "act2">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Lupong 2 <span class="required">*</span></label>
<div class="col-sm-7">
<input name="actlupong[]" id="search_keyword_id_act1" class="search_keyword_act1 form-control col-md-7 col-xs-12" required="required" placeholder ="Search first name or last name... " type="text">
<input type="hidden" name="act_lupong[]" id="act_lup2"/>
<div id="result4"></div>
</div>
</div>
<div class="f item form-group" id = "act3">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Lupong 3 <span class="required">*</span></label>
<div class="col-sm-7">
<input name="actlupong[]" id="search_keyword_id_act2" class="search_keyword_act2 form-control col-md-7 col-xs-12" required="required" placeholder ="Search first name or last name... " type="text">
<input type="hidden" name="act_lupong[]" id="act_lup3"/>
<div id="result5"></div>
</div>
</div>
</div>
<div class="modal-footer" style="margin:0;">
<button type="button" class="btn btn-default" data-dismiss="modal" style="margin-top: 4px;">Close</button>
<button id="send" type="submit" class="btn btn-success" name="update_actual_lupong">Save Record</button>
</div>
</form>
</div>
</div>
</div>
Screenshot:
What seems to be wrong in my code? Which part should I change? Your help will be much appreciated. Thank you.
As you said:- There are three inputs. If in those 3 inputs, a value occurs twice or thrice, it will issue an error.
A bit modification to your code needed:-
<?php
include 'config.php';
if(isset($_POST['update_actual_lupong'])){
$id = isset($_GET['id'])? $_GET['id'] : "";
$id_hearing = $_POST['hearing_lup'];
$lupong = $_POST['act_lupong'];
if (count(array_unique($lupong)) < count($lupong))) { // check that count of unique $lupong and original $lupong is equal or not if not then $lupong have duplicate values
echo '<br><br>this array contains DUPLICATE <br><br>';
}
else {
foreach ($lupong as $lup) {
$updateMemo = mysqli_query($conn, "INSERT INTO actuallupong(Hearing_idHearing, bar_pos2) VALUES ('$id_hearing', (SELECT idtable_position FROM table_position WHERE Person_idPerson = '$lup'));");
}
echo "ADDED ggg";
}
//header ('location: view_case_profile.php?id='.$id);
mysqli_close($conn);
}
?>
You exit the loop with $aaa as the value of c so only check this value for duplication.
You should check for duplicates inside the loop and set a variable i.e.
$dup = false;
foreach($lupong as $aaa) {
$actual[] = $aaa;
if ((count(array_keys($actual, $aaa)) > 1)) {
$dup = true;
}
}
if ($dup) {
echo '<br><br>this array contains DUPLICATE <br><br>';
}
else {
foreach ($lupong as $lup) {
$updateMemo = mysqli_query($conn, "INSERT INTO actuallupong(Hearing_idHearing, bar_pos2) VALUES ('$id_hearing', (SELECT idtable_position FROM table_position WHERE Person_idPerson = '$lup'));");
}
echo "ADDED ggg";
}

Categories