required attribute form input in ajax not working - php

attribute required not working when I submitted data with Ajax if I change on click with submitting data not insert, just success insert with on click, this my code. this is in form wizard #jquery #Codeigniter
<form action="#">
<div class="form-group row">
<div class="col-sm-7">
<h3 class="wizard-title">Data Surat</h3>
</div>
</div>
<div class="form-group row">
<label class="col-12 col-sm-3 col-form-label text-left text-sm-right">Nomor</label>
<div class="col-12 col-sm-8 col-lg-6">
<input class="form-control" type="text" required placeholder="Nomor Surat" id="nomor" name="nomor">
</div>
</div>
<div class="form-group row">
<label class="col-12 col-sm-3 col-form-label text-left text-sm-right">Tanggal</label>
<div class="col-12 col-sm-8 col-lg-6">
<input class="form-control" type="date" required name="tanggal" id="tanggal" placeholder="Tanggal">
</div>
</div>
<div class="form-group row">
<label class="col-12 col-sm-3 col-form-label text-left text-sm-right">Tujuan</label>
<div class="col-12 col-sm-8 col-lg-6">
<Textarea class="form-control" required name="tujuan" id="tujuan" ></Textarea>
</div>
</div>
<div class="form-group row">
<div class="offset-sm-2 col-sm-10">
<!-- <input type="button" onclick="myFunction()" class="btn btn-secondary btn-space" value="Reset"> -->
<button class="btn btn-primary btn-space wizard-next" id="btn_save" type="submit" data-wizard="#wizard1">Selanjutnya</button>
</div>
</div>
</form>
this is my script ajax
<script type="text/javascript">
$(document).ready(function() {
//Save product
$('#btn_save').on('click', function(e) {
e.preventDefault();
if(! $form.valid()) return false;
var $form = $(this);
var nomor = $('#nomor').val();
var tanggal = $('#tanggal').val();
var tujuan = $('#tujuan').val();
$.ajax({
type: "POST",
url: "<?php echo site_url('users/bendahara/sp/save')?>",
dataType: "JSON",
data: {
nomor: nomor,
tanggal: tanggal,
tujuan: tujuan
},
success: function(data) {
$('[name="nomor"]').val("");
$('[name="tanggal"]').val("");
$('[name="tujuan"]').val("");
// show_product();
}
});
return false;
});
});
</script>
Maybe you know
required is working

Related

data never send to php file with axios

hello i have a problem with sending data
i have a form that send data to a php file to insert it in data base.
signup.js
import { useState } from "react";
import axios from "axios";
const Signup =()=>{
const [cin,setCIN]=useState()
const [name,setName]=useState()
const [lastname,setLastname]=useState()
const [adress,setAdress]=useState()
const [tele,setTele]=useState()
const [email,setEmail]=useState()
const [pass,setPass]=useState()
const [re_pass,setRepass]=useState()
function Valide(e){
e.preventDefault();
if(pass===re_pass){
let data=new FormData()
data.append("cin",cin)
data.append("nom",name)
data.append("prenom",lastname)
data.append("email",email)
data.append("tele",tele)
data.append("adress",adress)
data.append("pass_word",pass)
axios({
method: 'post',
url:'http://localhost/projet_stage/src/PHP/register.php',
data: data,
config: { headers: { 'Content-Type': 'multipart/form-data' } }
})
.then(function (response) {
//handle success
console.log(response)
})
.catch(function (response) {
//handle error
console.log(response)
});
} else{
alert("Password mismatch")
}
}
function handleCIN(e){
setCIN(e.target.value)
}
function handleName(e){
setName(e.target.value)
}
function handleLastname(e){
setLastname(e.target.value)
}
function handleEmail(e){
setEmail(e.target.value)
}
function handlePass(e){
setPass(e.target.value)
}
function handleRepass(e){
setRepass(e.target.value)
}
function handleAdress(e){
setAdress(e.target.value)
}
function handleTele(e){
setTele(e.target.value)
}
return (
<section class="vh-100 bg-image"
style={{backgroundImage: "url('https://mdbcdn.b-cdn.net/img/Photos/new-templates/search-box/img4.webp');"}}>
<div class="mask d-flex align-items-center h-100 gradient-custom-3">
<div class="container h-100">
<div class="row d-flex justify-content-center align-items-center h-100">
<div class="col-12 col-md-9 col-lg-7 col-xl-6">
<div class="card" style={{borderRadius: "15px"}}>
<div class="card-body p-5">
<h2 class="text-uppercase text-center mb-5">Create an account</h2>
<div class="form-outline mb-4">
<input value={cin} name={"cin"} onChange={(e)=>handleCIN(e)} type="text" id="form3Example1ck" class="form-control form-control-lg" />
<label class="form-label" >cin</label>
</div>
<div class="form-outline mb-4">
<input value={name} name={"nom"} onChange={(e)=>handleName(e)} type="text" id="form3Example1cg" class="form-control form-control-lg" />
<label class="form-label" >nom</label>
</div>
<div class="form-outline mb-4">
<input value={lastname} name={"prenom"} onChange={(e)=>handleLastname(e)} type="text" id="form3Example2cg" class="form-control form-control-lg" />
<label class="form-label" >prenom</label>
</div>
<div class="form-outline mb-4">
<input value={adress} name={"adress"} onChange={(e)=>handleAdress(e)} type="text" id="form3Example2cm" class="form-control form-control-lg" />
<label class="form-label" >adress</label>
</div>
<div class="form-outline mb-4">
<input value={tele} name={"tele"} onChange={(e)=>handleTele(e)} type="text" id="form3Example2cl" class="form-control form-control-lg" />
<label class="form-label" >téle</label>
</div>
<div class="form-outline mb-4">
<input value={email} name={"email"} onChange={(e)=>handleEmail(e)} type="email" id="form3Example3cg" class="form-control form-control-lg" />
<label class="form-label">Your Email</label>
</div>
<div class="form-outline mb-4">
<input value={pass} name={"password"} onChange={(e)=>handlePass(e)} type="password" id="form3Example4cg" class="form-control form-control-lg" />
<label class="form-label" >Password</label>
</div>
<div class="form-outline mb-4">
<input value={re_pass} name={"re_password"} onChange={(e)=>handleRepass(e)} type="password" id="form3Example4cdg" class="form-control form-control-lg" />
<label class="form-label" >Repeat your password</label>
</div>
<div class="form-check d-flex justify-content-center mb-5">
<input class="form-check-input me-2" type="checkbox" value="" id="form2Example3cg" />
<label class="form-check-label" for="form2Example3g">
I agree all statements in <u>Terms of service</u>
</label>
</div>
<div class="d-flex justify-content-center">
<button type="button" name={"valide"} onClick={(e)=>Valide(e)}
class="btn btn-success btn-block btn-lg gradient-custom-4 text-body">Register</button>
</div>
<p class="text-center text-muted mt-5 mb-0">Have already an account? <a href="#!"
class="fw-bold text-body"><u>Login here</u></a></p>
</div>
</div>
</div>
</div>
</div>
</div>
</section>)
}
export default Signup;
when i submit the form the the php file insert a user with empty values that mean every think work but the data never send to php file .
register.php
require './server/connect.php';
$method = $_SERVER['REQUEST_METHOD'];
$request = explode('/', trim($_SERVER['PATH_INFO'], '/'));
if(isset($_POST["valide"])){
$cin = $_POST["cin"];
$name = $_POST["nom"];
$prenom = $_POST["prenom"];
$adres = $_POST["adress"];
$tele = $_POST["tele"];
$email = $_POST["email"];
$password = $_POST["password"];
}
$sql = "INSERT INTO user
(cin,nom,prenom,email,télé,
adress,pass_word,statut)
VALUES ('$cin','$name','$prenom','$email','$tele',
'$adres','$password','guest')";
if($db->query($sql)){
echo "workd";
}
?>
when show the data in the console i find a empty object i dont know whay???
CONSOLE.LOG:
also:

Post option value to mysql via ajax wont work

i have been working on some project and getting some experience on php and ajax, jquery. So i have checked 20+ question on stackoverflow but couldnt find the solution even if its already answered the same question.
I have tryed 10 diffrent solution but i guess im skipping something.
I have simple select box and im using ajax to send mysql but only select box doesnt work for some reason.
HTML Code:
<section class="resume-section p-3 p-lg-5 d-flex flex-column" id="rezervasyon">
<div class="my-auto">
<h2 class="mb-5">Online Rezervasyon</h2>
<form id="loginForm" method="" action="" novalidate="novalidate">
<div class="form-group row">
<label for="rez_ad" class="col-sm-2 col-form-label">İsim Soyisim</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="rez_ad" id="rez_ad" placeholder="İsim Soyisim">
</div>
</div>
<div class="form-group row">
<label for="rez_saat" class="col-sm-2 col-form-label">Rezervasyon Saati</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="rez_saat" id="rez_saat" placeholder="Rezervasyon saati">
</div>
</div>
<div class="form-group row">
<label for="rez_gsm" class="col-sm-2 col-form-label">Cep Numaranız</label>
<div class="col-sm-10">
<input type="number" class="form-control" name="rez_gsm" id="rez_gsm" placeholder="İletişim numaranız.">
</div>
</div>
<div class="form-group row">
<label for="rez_tarih" class="col-sm-2 col-form-label">Tarih</label>
<div class="col-sm-10">
<input type="date" class="form-control" name="rez_tarih" id="rez_tarih" placeholder="Tarih belirtiniz.">
</div>
</div>
<div class="form-group row">
<label for="rez_email" class="col-sm-2 col-form-label">Email</label>
<div class="col-sm-10">
<input type="email" class="form-control" name="rez_email" id="rez_email" placeholder="Email">
</div>
</div>
<div class="form-group row">
<label for="rez_tip" class="col-sm-2 col-form-label" name="rez_tip" id="rez_tip">Rezervasyon Tipi</label>
<div class="col-sm-10">
<select id="rez_tip" class="custom-select">
<option value="" selected>Rezervasyon Tipi Seçiniz</option>
<option value="1">Cuma Fix Menü</option>
<option value="2">Cumartesi Fix Menü</option>
<option value="3">Haftaiçi ALKOLSÜZ</option>
<option value="4">Haftaiçi ALKOLLÜ</option>
<option value="5">Kutlama / Doğum Günü</option>
<option value="6">Diğer</option>
</select>
</div>
</div>
<div class="form-group row">
<label for="rez_sayi" class="col-sm-2 col-form-label" id="rez_sayi" name="rez_sayi">Kişi Sayısı</label>
<div class="col-sm-10">
<select id="rez_sayi" class="custom-select">
<option value="" selected>Kişi Sayısı Belirtiniz</option>
<option value="1">2 Kişilik Rezervasyon</option>
<option value="2">4 Kişilik Rezervasyon</option>
<option value="3">4+ Kişilik Rezervasyon</option>
<option value="4">6+ Kişilik Rezervasyon</option>
<option value="5">8+ Kişilik Rezervasyon</option>
<option value="6">20+ Kişilik Rezervasyon</option>
</select>
</div>
</div>
<textarea name="rez_aciklama" id="rez_aciklama" class="form-control" rows="6" cols="21" required="required"
placeholder="Ek açıklama girebilirsiniz."></textarea>
<form class="was-validated">
<!-- Button trigger modal -->
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title text-center" id="exampleModalLabel">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="divider"></div>
<div class="right-side">
<button type="button" class="btn btn-danger btn-link">Delete</button>
</div>
</div>
</div>
</div>
</div>
<div class="custom-control custom-checkbox mb-3">
<input type="checkbox" class="custom-control-input" id="customControlValidation1" required>
<label class="custom-control-label" for="customControlValidation1">Rezervasyon Şartlarını Kabul Ediyorum!</label>
<div class="invalid-feedback">Rezervasyon şartlarını okuyunuz!</div>
</div>
<div class="form-group row">
<div class="col-sm-10">
<button type="button" class="btn btn-success" name="insert-data" id="insert-data" onclick="insertData()">Rezervasyonu Tamamla</button>
</div>
</div>
<br>
<p id="message"></p>
</form>
Here is my php file ;
<?php
include('db.php');
$rez_ad=$_POST['rez_ad'];
$rez_saat=$_POST['rez_saat'];
$rez_gsm=$_POST['rez_gsm'];
$rez_tarih=$_POST['rez_tarih'];
$rez_tip=$_POST['rez_tip'];
$rez_sayi=$_POST['rez_sayi'];
$rez_aciklama=$_POST['rez_aciklama'];
$stmt = $DBcon->prepare("INSERT INTO student(rez_ad,rez_saat,rez_gsm,rez_tarih,rez_tip,rez_sayi,rez_aciklama) VALUES(:rez_ad, :rez_saat,:rez_gsm,:rez_tarih,:rez_tip,:rez_sayi,:rez_aciklama)");
$stmt->bindparam(':rez_ad', $rez_ad);
$stmt->bindparam(':rez_saat', $rez_saat);
$stmt->bindparam(':rez_gsm', $rez_gsm);
$stmt->bindparam(':rez_tarih', $rez_tarih);
$stmt->bindparam(':rez_tip', $rez_tip);
$stmt->bindparam(':rez_sayi', $rez_sayi);
$stmt->bindparam(':rez_aciklama', $rez_aciklama);
if($stmt->execute())
{
$res="Data Inserted Successfully:";
echo json_encode($res);
}
else {
$error="Not Inserted,Some Probelm occur.";
echo json_encode($error);
}
?>
Here is my jquery code;
function insertData() {
var rez_ad=$("#rez_ad").val();
var rez_saat=$("#rez_saat").val();
var rez_gsm=$("#rez_gsm").val();
var rez_tarih=$("#rez_tarih").val();
var rez_tip=$("#rez_tip").val();
var rez_sayi=$("#rez_sayi").val();
var rez_aciklama=$("#rez_aciklama").val();
// AJAX code to send data to php file.
$.ajax({
type: "POST",
url: "rez/insert-data.php",
data: {rez_ad:rez_ad,rez_saat:rez_saat,rez_gsm:rez_gsm,rez_tarih:rez_tarih,rez_tip:rez_tip,rez_sayi:rez_sayi,rez_aciklama:rez_aciklama},
dataType: "JSON",
success: function(data) {
$("#message").html(data);
$("p").addClass("alert alert-success");
},
error: function(err) {
alert(err);
}
});
}
Thanks for help !
You should then pass the rez_tip option in ajax call also.
<script type="text/javascript">
function insertData() {
//var rez_tip=$("#rez_tip option:selected").text();
//var rez_sayi=$("#rez_sayi option:selected").text();
var rez_tip=$("select#rez_tip").val();
var rez_sayi=$("select#rez_sayi").val();
// AJAX code to send data to php file.
$.ajax({
type: "POST",
url: "rez/insert-data.php",
data: {rez_sayi:rez_sayi,rez_tip:rez_tip},
dataType: "JSON",
success: function(data) {
$("#message").html(data);
$("p").addClass("alert alert-success");
},
error: function(err) {
alert(err);
}
});
}
</script>

Ajax form submit not working in PHP

When im doing a ajax call to the update.php from profiles.php it go a blank page update.php but it does make the changes if i go back to the profile.php
idk what im doing wrong what im missing??
profile.php
<?php include_once("includes/config.php");
if (isset($_GET['id']))
{
$Nome = $_GET['id'];
$sql = "SELECT * FROM utilizadores WHERE id='$Nome'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
if($row = $result->fetch_assoc()) {
echo'
<br>
<form action="update.php" method="post" id="go" class="go" accept-charset="utf-8">
<div class="content" >
<div class="container-fluid">
div class="row">
<div class="col-md-8">
<div class="card">
<div class="header">
<h4 class="title">Perfil do paciente</h4>
</div>
<div class="content">
<div class="row">
<div class="col-md-4">
<div class="form-group">
<label>Nome</label>
<input type="text" class="form-control" disabled placeholder="Nome" value="'.$row["Nome"].'">
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label>Apelido</label>
input type="text" class="form-control" disabled placeholder="Apelido" value="'.$row["Apelido"].'">
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label for="exampleInputEmail1">Email</label>
<input type="email" class="form-control" name="Email "disabled placeholder="Email" value="'.$row["Email"].'">
</div>
</div>
</div>
<div class="row">
<div class="col-md-4">
<div class="form-group">
<label>Idade</label>
<input type="text" class="form-control" disabled placeholder="Idade" value="'.$row["Idade"].'">
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label>Sexo</label>
<input type="text" class="form-control" disabled placeholder="Sexo" value="'.$row["Sexo"].'">
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label>Telefone</label>
<input type="text" class="form-control" disabled placeholder="Telefone" value="'.$row["Telefone"].'">
</div>
</div>
</div>
<div class="row">
<div class="col-md-3">
<div class="form-group">
<label>Peso</label>
<input type="text" class="form-control" disabled placeholder="Peso" value="'.$row["Peso"].'Kg">
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label>Altura</label>
<input type="text" class="form-control" disabled placeholder="Altura" value="'.$row["Altura"].'Cm">
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label>Perimetro Abdominal</label>
<input type="text" class="form-control" disabled placeholder="Perimetro abdominal" value="'.$row["Perimetro_abdominal"].'Cm">
</div>
</div>
<div class="col-md-2">
<div class="form-group">
<label>Perimetro Anca </label>
<input type="text" class="form-control" disabled placeholder="Perimetro Anca" value="'.$row["Perimetro_anca"].'Cm">
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label>Medicação:</label>
<textarea rows="3" class="form-control" name="Medicacao" placeholder="Medicação" id="Medicacao">'.$row["Medicacao"].'</textarea>
</div>
<input type=hidden name=hidden value="'.$row["id"].' ">
<input type="submit" name="med" class="btn btn-info btn-fill pull-right" value="Atualizar" id="med"></div>
<div class="col-md-6">
<div class="form-group">
<label>Observações:</label>
<textarea rows="3" name="Obs" class="form-control" placeholder="Anotar observações" id="Obs">'.$row["Obs"].' </textarea>
</div>
<input type="submit" name="obs" class="btn btn-info btn-fill pull-right" value="Atualizar">
</div>
</div></form>';
}
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_close($conn);
?>
<script type="text/javascript">
$( "#med" ).click(function() {
$.ajax({
type: "POST",
url: "update.php",
success: function(){
alertify.success('campo atualizado com sucesso');
}
});
});
</script>
update.php
include_once("includes/config.php");
if(isset($_POST['med'])) {
$UpdateQuery = "UPDATE utilizadores SET Medicacao='$_POST[Medicacao]' WHERE id='$_POST[hidden]' ";
$res = mysqli_query($conn,$UpdateQuery);
if($res == 1)
{
//header('Location: ' . $_SERVER['HTTP_REFERER'],true, 301 );
}
?>
You are not cancelling the default submit event of your submit button when you handle the click event. That causes the form to be submitted in the normal way as well by going to the url that is set in the action attribute of the form.
Apart from that, you also need to add the data to the ajax request.
This:
$( "#med" ).click(function() {
$.ajax({
type: "POST",
url: "update.php",
success: function(){
alertify.success('campo atualizado com sucesso');
}
});
});
should be:
$( "#med" ).click(function(event) {
// cancel default submit event
event.preventDefault();
$.ajax({
type: "POST",
// add data
data: $('form#go').serialize(),
url: "update.php",
success: function(){
// Do whatever you need to here
console.log('campo atualizado com sucesso');
}
});
});

Uploading image with another form text input in codeigniter?

i'm have confusing problem about uploading image in multiple input text like registration page. i found solution but the image is not save to database or folder.
please help me all...
my admin.php view
<script>
//upload img
$('#form').ajaxForm({
//uploadimg is my form id
dataType: 'json',
success: processJson
});
function processJson(data) {
if(data.msg=="success"){
alert('Upload sukses.');
}
else{
alert('Upload gagal.');
}
}
function add_edulibs()
{
save_method = 'add';
$('#form')[0].reset(); // reset form on modals
$('.form-group').removeClass('has-error'); // clear error class
$('.help-block').empty(); // clear error string
$('#modal_form').modal('show'); // show bootstrap modal
$('.modal-title').text('Tambah Data'); // Set Title to Bootstrap modal title
}
function edit_edulibs(id)
{
save_method = 'update';
$('#form')[0].reset(); // reset form on modals
$('.form-group').removeClass('has-error'); // clear error class
$('.help-block').empty(); // clear error string
//Ajax Load data from ajax
$.ajax({
url : "<?php echo site_url('edulibs/ajax_edit/')?>/" + id,
type: "GET",
dataType: "JSON",
success: function(data)
{
$('[name="id"]').val(data.id);
$('[name="nama"]').val(data.nama);
$('[name="nim"]').val(data.nim);
$('[name="pembimbing"]').val(data.pembimbing);
$('[name="subyek"]').val(data.subyek);
$('[name="judul"]').val(data.judul);
$('[name="tanggal"]').datepicker('update',data.tanggal);
$('[name="alamat"]').val(data.alamat);
$('[name="kontak"]').val(data.kontak);
$('[name="email"]').val(data.email);
$('[name="penerbit"]').val(data.penerbit);
$('[name="file_upload"]').val(data.gambar);
$('#modal_form').modal('show'); // show bootstrap modal when complete loaded
$('.modal-title').text('Edit Data'); // Set title to Bootstrap modal title
},
error: function (jqXHR, textStatus, errorThrown)
{
alert('Error mendapatkan data');
}
});
}
</script>
<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">Kelola Data</h3>
</div>
<div class="modal-body form">
<form action="<?php echo base_url('edulibs/do_upload/')?>" method="post" enctype="multipart/form-data" 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">Nama</label>
<div class="col-md-9">
<input name="nama" placeholder="Nama Lengkap" class="form-control" type="text">
<span class="help-block"></span>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">NIM</label>
<div class="col-md-9">
<input name="nim" placeholder="Nomor Induk Mahasiswa" class="form-control" type="text">
<span class="help-block"></span>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">Pembimbing</label>
<div class="col-md-9">
<input name="pembimbing" placeholder="Nama Pembimbing" class="form-control" type="text">
<span class="help-block"></span>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">Subyek</label>
<div class="col-md-9">
<input name="subyek" placeholder="Subyek" class="form-control" type="text">
<span class="help-block"></span>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">Judul</label>
<div class="col-md-9">
<textarea name="judul" placeholder="Judul Karya Tulis" class="form-control"></textarea>
<span class="help-block"></span>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">Tanggal Terbit</label>
<div class="col-md-9">
<input name="tanggal" placeholder="Tahun-Bulan-Hari" class="form-control datepicker" type="text">
<span class="help-block"></span>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">Alamat</label>
<div class="col-md-9">
<textarea name="alamat" placeholder="Alamat Lengkap" class="form-control"></textarea>
<span class="help-block"></span>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">Kontak</label>
<div class="col-md-9">
<input name="kontak" placeholder="Nomor Kontak" class="form-control" type="text">
<span class="help-block"></span>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">Email</label>
<div class="col-md-9">
<input name="email" placeholder="Alamat e-mail" class="form-control" type="text">
<span class="help-block"></span>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">Penerbit</label>
<div class="col-md-9">
<input name="penerbit" placeholder="Nama Penerbit" class="form-control" type="text">
<span class="help-block"></span>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">Foto</label>
<div class="col-md-9">
<input type="file" name="file_upload" />
<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">Simpan</button>
<button type="button" class="btn btn-danger" data-dismiss="modal">Batalkan</button>
</div>
My edulibs.php controller
public function admin()
{
$this->load->view('admin');
}
function do_upload(){
$config['upload_path'] = "./uploads/foto/";
$config['allowed_types'] = 'gif|jpg|png|JPEG';
$config['file_name'] = url_title($this->input->post('file_upload'));
$this->upload->initialize($config);
if(!$this->upload->do_upload('file_upload'))
{
echo $this->upload->display_errors();
}
else{
$data = array(
'gambar'=>$this->upload->file_name
);
$this->edulib_model->insert($data,'edulib');
}
}
My edulib_model.php
public function save($data)
{
$this->db->insert($this->table, $data);
return $this->db->insert_id();
}
public function insert($data,$table){
$this->db->insert($data,$table);
}

PHP & Bootstrap - Modal Shown just before content updated

In the database I have a table called articles. On one page of my website there is a list of titles of all articles - and each title is actually a link to edit the article. It looks like this:
<a onclick="launch_edit_modal(<?php echo $article->getId(); ?>)" data-toggle="modal" data-target="#edit_modal" href="#">Edit</a>
When you click on the link - function launch_edit_modal(id_of_article) is triggered:
<script type="text/javascript">
$('#edit_modal').modal({
show: false
});
function launch_edit_modal(id) {
$.ajax({
type: "POST",
url: "return_article_data.php",
data: {
id:id
},
dataType: "json",
success: function(data) {
$("#category").val(data.category_id);
$("#title").val(data.title);
$("#content").val(data.content);
$('#edit_modal').show();
}
});
}
</script>
This script is at the bottom.
So, using AJAX I find the other data (category, title, content) about that article and then I update the form which is modal's content:
<div class="modal fade" id="edit_modal" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span>×</span></button>
<h3 class="modal-title">Edit article</h3>
</div>
<div class="modal-body">
<div class="row">
<form id="edit_forma" class="form-horizontal" method="post" action="edit_article.php">
<fieldset>
<!-- Select Basic -->
<div class="form-group">
<label class="col-md-4 control-label" for="selectbasic">Category:</label>
<div class="col-md-4">
<select id="category" name="category" class="form-control">
<?php foreach($categories as $c): ?>
<option value="<?php echo $c->getId(); ?>"><?php echo $c->getName(); ?></option>
<?php endforeach; ?>
</select>
</div>
</div>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="title">Title:</label>
<div class="col-md-4">
<input id="title" name="title" placeholder="" class="form-control input-md" type="text">
</div>
</div>
<!-- Textarea -->
<div class="form-group">
<label class="col-md-4 control-label" for="content">Content:</label>
<div class="col-md-4">
<textarea class="form-control" id="content" name="content" rows="10"></textarea>
</div>
</div>
<!-- Button -->
<div class="form-group">
<label class="col-md-4 control-label" for="submit"></label>
<div class="col-md-4">
<button type="submit" id="submit" name="submit" class="btn btn-success">Izmeni</button>
</div>
</div>
</fieldset>
</form>
</div>
</div>
<div class="modal-footer">
<button type="button" data-dismiss="modal" class="btn btn-default">
Close
</button>
</div>
</div>
</div>
</div>
All of this works - but there is a problem - when I click on edit link - Modal is shown, and then after 2 or 3 seconds the form is filled (updated) with data.
Is there a way to fix this - to show Modal after the form is filled (updated)?
Thanks in advance.
As Ghazanfar Mir wrote in the comments below my question, I need just to set:
async: false
for ajax call, and the problem is solved.
Thank you Ghazanfar Mir.

Categories