I can't pull data other than input with ajax - php

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);
}
});
});

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){
}
});
});
});

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

How to pass data to a modal from a context menu?

I've a table which contains two columns. I created a jQuery context menu with two entries: edit and delete.
If I perform a right click on a row and click on edit button, I'd like to open my bootstrap modal and pass data into it.
Here's my context menu code:
<script type='text/javascript'>
window.onload=function(){
$(function() {
$(".context-menu-one").contextMenu({
selector: 'td',
callback: function(key, options) {
switch (key) {
case 'edit':
var content = $(this).data('id');
$('#myModal').modal('show');
break;
case 'delete':
break;
}
},
items: {
"edit": {name: "Modifier", icon: "edit"},
"delete": {name: "Supprimer", icon: "delete"},
}
});
$('.context-menu-one').on('click', function(e){
console.log('clicked', this);
})
});
}
</script>
And here's my modal code:
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<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" id="myModalLabel">Test</h4>
</div>
<form class="my-form">
<div class="modal-body">
<label>SDA
<input type="text" id="sda" name="sda" value="" required="required" class="form-control col-md-7 col-xs-12">
</label>
<label>Service
<input type="text" id="service" name="service" value="" required="required" class="form-control col-md-7 col-xs-12">
</label>
<label id="label-id"></label>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Clode</button>
<button type="button" class="btn btn-primary" id="add">Add</button>
</form>
</div>
</div>
</div>
Here's PHP code to retrieve data:
$reponse = $bdd->query("SELECT * FROM table");
while ($donnees = $reponse->fetch()) {
echo '
<tr>
<td data-id="'.$donnees[0].'">' . $donnees[1] . '</td>
<td data-id="'.$donnees[0].'">' . $donnees[2]. '</td>
</tr>
';
}
Can someone tell me which is the best way to do this?
I've found a solution, here's updated code:
<script type='text/javascript'>
window.onload=function(){
$(function() {
$(".context-menu-one").contextMenu({
selector: 'td',
callback: function(key, options) {
switch (key) {
case 'edit':
sda = $(this).parent().find("td:eq(0)").text();
service = $(this).parent().find("td:eq(1)").text();
$("#sda").val(sda);
$("#service").val(service);
$('#myModal').modal('show');
break;
case 'delete':
break;
}
},
items: {
"edit": {name: "Modifier", icon: "edit"},
"delete": {name: "Supprimer", icon: "delete"},
}
});
$('.context-menu-one').on('click', function(e){
console.log('clicked', this);
})
});
}
</script>

modal Pop Up window not waiting for Ajax Call

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);
});
});
});

Categories