modal Pop Up window not waiting for Ajax Call - php

i have tried all my possible best and searched every where but to no avail that is why i have decided to do this after about 4years of learning from this website.
i noticed that my modal pop up window is not waiting for my ajax that sends the value retrived from the anchor tag which is then sent to the PHP file here by casuing the php to give an index undefined error. but if i just use the alert() on success: function() i see the values been retrived from the anchor tag. now this is my code the html link <a href="#" class="btn btn-info btn-sm" id="editr" data-id="<?php echo $postID; ?>" data-owner="<?php echo $op; ?>" data-toggle="modal" data-target="#modal-form" >Edit Page</a>
$(function(){
$("#editr").click(function(e) {
e.preventDefault();
var id = $(this).attr("data-id");
var owner = $(this).attr("data-owner");
var dataString = 'id=' + id + '&user=' + owner;
$.ajax({
url: "../config/edit.php",
type: "POST",
data : dataString,
cache: false,
success: function(data){
alert(data);
}
});
});
});
this is the modal part
<div class="modal fade" id="modal-form" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<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">Form in moadl</h4>
</div>
<div class="modal-body">
<form role="form">
<div class="form-group">
<label for="exampleInputEmail1"> Email address</label>
<input type="email" class="form-control" id="exampleInputEmail1" placeholder="Enter email">
</div>
<div class="form-group">
<label for="exampleInputPassword1">Password</label>
<input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password">
</div>
<div class="checkbox">
<label>
<input type="checkbox"> Check me out
</label>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>

You need to wait for the promise to return and then continue on with what you were trying to do.
$(function(){
$("#editr").click(function(e) {
e.preventDefault();
var id = $(this).attr("data-id");
var owner = $(this).attr("data-owner");
var dataString = 'id=' + id + '&user=' + owner;
var promise = $.ajax({
url: "../config/edit.php",
type: "POST",
data : dataString,
cache: false
});
promise.then(function(data){
//show modal with data (in place of alert)
alert(data);
});
});
});

Related

Getting id from MySQL to modal and passing it through Ajax

I fetched the id from the database and passed it to the modal, and then echo it on another page through ajax but the problem is that it keeps returning the same id
<?php if(mysqli_num_rows($select_invoice_query)>0) {
while($invoice_result = mysqli_fetch_assoc($select_invoice_query)) { ?>
<tr>
<td><i class="fa fa-envelope"></i></td>
</tr>
<?php }} ?>
Bootstrap Modal
<div id="send-invoice-modal" class="modal fade" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Send Invoice</h4>
</div>
<div class="modal-body">
<div class="form-group">
<input type="email" id="invoice_to" class="form-control" placeholder="Email">
</div>
<div class="form-group">
<input type="text" id="invoice_subject" class="form-control" placeholder="Subject">
</div>
<div class="form-group">
<textarea class="form-control" id="invoice_message" rows="3" placeholder="Message"></textarea>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary" data-dismiss="modal" id="send_invoice_btn">Send</button>
<div class="error_message"></div>
</div>
</div>
</div>
</div>
JS
$('#send_invoice_btn').on('click',function(){
var invoice_id = $('#send_invoice').data('invoiceid'); // this is collecting the id from the anchor tag
var invoice_to = $('#invoice_to').val();
var invoice_subject = $('#invoice_subject').val();
var invoice_message = $('#invoice_message').val();
$.ajax({
url: "includes/send-invoice.inc.php"+invoice_id,
type: 'POST',
data: {invoice_id:invoice_id,invoice_to:invoice_to,invoice_subject:invoice_subject,invoice_message:invoice_message},
success: function(send_invoice_result){
$('.error_message').html(send_invoice_result);
}
})
});
send-invoice.inc.php File
<?php
include 'db-connection.php';
echo $invoice_id = $_GET['invoice_id'];
?>
it keeps returning the same id for some unknown reason. Can anyone tell me what am I doing wrong?
This works for me
$('.send_invoice').on('click',function(){
var invoice_id = $(this).attr('data-invoiceid');
$('#send_invoice_btn').on('click',function(){
var invoice_to = $('#invoice_to').val();
var invoice_subject = $('#invoice_subject').val();
var invoice_message = $('#invoice_message').val();
$.ajax({
url: 'includes/send-invoice.inc.php?invoice_id='+invoice_id,
type: 'POST',
data: {invoice_to:invoice_to,invoice_subject:invoice_subject,invoice_message:invoice_message},
success: function(send_invoice_result){
}
});
});
});

I can't pull data other than input with ajax

I have a modal I opened with Ajax and I can pull the information from the database. My problem is this: I can only pull data into the input. Any div, p tags or ckeditor etc. I can't pull data into it. What can I do about this? In the meantime, I do not have problems withdrawing data from the database. I can pull the data. I'm looking at Console. But I can only pull it into the input.
index.php
<div class="modal fade edit" id="edit-modal">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title pl-3" id="exampleModalScrollableTitle">Edit Test</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true" class="text-white">×</span>
</button>
</div>
<form method="post" id="frmedit" enctype="multipart/form-data">
<div class="modal-body pre-scrollable">
<input type="text" name="id" id="id" class="form-control">
<p id="asd"></p>
<div class="form-group form-float mt-3">
<div class="form-line">
<input type="text" class="form-control" name="title" id="title">
<label class="form-label">Title</label>
</div>
</div>
<div class="form-group form-float">
<label class="form-label">CKeditor</label>
<textarea name="test1"></textarea>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn bg-grey waves-effect" data dismiss="modal">Close</button>
<input type="submit" id="update" class="btn bg-purple waves-effect" value="Save">
</div>
</form>
</div>
</div>
</div>
<script type="text/javascript">
$(document).ready(function () {
$(document).on('click' , '.bn-edit' ,function(){
var id = this.id;
console.log("id:"+id+",type:test");
$.ajax({
type: "GET",
url: "read.php?id="+id+"&type=test",
success:function(response){
$("#edit-modal").modal('show');
$.each(response, function(key, value) {
console.log("key:"+key+",value:"+value);
})
$("#title").val(title);
$("#test1").html(test1);
$("#asd").val(asd);
$("#id").val(id);
}
});
});
});
function loaddata() {
$.ajax({
url: 'read.php',
type: 'post',
data: {"type":"test_all"},
success:function(response){
$("#container").html(response);
}
});
}
$(function () {
CKEDITOR.replace('test1');
CKEDITOR.config.height = 125;
});
read.php:
$id = $_REQUEST["id"];
switch($_REQUEST["type"]) {
case "test":
if(isset($_REQUEST["id"])) {
$result = $conn->read("test", " id='$id' ", "", "", "");
if(!empty($result)) {
$responseArray["title"] = $result[0]["title"];
$responseArray["asd"] = $result[0]["asd"];
$responseArray["test1"] = $result[0]["test1"];
$responseArray["id"] = $result[0]["id"];
echo json_encode($responseArray);
}
}
break;
case "test_all":
$result = $conn->verioku("id,title,asd", "test", "id");
break;
default:
break;
}
The problem solved. I added just this code:
CKEDITOR.instances.ic_ozet.setData(response.ic_ozet);
Thank you so much for your comments.
$(document).on('click' , '.bn-edit' ,function(){
var id = this.id;
console.log("id:"+id+",type:test");
$.ajax({
type: "GET",
url: "read.php?id="+id+"&type=test",
success:function(response){
$("#edit-modal").modal('show');
$.each(response, function(key, value) {
console.log("key:"+key+",value:"+value);
})
$("#title").val(title);
CKEDITOR.instances.test1.setData(response.test1);
$("#asd").html(asd);
$("#id").val(id);
}
});
});

How to submit a remote modal form via ajax - jQuery

This is a little different from the method of submitting a form via static form value in modal. Using the same method is not working because AJAX not submitting the form value from the shown modal. So,
html
load form
modal
<div class="modal fade" id="xModal" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title" id="xModalLabel">loading...</h4>
</div>
<div class="modal-body">...</div>
<div class="modal-footer">
<button type="button" class="btn btn-link waves-effect">SAVE CHANGES</button>
<button type="button" class="btn btn-link waves-effect" data-dismiss="modal">CLOSE</button>
</div>
</div>
</div>
</div>
jquery
//clear modal
$('body').on('hidden.bs.modal', '.modal', function () {
$(this).removeData('bs.modal');
});
//submit form
$('#xModal').on('shown.bs.modal',function (e) {
e.preventDefault();
var form=$(this).find('form').serialize();
$('#save_btn').on('click',function(){
$.ajax({
url:'inc/data.php',
type:'POST',
data:form,
success : function(data){
console.log(data);
}
});
});
});
data.php
<?
print_r($_POST);
?>
remote_form.html
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title" id="xModalLabel">Reg Form</h4>
</div>
<div class="modal-body">
<form>
<input type="text" name="fname" /><br />
<input type="text" name="lname" /><br />
<input type="text" name="email" />
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-link waves-effect" id="save_btn">SAVE</button>
</div>
</div>
Thanks, any comment is welcome!
Delegate your click event, move the event outside the on('shown.bs.modal') event
$(document).on('click', '#save_btn',function(){
var form=$(this).closest('form').serialize();
$.ajax({
url:'inc/data.php',
type:'POST',
data:form,
success : function(data){
console.log(data);
}
});
});
You can set ids to input fields and then getting data from that id as follows.
<input type="text" name="fname" id="fname"/>
and on the script do like this.
//submit form
$('#xModal').on('shown.bs.modal',function (e) {
e.preventDefault();
$('#save_btn').on('click',function(){
var fname = $("#fname");
$.ajax({
url:'inc/data.php',
type:'POST',
data:{
fname = fname
},
success : function(data){
console.log(data);
}
});
});
});

bootstrap modal insert form submit won't reset to add another entry

I am using php mysql to insert to the database and that was working, however the bootstrap modal won't let me submitting another item. The button still says inserting and the last inserted data is still showing in the inputs.
I want to insert multiple times as this becomes part of a larger edit form.
This is put outside of the edit form submit button:
<div id="add_data_Modal" class="modal fade" tabindex="-1" 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>
<h4 class="modal-title">Add A New Item</h4>
</div>
<div class="modal-body">
<form method="post" id="insert_form">
<input type="hidden" name="job_id" id="job_id" value="<?php echo $job_id;?>">
<input type="hidden" class="form-control" id="job_mat_qty" name="job_mat_qty" value="1" />
<div class="form-group">
<label for="job_mat_desc" class="col-sm-2 control-label">Description</label>
<div class="col-sm-10">
<textarea class="form-control editors" id="job_mat_desc" name="job_mat_desc" autocomplete="off" placeholder="Enter item title and / or description" />
</textarea>
</div>
</div>
<div class="form-group">
<label for="job_mat_cost" class="col-sm-2 control-label">Price</label>
<div class="col-sm-10">
<div class="input-group input-group"> <span class="input-group-addon"><?php echo CURRENCY ?></span>
<input type="text" class="form-control calculate invoice_product_price txt" autocomplete="off" id="job_mat_cost" name="job_mat_cost" aria-describedby="sizing-addon1" placeholder="0.00" />
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-10"> </div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<input type="submit" name="insert" id="selected" value="Insert" class="btn btn-success" />
</div>
</form>
</div>
</div>
</div>
$(document).on('click', '.delete-rows', function(){
var id = $(this).attr("id");
if (confirm("Are you sure you want to delete this Item?")) {
$.ajax({
type: "POST",
url: "job_remove_material_new.php",
data: ({
id: id
}),
cache: false,
success: function(html) {
$(".delete_mem" + id).fadeOut('slow');
}
});
} else {
return false;
}
});
$('#insert_form').on("submit", function(event){
event.preventDefault();
if($('#job_mat_desc').val() == "")
{
alert("Description is required");
}
else
{
$.ajax({
url:"add_material.php",
method:"POST",
data:$('#insert_form').serialize(),
cache: false,
beforeSend:function(){
$('#insert').val("Inserting");
},
success:function(data){
$('#insert_form')[0].reset();
$('#add_data_Modal').modal('hide');
$('#invoice_table').html(data);
}
});
$(function () {
$(document).on("hidden.bs.modal", "#add_data_Modal", function () {
$('#insert_form')[0].reset();
});
});
}
});
Sorry I have no clue how to add this code correctly for this forum so please be kind....
Thanks
You should try like this
$('#insert_form').on("submit", function(event){
event.preventDefault();
if($('#job_mat_desc').val() == "")
{
alert("Description is required");
}
else
{
$.ajax({
url:"add_material.php",
method:"POST",
data:$('#insert_form').serialize(),
cache: false,
beforeSend:function(){
$('#insert').val("Inserting");
},
success:function(data){
//$('#insert_form')[0].reset();
$('#insert_form input[type="text"],textarea').val(''); // here is change
$('#add_data_Modal').modal('hide');
$('#invoice_table').html(data);
}
});
In my case it will work successfully so I hope it will help you

Updating the query on the other page and set the button disabled

I have a question regarding ajax and modal. What I need to do is, when user click submit of the button, it should set the button disabled and sending the values to the other page to update the database. But my approach is using modal.
My modal code is like this,
<div class="modal fade" id="qcRejectModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" 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">QC <font color="CC0000"><b>REJECT</b></font> Confirmation Window</h4>
</div>
<div class="modal-body">
<div class="row">
<div class="col-md-12">
<div class="form-group">
This is the confirmation window to PASS all the Quality Control for, <br/><br/>
PROJECT : <b>'.$row['PROJECT_NAME'].'</b>
HEADMARK : <b>'.$row['HEAD_MARK'].'</b>
ID : <b>'.$row['ID'].'
</div>
</div>
</div><br/>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<textarea class="form-control" placeholder="Reason for rejection" rows="2" name="rejectionreason" id="rejectionreason" required></textarea>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="button" name="rejectbutton" id="rejectbutton" class="btn btn-danger">Confirm REJECT!</button>
</div>
</div>
</div>
</div>
</div>
So in this code when user click the button,
echo "<input type='hidden' data_project='$row[PROJECT_NAME]' data_headmark='$row[HEAD_MARK]' data_id='$row[ID]'></input>";
echo '<button type="button" name="qcreject" id="qcreject" class="btn btn-primary btn-danger btn- lg btn-block" data-toggle="modal" data-target="#qcRejectModal">
<span style="font-size:35px"> '.$row['PROJECT_NAME'].' / <b>'.$_POST ["hm"].'</b>/'.$row['ID'].' ~ FAIL</span></button>';
modal code is executed and showed and waiting for the user to input the comment in the textfield and when user click REJECT button, it should pass
$row['PROJECT_NAME'], $row['HEAD_MARK'], $row['ID'] and the textfield value (#rejectionreason)into the processclass.php.
And on the processclass.php, those variables will be used to update the database with SQL query.
This is the snippet of my ajax.
$('.rejectbutton').click(function() {
$.ajax({
url: 'processclass.php',
type: 'POST',
data: ,
success: function (result) {
alert("Your data has been submitted");
}
});
});
I am having problem with executing on the process class. that means passing those values to the processclass.php.
$('.rejectbutton').click(function() {
$(this).attr('disabled','disabled');
$.ajax({
url: 'processclass.php',
type: 'POST',
data: {param_name: '<?php echo $param_value ?>'},
success: function (result) {
alert("Your data has been submitted");
},
complete: function(){
$(this).removeAttr('disabled');
}
});
});

Categories