How to validate bootstrap modal using PHP codes? - php

I want to validate every data that the user input in modal. The problem is not working at all, even I put the "required" in the text input same problem. It accept empty string. I don't want to accept empty string and the maximum length that user can input 45. I used bootstrap to create a modal and PHP for server side
<?php require_once('../../private/initialize.php'); ?>
<?php
if($_SERVER['REQUEST_METHOD'] == "POST") {
$validation = new Validation();
$data = [
"category_description" => $_POST['category_description']
];
$validation->validate($data, [
"category_description" => "required|maxlen:45"
]);
$errors = $validation->getErrors();
} else {
$data = [
"category_description" => ""
];
$errors = [
"category_description" => ""
];
}
if (isset($_POST['submit'])) {
if($data) {
echo $data;
}
<div class="modal" id="myModal1" > <!-- start update modal -->
<div class="modal-dialog">
<div class="modal-content " style="height:auto">
<!-- Modal Header -->
<div class="modal-header">
<h4 class="modal-title">Update Category</h4>
<button #click="clearData" type="button" class="close" data-dismiss="modal"><i class="fas fa-times"></i></button>
</div>
<!-- Modal body -->
<form method="post" action="unit_category/index.php">
<div class="modal-body">
<div class="form-group">
<div class="col-lg-12">
<input type="hidden" class="form-control" id="category_id" name="category_id" v-model="category_id" disabled>
</div>
</div>
<div class="form-group">
<div class="col-lg-12">
<input type="text" id="category_description" name="category_description" v-model="category_description" value="<?php echo $data['category_description'] ?>" placeholder="Enter Description" class="form-control <?php if(!empty($errors['category_description'])) { echo 'is-invalid'; } ?>" required>
<div class="invalid-feedback"><?php echo $errors['category_description'] ?></div>
</div>
</div>
</div>
<!-- Modal footer -->
<div class="modal-footer">
<button type="submit" #click="updateCategory" class="btn btn-primary" data-dismiss="modal">Update Category</button>
</div>
</form>
</div>
</div>
</div> <!-- end update modal -->

Related

I want to call php inserting query when i am clicking the bootstrap button

My problem is I am trying to call the PHP file while clicking the button on my bootstrap but it is not working for me here by I paste my code of bootstrap and PHP kindly guide me for the solution
Thanks in advance
I am tired of changing button to form and all of these but not working I am working on PHP 7 and Bootstrap 4
<?php include "includes/db.php"; ?>
<?php
// echo "<h1> Testing </h1>";
// $db = mysql_select_db("cateory", $connection); // Selecting Database from Server
function(){
if (isset($POST['submit'])){
// $connect=mysqli_query($connection,)
$cat_title=$POST['cat_title'];
if($cat_title==""||empty($cat_title)){
echo "The field should not empty";
}
else{
$query="INSERT INTO category (cat_title)";
$query .=" VALUE ('{$cat_title}')";
$create_category_query = mysqli_query ($connection,$query);
if(!$create_category_query){
die ('QUERY FAILED'.mysqli_error($connection));
}
}
}
}
?>```
<!-- ADD CATEGORY MODAL -->
<div class="modal fade" id="addCategoryModal">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header bg-success text-white">
<h5 class="modal-title">Add Category</h5>
<button class="close" data-dismiss="modal">
<span>×</span>
</button>
</div>
<div class="modal-body">
<form action="" method="post">
<div class="form-group">
<label for="title">Title</label>
<input type="text" class="form-control" name="title">
</div>
</div>
<div class="modal-footer">
<?php insert_categories(); ?>
<button class="btn btn-success" type="submit" data-dismiss="modal">Save Changes</button>```
</div>
</form>
</div>
</div>
Do change your code like:
<?php
if ( isset( $_POST['submit']) ) {
// $connect=mysqli_query($connection,)
$cat_title = $_POST['cat_title'];
if($cat_title==""||empty($cat_title)){
echo "The field should not empty";
} else {
$query="INSERT INTO category (cat_title)";
$query .= " VALUE ('{$cat_title}')";
$create_category_query = mysqli_query ($connection,$query);
if(!$create_category_query){
die ('QUERY FAILED'.mysqli_error($connection));
}
}
}
?>
<!-- ADD CATEGORY MODAL -->
<div class="modal fade" id="addCategoryModal">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header bg-success text-white">
<h5 class="modal-title">Add Category</h5>
<button class="close" data-dismiss="modal">
<span>×</span>
</button>
</div>
<div class="modal-body">
<form action="" method="post">
<div class="form-group">
<label for="title">Title</label>
<input type="text" class="form-control" name="cat_title">
</div>
<div class="modal-footer">
<?php insert_categories(); ?>
<button class="btn btn-success" type="submit" data-dismiss="modal">Save Changes</button>```
</div>
</form>
</div>
</div>
</div>
</div>
Problems with your code:
Your HTML formatting itself is wrong. you have broken your form tag with div of modal-body. You have to reconsider your HTML design.
No need to wrap PHP code in a function with no name.
You just have to use the code only.
You have named your input as title while saving/checking data as cat_title.
Check your whole codebase again.

Codeigniter : Form became weird when using select option (form_dropdown) to edit my data

It's work fine when I am inputing data using select option on CI but when I try to edit or update my data again my form became weird like this (See picture edit form)
Here my controller :
public function updateobat ($kode_obat){
if($_POST==null){
$this->load->model('a_model');
$data['hasil'] = $this->a_model->select2($kode_obat);
$data['title'] = "Data Stok Obat | Praktik Dokter Umum";
$data['tb_jenisobat'] = $this->a_model->ambil_jenisobat();
$this->template->admin('admin/edit-obat',$data);
}else{
$this->load->model('a_model');
$this->a_model->update_obat($kode_obat);
$data['title'] = "Data Stok Obat | Praktik Dokter Umum";
$data['obat'] = $this->a_model->ambil_obat();
$this->template->admin('admin/data-obat',$data);
}
}
Model
public function update_obat($kode_obat){
$nama_obat = $this->input->post('nama_obat');
$kode_jenis_obat = $this->input->post('kode_jenis_obat');
$stok= $this->input->post('stok');
$data = array('nama_obat' => $nama_obat,
'kode_jenis_obat' => $kode_jenis_obat,
'stok' => $stok,
);
$this->db->where('kode_obat',$kode_obat);
$this->db->update('tb_obat',$data);
}
public function select2($kode_obat){
return $this->db->get_where('tb_obat', array('kode_obat' => $kode_obat))->row();
}
View
<!-- Main content -->
<section class="content">
<div class="row">
<!-- left column -->
<div class="col-md-8">
<!-- general form elements -->
<div class="box box-primary">
<div class="box-header with-border">
<h3 class="box-title">Silahkan melakukan edit stok obat</h3>
</div>
<!-- /.box-header -->
<!-- form start -->
<form role="form" action="<?php echo base_url().'index.php/a_controller/updateobat/'.$hasil->kode_obat; ?>" method="post">
<div class="box-body">
<div class="form-group">
<label for="exampleInputNama">Nama Obat</label>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-medkit"></i></span>
<input type="text" class="form-control" name="nama_obat" id="exampleInputNama" placeholder="Nama Obat" action="<?php echo form_input('nama_obat',$hasil->nama_obat);?>">
</div>
</div>
<div class="form-group">
<label for="exampleInputJenis">Jenis Obat</label>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-list"></i></span>
<select name="kode_jenis_obat" class="form-control" action="<?php echo form_dropdown('kode_jenis_obat',$hasil->kode_jenis_obat);?>">
<option value="none" selected="selected">Pilih Jenis Obat</option>
<!-----Displaying fetched cities in options using foreach loop ---->
<?php foreach($tb_jenisobat as $jenisobat):?>
<option value="<?php echo $jenisobat->kode_jenis_obat?>"><?php echo $jenisobat->nama_jenis_obat?></option>
<?php endforeach;?>
</select>
</div>
</div>
<div class="form-group">
<label for="exampleInputUsername">Stok Obat</label>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-list"></i></span>
<input type="text" class="form-control" name="stok" id="exampleInputUsername" placeholder="Stok" action="<?php echo form_input('stok',$hasil->stok);?>">
</div>
</div>
</div>
<!-- /.box-body -->
<div class="box-footer">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
</div>
<!-- /.box -->
</div>
</div>
<!-- /.row -->
</section>
I am using form_dropdown for my select option
I think there is something wrong with my controller syntax?
$data['tb_jenisobat'] = $this->a_model->ambil_jenisobat();
But I am not sure enough, can someone help me?
Picture : edit form

How to create Drop down from database in Codeigniter

Sorry, i want the create Form input using drop down from database. but i've try to created and error like this.
My Code in controller
public function ajax_add()
{
$data = array(
'date_man_activity_ra' => $this->input->post('date_man_activity_ra',TRUE),
'dd_user' => $this->mymodel->dd_user(),
'user_selected' => $this->input->post('id_user') ? $this->input->post('id_user') : '',
'id_user' => $this->input->post('id_user',TRUE),
'note' => $this->input->post('note',TRUE),
);
$insert = $this->Man_activity->save($data);
echo json_encode(array("status" => TRUE));
}
and mymodel
public function dd_user()
{
// ambil data dari db
$this->db->order_by('id_user', 'asc');
$result = $this->db->get('ops_user');
// bikin array
// please select berikut ini merupakan tambahan saja agar saat pertama
// diload akan ditampilkan text please select.
$dd[''] = 'Please Select';
if ($result->num_rows() > 0) {
foreach ($result->result() as $row) {
// tentukan value (sebelah kiri) dan labelnya (sebelah kanan)
$dd[$row->id_user] = $row->username;
}
}
return $dd;
}
and my View
<div class="modal fade" id="modal_form" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h3 class="modal-title">Person Form</h3>
</div>
<div class="modal-body form">
<form action="#" id="form" class="form-horizontal">
<input type="hidden" value="" name="id"/>
<div class="form-body">
<div class="form-group">
<label class="control-label col-md-3">date_man_activity_ra</label>
<div class="col-md-9">
<input name="date_man_activity_ra" placeholder="yyyy-mm-dd" class="form-control datepicker" type="text">
<span class="help-block"></span>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">Id User</label>
<div class="col-md-9">
<?php
$dd_user_attribute = 'class="form-control select2"';
echo form_dropdown('id_user', $dd_user, $user_selected, $dd_user_attribute);
?>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">Note</label>
<div class="col-md-9">
<input name="note" placeholder="note" class="form-control" type="text">
<span class="help-block"></span>
</div>
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" id="btnSave" onclick="save()" class="btn btn-primary">Save</button>
<button type="button" class="btn btn-danger" data-dismiss="modal">Cancel</button>
</div>
</div>
</div>
how to solve it? or Another ways?
Thank You
public function ajax_add()
{
$data = array(
'date_man_activity_ra' => $this->input->post('date_man_activity_ra',TRUE),
'dd_user' => $this->mymodel->dd_user(),
'user_selected' => $this->input->post('id_user') ? $this->input->post('id_user') : '',
'id_user' => $this->input->post('id_user',TRUE),
'note' => $this->input->post('note',TRUE),
);
$insert = $this->Man_activity->save($data);
// Load your view and pass the data that you use in dropdown
//echo json_encode(array("status" => TRUE)); // Remove it
}

Reload Bootstrap Modal on PHP variable not valid

Having some problems with the error handling of a bootstrap modal. In the modal I have two inputs (both are required). I want to be able to display a simple "required" message if the form is submitted without one of the fields populated.
I tried doing this with PHP and it works in a page by itself, but when I put it in a modal the modal closes on submit and then if you re-open the modal you see the error messages. I really want the modal to stay open or re-open if the input fields are not valid when the user submits.
Any help would be appreciated! Thanks!
HTML (Start of the modal):
<div class="modal fade" id="createModal" tabindex="-1" role="dialog" aria-labelledby="createModal" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="myModalLabel">Add a vCenter</h4>
</div>
<div class="modal-body">
PHP (To graph the input and report any errors):
if ( !empty($_POST)) {
// keep track validation errors
$vcenternameError = null;
$vcenterownerError = null;
// keep track post values
$vcentername = $_POST['vcentername'];
$vcenterowner = $_POST['vcenterowner'];
// validate input
$valid = true;
if (empty($vcentername)) {
$vcenternameError = 'Please enter vCenter Name';
$valid = false;
}
if (empty($vcenterowner)) {
$vcenterownerError = 'Please select vCenter Owner';
$valid = false;
}
// insert data
if ($valid) {
$sql = "INSERT INTO ";
$stmt = sqlsrv_query( $conn, $sql );
if( $stmt === false) {
die( print_r( sqlsrv_errors(), true) );
}
header("Location: index.php");
}
}
?>
HTML (the rest of the modal):
<div class="panel-body">
<form class="form-horizontal" action="index.php" method="post">
<div class="<?php echo !empty($vcenternameError)?'error':'';?> form-group">
<div class="alert alert-warning">
Please input the FQDN of the vCenter and select an owner.
</div>
<label class="control-label">vCenter Name</label>
<div class="controls">
<input name="vcentername" type="text" placeholder="vCenter Name" value="<?php echo !empty($vcentername)?$vcentername:'';?>" class="form-control">
<?php if (!empty($vcenternameError)): ?>
<div class="alert alert-warning alert-dismissable">
<?php echo $vcenternameError;?>
</div>
<?php endif; ?>
</div>
</div>
<div class="<?php echo !empty($vcenterownerError)?'error':'';?> form-group">
<label class="control-label">vCenter Owner</label>
<div class="controls">
<div class="btn-group" data-toggle="button" role="group" aria-label="...">
<label class="btn btn-default">
<input type="radio" name="vcenterowner" value="Team1"> Team1
</label>
<label class="btn btn-default">
<input type="radio" name="vcenterowner" value="Team2"> Team2
</label>
<label class="btn btn-default">
<input type="radio" name="vcenterowner" value="Team3"> Team3
</label>
</div>
<?php if (!empty($vcenterownerError)): ?>
<div class="alert alert-warning alert-dismissable">
<?php echo $vcenterownerError;?>
</div>
<?php endif; ?>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<div class="form-actions">
<button type="submit" class="btn btn-success">Add</button>
<a class="btn btn-default" href="index.php">Back</a>
</div>
</form>
</div>
</div>
</div>
</div>
You can use jQuery to open the modal again if the modal contains errors when the page loads:
var modal = $('#createModal');
if(modal.find('div.alert-warning').length)
modal.modal();
But for the best user experience, you should call your PHP script with an ajax request: http://api.jquery.com/jquery.ajax/

Bootstrap include php inside modal

So I am trying to insert a php script that gets values for a select element into a modal. But The php script works and gets the right info. But whenever i try and load the script into the modal like so:
<?php include 'script.php' ?>
My modal wont show up. These are my files:
Index.php
<div class="modal fade" id="upload" tabindex="-1">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Tilføj en note</h4>
</div>
<form action="scripts/upload.php" method="post" enctype="multipart/form-data">
<div class="modal-body">
<div class="form-group">
<label for="name">Navn:</label>
<input type="text" class="form-control" name="name" placeholder="Navn på note..">
</div>
<div class="form-group">
<label for="name">Fag:</label>
<select class="form-control">
<?php include 'scripts/getClasses.php'; ?>
</select>
</div>
<div class="form-group">
<label for="name">Beskrivelse:</label>
<textarea name="description" data-provide="markdown" rows="10"></textarea>
</div>
<label for="file">Vælg fil:</label>
<input type="file" name="file" id="file"><br>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Luk</button>
<button type="submit" class="btn btn-success">Upload</button>
</div>
</form>
</div><!-- /.modal-content -->
</div><!-- /.modal -->
scripts/getClasses.php
<?php
require 'funcs.php';
$function = new functions;
$class = $function->getClasses();
$numclasses = count($class);
for($i=0;$i<$numclasses;$i++)
{
$id = $class[$i]['id'];
$name = $class[$i]['name'];
echo "<option value='".$id."'>".$name."</option>";
}
?>
snippet of funcs.php
public function getClasses()
//Function that gets all the classes. Used when user adds a new note.
{
require 'connect.php';
$count = 0;
$q = $db->prepare('SELECT * FROM classes ORDER BY name ASC');
$q->execute();
while($row = $q->fetch())
{
$results[$count]['id'] = $row['id'];
$results[$count]['name'] = $row['name'];
$count++;
}
return $results;
}

Categories