PHP Controller function instantiated before it's called - php

I'm having a problem with a CRUD application form being displayed on a bootstrap modal.
The issue happens when the form's edit button send the "editId" post var, the modal shows up but the values for the options of the select tag are displayed outside the form.
My controller looks like this:
public function editarUsuarioController(){
if (isset($_GET["editId"])) {
$dataController = $_GET["editId"];
$data = Datos::editUserModel($dataController, "users");
echo'<div id="editModal">
<form method="post" role="form">
<div class="form-group">
<label for="roleEdit">Rol<span></span></label>
<div class="input-group">
<span class="input-group-addon">
<i class="fa fa-user"></i>
</span>
<select name="roleTypes" class="form-control">
<option selected>
'.$data["rols"].'
</option>
'.$editOptions = MainController::viewRolesController().'
</select>
</div>
</div>
<div class="form-actions">
<input type="submit" class="btn blue" value="Update">
<button type="button" data-dismiss="modal" class="btn default">Cancel</button>
</div>
</form>
</div>';
} }
As you can see, I'm instantiating another controller from another file which lists the available roles which looks like:
public function viewRolesController(){
$response= Datos::vistaRolesModel("roles");
foreach($response as $row => $item){
echo'<option value="'.$item["id"].'">'.$item["role"].'</option>';
}
}
Any suggestions?

I fixed it by splitting up the echo and using a variable inside the original controller to store the values brought by the vistaRolesModel() model function so this is the new editarUsuarioController() controller function:
public function editarUsuarioController(){
if (isset($_GET["editId"])) {
$dataController = $_GET["editId"];
$data = Datos::editUserModel($dataController, "users");
$roles = Datos::vistaRolesModel("roles");
echo'
<div id="editModal">
<form method="post" role="form">
<div class="form-group">
<label for="roleEdit">Rol<span></span></label>
<div class="input-group">
<span class="input-group-addon">
<i class="fa fa-user"></i>
</span>
<select name="roleTypes" class="form-control">
<option selected>
'.$data["rols"].'
</option>
';
foreach($roles as $row => $item){
echo'<option value="'.$item["id"].'">'.$item["role"].'</option>';
}
echo'
</select>
</div>
</div>
<div class="form-actions">
<input type="submit" class="btn blue" value="Update">
<button type="button" data-dismiss="modal" class="btn default">Cancel</button>
</div>
</form>
</div>
';
}
}

Related

jquery repeater taking last data record when added

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>```

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'];
}

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'];
}}

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.

Setting an active button after a POST operation using PHP and bootstrap

I'd appreciate your help with this!
Here is my bootstrap HTML code:
<div class="form-group centered">
<input type="submit" class="btn btn-primary btn-block" name="login" value="Log In"></input>
</div>
<div class="centered">
<div class="form-group btn-group" data-toggle="buttons">
<label class="btn btn-default active">
<input type="radio" id="app" value="app" name="dashboard"/>Application
</label>
<label class="btn btn-default">
<input type="radio" id="setup" value="setup" name="dashboard"/>Setup
</label>
</div>
</div>
I would like to add some logic that after the "Log In" button is selected, the "active" class on the label is retained properly.
Example:
Setup is selected
The user clicks login
The password is wrong so the user is thrown back to the same log in form
This time the form has the setup button "active" automatically
I tried playing around with the following within the app input type:
<?php if($_POST['dashboard'] == "app") { echo checked="\"checked\""; } ?>
I now realize, however, that this button group does not work like a regular radio button, and I probably need to toggle the class on the labels somehow.
Thanks in advance for your help!
I got it working piggybacking on the advice of user1844933 :)
<div class="form-group centered">
<input type="submit" class="btn btn-primary btn-block" name="login" value="Log In"></input>
</div>
<div class="centered">
<div class="form-group btn-group" data-toggle="buttons">
<label class="btn btn-default <?php setAppDefault( $lastDashboardSetting ); ?>">
<input type="radio" value="app" name="dashboard"/>Application
</label>
<label class="btn btn-default <?php setSetupDefault( $lastDashboardSetting ); ?>">
<input type="radio" value="setup" name="dashboard"/>Setup
</label>
</div>
</div>
My PHP code:
$lastDashboardSetting = $_POST['dashboard'];
function setAppDefault( $setting ) {
if( $setting === "app" )
echo "active";
else
echo "";
}
function setSetupDefault( $setting ) {
if( $setting === "setup" )
echo "active";
else
echo "";
}
try this
php
if($_POST['dashboard'] == "app" { $appchk='checked'; } else {$appchk='';}
if($_POST['dashboard'] == "setup" { $setupchk='checked'; } else {$setupchk='';}
HTML
<input type="radio" id="app" value="app" name="dashboard" <?php echo($appchk);?>/>Application
<input type="radio" id="setup" value="setup" name="dashboard" <?php echo($setupchk);?> />Setup

Categories