Submit function sending old and new data - php

$("#create-center-form").submit(function(e){
e.preventDefault();
let parent;
let _token = $("input[name=_token]").val();
let name=$("#name_create_medical_center").val();
let email=$("#email_create_medical_center").val();
let phone=$("#phone_number_create_medical_center").val();
let country_id=$("#country_selector_create").val();
let state_id=$("#state_selector_create").val();
let city_id=$("#city_selector_create").val();
let country_name=$("#country_selector_create option:selected").text();
let state_name=$("#state_selector_create option:selected").text();
let city_name=$("#city_selector_create option:selected").text();
if($("#center_selector").is(":visible")){
parent=$("#center_selector option:selected").val();
}
else
parent="";
console.log(name);
console.log(email);
console.log(phone);
$("#first_button_create").prop('disabled', true);
$.ajax({
url:"{{route('add-medical-center')}}",
type:"POST",
data:{
_token:_token,
type:type,
name:name,
email:email,
phone:phone,
country_id:country_id,
state_id:state_id,
city_id:city_id,
country_name:country_name,
state_name:state_name,
city_name:city_name,
parent_id:parent,
},
success:function(response){
$("#tbody1").append(
'<tr>'+
'<td>'+response.id+'</td>'+
'<td>'+response.name+' </td>'+
'<td>'+response.address+' </td>'+
'<td>'+response.email+' </td>'+
'<td>'+response.phone+'</td>'+
'<td>'+
'<img src="https://img.icons8.com/material-sharp/24/26e07f/edit--v1.png" />'+
'<img src="https://img.icons8.com/ios-filled/50/fa314a/full-trash--v2.png" width="25"/>'+
'</td>'+
'</tr>'
);
alert("your response has been added!");
$("#create_modal").modal('hide');
$("#name_create_medical_center").val('');
$("#email_create_medical_center").val('');
$("#phone_number_create_medical_center").val('');
$('#country_selector_create').find('option').remove().end().append("<option value='select'> Select Country</option>");
$('#state_selector_create').find('option').remove().end().append("<option value='select'> Select State</option>");
$('#city_selector_create').find('option').remove().end().append("<option value='select'> Select City</option>");
$("#first_button_create").prop('disabled', false);
},
error:function(respomse){
alert("please check the input data and try again");
$("#first_button_create").prop('disabled', false);
},
});
});
Hello I'm using Laravel with Ajax and sending a form to add a record to the database but when submitting this form on the first time it works good but on the second, third, fourth … nth time it resends all of the old form submits.
,for example
submitting for the first time: works great it only sends the first time data
submitting for the second time: it sends the second time data and first time data
submitting for the third time: it sends the third, second and the first time data
so, on the second time instead of only adding one record to my data base I'm adding two records the new and old one and in the third time I'm adding three recode the new and old ones and goes on....
the html/bootstrap modal that contains the form:
<div id="create_modal" class="modal fade" role="dialog" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h3 class="modal-title">Add New User</h3>
</div>
<div class="modal-body">
<form id="create-center-form" class="ajax-form form-horizontal" enctype="multipart/form-data">
#csrf
<input type="hidden" id="hidden_create" value="">
<!-- First Name -->
<div class="form-group row">
<label for="name" class="col-md-4 col-form-label text-md-right">Name</label>
<div class="col-md-6">
<input id="name_create_medical_center" type="text" class="form-control" name="first_name" value="" required autocomplete="first_name" autofocus>
</div>
</div>
<!-- Main Center -->
<div class="form-group row">
<label for="main_center" class="col-md-4 col-form-label text-md-right">Has a Main Center</label>
<div class="col-md-6">
<input type="checkbox" class="onoffswitch-checkbox" id="has_main_center" checked>
</div>
</div>
<!-- Select Main Center -->
<div class="form-group row" id="center_selector">
<label for="main_center" class="col-md-4 col-form-label text-md-right">Select Center:</label>
<div class="col-md-6">
<select class="form-select" aria-label="Default select example" id="medical_center_selector" >
<option selected>Select The Main Center</option>
</select>
</div>
</div>
<!-- Email Address -->
<div class="form-group row">
<label for="email" class="col-md-4 col-form-label text-md-right">Email</label>
<div class="col-md-6">
<input id="email_create_medical_center" type="email" class="form-control" name="Emial" value="" required autocomplete="email">
</div>
</div>
<!-- Phone Number -->
<div class="form-group row">
<label for="phone_number" class="col-md-4 col-form-label text-md-right">Phone Number</label>
<div class="col-md-6">
<input id="phone_number_create_medical_center" type="text" class="form-control" name="phone_number" value="" required autocomplete="Password">
</div>
</div>
<!-- Country, State and City -->
<div class="row">
<div class="col-md-12">
<div class="form-group">
<select class="form-select" aria-label="Default select example" id="country_selector_create" >
<option selected>Select Country</option>
</select>
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<select class="form-select" id="state_selector_create" onchange="">
<option selected>Select State</option>
</select>
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<select class="form-select" aria-label="Default select example" id="city_selector_create">
<option selected>Select City</option>
</select>
</div>
</div>
</div>
<div class="form-group row">
<input type="hidden" name="action" id="user-form-action" value="create" />
<input type="hidden" name="hidden_id" id="user-hidden-id" />
<button type="submit" name="create_update_user" id="user-form-action-button" value="create" class="btn btn-warning">Submit</button>
</div>
</form>
</div>
</div>
</div>

You are working with modals (Bootstrap). All good. The thing is, they are cached in the browser. You probably close the modal and open a new one. This could be the reason you are sending twice or more the same data.
Instead of
$("#create_modal").modal('hide');
use
$("#create_modal").modal('dispose');
to destroy it's instance as explained at https://getbootstrap.com/docs/4.6/components/modal/#modaldispose
A user's request to open a new modal will simply create a new instance of the modal. I hope this solves your issue. It's worth a try.

Related

How to attach an element to a form

I'm writing a dynamic form that appends available text-fields depending on value that was selected by the user. How do I make it work? For context, it's an edit button, so the text-field will be pre-filled with STRING from the database.
The functions of getting the data from the database works, and the modal also shows, but the problem is just that the select-form doesn't work.
I tried calling "#btn-edit-account" but it doesn't work.
Code for the modal edit-account
<div class="modal fade" id="edit-account-modal" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLongTitle">Edit Account Details</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="tab-content" id="nav-tabContent">
<div class="tab-pane fade show active" id="nav-add" role="tabpanel" aria-labelledby="nav-home-tab">
<form id="form-edit-account">
<input type="hidden" name="_token" value="{{csrf_token()}}">
<div class="container my-2">
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label>Role</label>
<select type="select" id="select-role" class="form-control edit_role select-role" name="role_id" required>
</select>
</div>
<div class="form-group student-number-container hidden">
<label>Student Number</label>
<input type="text" class="form-control" id="edit_student_number" name="student_number" required>
</div>
<div class="organization-container hidden">
</div>
<div class="form-row">
<div class="col-md-6">
<label>First name</label>
<input type="text" class="form-control" id="edit_first_name" name="first_name" required>
</div>
<input type="hidden" id="token" name="_token" value="{{csrf_token()}}">
<div class="col-md-6">
<label>Last Name</label>
<input type="text" class="form-control" id="edit_last_name" name="last_name" required>
</div>
</div>
<div class="form-group">
<label>Email</label>
<input type="email" class="form-control" id="edit_email" name="email">
</div>
<div class="form-row">
<label class="control-label">New Password <span class="required">*</span></label>
<input type="password" id="new_password" name="new_password" minlength="6" class="form-control password" required>
</div>
<div class="form-row">
<label class="control-label">Confirm New Password <span class="required">*</span></label>
<input type="password" id="confirm_password" name="confirm_password" minlength="6" class="form-control password" required>
</div>
<div class="form-group text-right mt-4">
<button type="button" class="btn btn-dark" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-success btn-confirm-edit-account">Update</button>
</div>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
Javascript for btn-edit-account
$(document).on('click', '.btn-edit-account', function() {
$('#edit-account-modal').modal('show');
var id = $(this).attr('data-id');
$.ajax({
url: "users/get-specific-user-info",
type: "POST",
data: {
id: id,
_token: "{{csrf_token()}}"
},
success: function(data) {
// $('#edit_role').val(data.role_id);
$('#edit_first_name').val(data.first_name);
$('#edit_last_name').val(data.last_name);
$('#edit_email').val(data.email);
console.log(data);
}
});
});
});
Javascript for select-role
$(document).on('change', '#select-role', function() {
var val = $(this).val();
if(val != 3){
$('.student-number-container').show();
}
else {
$('.student-number-container').hide();
}
if(val != 1){
$('.organization-container').html("");
}
else {
appendOrganization();
$('.organization-container').show();
}
});
function for appendOrganization
function appendOrganization() {
var html = "";
html += '<div class="form-group"> <label>Organization Name</label> <input type="text" class="form-control" name="organization_name" required> </div>';
html += '<div class="form-group"> <label>Organization Type</label> <select type="select" id="select-org-type" class="form-control" name="organization_type" required>'+
'<option value="" selected disabled>Select Organization Type</option>'+
'<option value="TYPE A">TYPE A</option>'+
'<option value="TYPE B">TYPE B</option>'+
'</select></div>';
html += '<div class="form-group"> <label>College</label> <select type="select" id="select-org-type" class="form-control" name="organization_college" required> <option value="" selected disabled>Select Organization Type</option> <option value="COLLEGE ONE">COLLEGE ONE</option> <option value="COLLEGE TWO">COLLEGE TWO</option> </select> </div>';
$('.organization-container').html(html);
}
The select in the modal should show the available choices in the given select, then will dynamically change the form depending on the selected choice.

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>

Update Database tables with values input by user in CodeIgniter using PHP

I'm trying to update one of my user tables in the Database with values taken from the the user. But for some reason it's not updating anything.
HTML Form
<form class="form-horizontal" method = "post">
<fieldset>
<!-- Form Name -->
<legend>User Details</legend>
<div>
<?php echo $this->session->flashdata('msg'); ?>
</div>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="name">Full Name</label>
<div class="col-md-8">
<input id="name" name="name" type="text" placeholder="something" class="form-control input-md">
</div>
</div>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="dob">Date of Birth</label>
<div class="col-md-4">
<input id="dob" name="dob" type="text" placeholder="" class="form-control input-md">
</div>
</div>
<!-- Multiple Radios -->
<div class="form-group">
<label class="col-md-4 control-label" for="gender">Gender</label>
<div class="col-md-2">
<select id="gender" name="gender" class="form-control">
<option value="1">Male</option>
<option value="2">Female</option>
</select>
</div>
</div>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="degree">Degree</label>
<div class="col-md-8">
<input id="degree" name="degree" type="text" placeholder="degree" class="form-control input-md">
</div>
</div>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="specialization">Specialization</label>
<div class="col-md-8">
<input id="specialization" name="specialization" type="text" placeholder="specialization" class="form-control input-md">
</div>
</div>
<!-- Select Basic -->
<div class="form-group">
<label class="col-md-4 control-label" for="year">Degree Year</label>
<div class="col-md-2">
<select id="year" name="year" class="form-control">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</div>
</div>
<!-- Select Basic -->
<div class="form-group">
<label class="col-md-4 control-label" for="semester">Semester</label>
<div class="col-md-2">
<select id="semester" name="semester" class="form-control">
<option value="1">1</option>
<option value="2">2</option>
</select>
</div>
</div>
<!-- File Button -->
<div class="form-group">
<label class="col-md-4 control-label" for="filebutton">Upload Profile Picture</label>
<div class="col-md-4">
<input id="filebutton" name="filebutton" class="input-file" type="file">
</div>
</div>
<!-- Button -->
<div class="form-group">
<label class="col-md-4 control-label" for="submit"></label>
<div class="col-md-4">
<a href="<?php echo base_url("/index.php/studentDashboardController/saveUserDetails"); ?>" >
<button id="submit" name="submit" class="btn btn-primary">Save Changes</button>
</a>
</div>
</div>
</fieldset>
studentDashboardController
public function saveUserDetails()
{
$this->load->model('userModel');
if (isset($this->session->userdata['logged_in']))
{
$username = ($this->session->userdata['logged_in']['username']);
}
$user = $this-> userModel-> getUserUid($username); //Gets the uid of the current user
$this->userModel->saveUserDetails($user);
$this->session->set_flashdata('msg', '<div class="alert alert-success text-center">Successfully Inserted Data</div>');
$this->load->view('studentDashboard/common',$data);
redirect(base_url('index.php/studentDashboard/editProfile',$data1));
}
userModel
public function saveUserDetails($uid)
{
$data = array(
'name' => $this->input->post('name')
);
$this->db->where("uid",$uid);
$this->db->update("sysuser",$data);
}
The sysuser has the uid and name fields.I' not sure what I'm doing wrong here. Any help in this regard will be appreciated
You dont send your post data to controller. Change $this->userModel->saveUserDetails($user); as
$this->userModel->saveUserDetails($user,$this->input->post('name'));
Now Model should be like
public function saveUserDetails($uid,$name)
{
$data = array(
'name' => $name
);
$this->db->where("uid",$uid);
$this->db->update("sysuser",$data);
}
and make sure you have set correctly form action like
<form class="form-horizontal" method="post" action="<?php echo site_url('studentDashboardController/saveUserDetails');?>">

send a php variable as part of an html form

I am working on a website which I didn't create and to which I do not have full access (I can create a page and add html scripts but not much more)
I am trying to add a form which will send data to be processed by a third-party servlet.
I am trying to include in this form the client ID, which I saw can be retrieved using the php functionsession_id().
My question is, how to send this information along with the rest of the input data.
my form for now is this:
<form class="form-horizontal" action = "url" method="POST" target="_blank">
<fieldset>
<!-- Select Basic -->
<div class="form-group">
<label class="col-md-4 control-label" for="selectbasic">Culture</label>
<div class="col-md-4">
<select id="Crop" name="Crop" class="form-control">
<option value="1">Maïs</option>
<option value="2">Ble</option>
</select>
</div>
</div>
<!-- Select Basic -->
<div class="form-group">
<label class="col-md-4 control-label" for="selectbasic">Produit</label>
<div class="col-md-4">
<select id="Product" name="Product" class="form-control">
<option value="Iodure d'azote">Iodure D'azote</option>
<option value="Desherbant">Desherbant</option>
</select>
</div>
</div>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="area">Surface</label>
<div class="col-md-4">
<input id="Area" name="Area" type="text" placeholder=" " class="form-control input-md" required="">
<span class="help-block">en Ha</span>
</div>
</div>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="dose">Dosage</label>
<div class="col-md-4">
<input id="Dose" name="Dose" type="text" placeholder="" class="form-control input-md" required="">
<span class="help-block">en L/Ha</span>
</div>
</div>
<div class="form-group">
<!--need to get the actual id-->
<input type="hidden" value="1" name = "ID"/>
<input type="submit" value="Ajouter" />
</div>
</fieldset>
</form>
Since this is my first time handling web apps, is there something obvious i'm missing?
OK i was lacking basic understanding of php. sorry for bothering,
the answer was here
i just had to write
<input type="hidden" value="<?php echo session_id();?>" name = "ID"/>
instead of
<input type="hidden" value="1"name = "ID"/>

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