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){
}
});
});
});
Related
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);
}
});
});
Hi I am totally a newbie in php and i found a select2 to automatically populate dropdown, I would like to use it inside modal, first select2 inside its modal is working but the second one isnt working, here is my code for the FIRST ONE (Working)
HTML:
<div id="prevprojmodal" class="modal fade" role="dialog" data-keyboard="false" data-backdrop="static">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-body">
<select class="substat form-control" style="width:500px" name="substat" style="min-width: 100%"></select>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary">Save</button>
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
</div>
</div>
</div>
</div>
JS
<script type="text/javascript">
$('.substat').select2({
allowClear: true,
placeholder: 'Type to search for previous project',
ajax: {
url: 'ajax/getdata_previousproject.php',
dataType: 'json',
delay: 250,
processResults: function (data) {
return {
results: data
};
},
cache: true
}
});
$('.substat').on('change', function() {
var data = $(".substat option:selected").text();
$("#projectprevioustest").val(data);
})
</script>
And this is the code of the second one, but NOT WORKING:
HTML
<!-- Add Team Compo Modal -->
<div class="modal fade" id="addteamcompomodal">
<div class="modal-dialog">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header">
<h4 class="modal-title">Add a member to Team</h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<!-- Modal body -->
<div class="modal-body">
<div class="form-group">
<label> Staff Name </label>
<select class="staffname form-control" name="staffname" id="staffname" placeholder="Staff Name" style="width: 100%; padding: 0;"></select>
</div>
<div class="form-group">
<label> Percent Involvement </label>
<input type="text" name="teampi" id="teampi" placeholder="Percent Involvement" class="form-control">
</div>
<div class="form-group">
<label> Specific Duties & Responsibilities </label>
<input type="text" name="teamsdnr" id="teamsdnr" placeholder="Specific Duties & Responsibilities" class="form-control">
</div>
<div class="form-group">
<label> Specific Deliverables </label>
<input type="text" name="teamsd" id="teamsd" placeholder="Specific Deliverables" class="form-control">
</div>
<div class="form-group">
<label> Project Role </label>
<select required="" name="teampr" class="form-control" style="font-size: 12px;">
<?php
$resultatc = $pdo->prepare("SELECT * FROM teamroles");
$resultatc->execute();
$checkRtc = $resultatc->rowCount();
if ($checkRtc > 0) {
echo "<option disabled='disabled' value = '' selected>SELECT</option>";
foreach ($resultatc->fetchAll(PDO::FETCH_BOTH) as $disptc) {
for ($z = 0; $z < count($checkRtc); $z++) {
$disp_idtc = $disptc['tr_id'];
$disp_statustc = $disptc['tr_abb'];
}
echo "<option value='" . $disp_idtc . "'>" . utf8_encode($disp_statustc) . "</option>";
}
}
?>
</select>
</div>
</div>
<!-- Modal footer -->
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-dismiss="modal" onclick="addRecord()">Save</button>
<button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<!-- Add Team Compo Modal END-->
JS
<script type="text/javascript">
$(document).ready(function() {
$('.staffname').select2({
dropdownParent: $('#addteamcompomodal')
allowClear: true,
placeholder: 'Type to Search for Staff',
ajax: {
url: 'ajax/getdata_staffteamcomp.php',
dataType: 'json',
delay: 250,
processResults: function (data) {
return {
results: data
};
},
cache: true
}
});
</script>
What should I be doing wrong? Thank you in advance.
So the problem when I click on submit it gives me this + no data is inserted into the db :
Screen shot of modal
I don't see any errors in the console.
my ajax code :
<script type="text/javascript">
$(document).ready(function () {
$("#lmao").on("submit", function(e) {
var postData = $(this).serializeArray();
var formURL = $(this).attr("action");
$.ajax({
url: formURL,
type: "POST",
data: postData,
success: function(data, textStatus, jqXHR) {
$('#add_data_Modal .modal-header .modal-title').html("Result");
$('#add_data_Modal .modal-body').html(data);
$("#submitForm").remove();
},
error: function(jqXHR, status, error) {
console.log(status + ": " + error);
}
});
e.preventDefault();
});
$("#submitForm").on('click', function() {
$("#lmao").submit();
});
});
</script>
my form + modal :
<div id="add_data_Modal"class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Add mew course</h4>
</div>
<div class="modal-body">
<div class="block-content block-content-narrow">
<form id="lmao"class="js-validation-courses form-horizontal push-10-t" data-async data-target="#add_data_Modal" action="add_c.php" method="post">
<div class="form-group">
<div class="col-xs-12">
<div class="form-material">
<input class="form-control" type="text" id="course_title" name="course_title" >
<label for="material-email">course title</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-xs-12">
<div class="form-material">
<input class="form-control" type="text" id="course_cord"name="course_cord" >
<label for="material-email">Course cord</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-xs-12">
<div class="form-material">
<input class="form-control" type="text" id="course_hours"name="course_hours" >
<label for="material-email">hours </label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-xs-12">
<div class="form-material">
<input class="form-control" type="text" id="course_price" name="course_price" >
<label for="material-email">price</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-9">
<button class="btn btn-sm btn-primary" name="add_course" id="submitForm" type="submit">Add course</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
I have the add query in a separate file called add_c.PHP
so I have tried several answers from a lot of questions but I didn't manage to get something useful.
The PHP code in add_c.php :
Php code - pastebin
My knowledge is very small about ajax so I appreciate any help.
I tried your code, and the php file retrieves this post array
Array ( [course_title] => test [course_cord] => tet [course_hours] => tet [course_price] => tt )
your php code:
if(isset($_POST['add_course'])) {
add_course nevers gets posted, so it will never come into the if. Another issue is your form gets submitted twice the way you build it.
Change the button type into button, not submit. You already have a event listener on the button ;)
<button class="btn btn-sm btn-primary" name="add_course" id="submitForm" type="button">Add course</button>
I'm having an issue passing input and textarea values from a form in modal to AJAX so I can pass them to php.
The input field and textarea values are coming up blank when I submit the form in modal and jquery is not displaying any errors. It simply POST an empty (blank) string for each element.
for example, it shows something like this.
subject=&content=
in the dialog box, I load a name and an ID and both POST successfully but the input values which are not loaded with the form, are not being sent. I understand that the name and ID do POST because they are loaded on DOM along with the form but I do not know how to pass values that are not loaded along with the form in modal.
This is a sample of the form.
<?php while($row = mysqli_fetch_array($res)){
//row data for each post
$r_id = $row['r_id'];
$name = $row['name'];
?>
<div id="m_f_box" class="modal fade msg-box-custom" tabindex="-1" data-width="400">
<div class="modal-header-custom">
<h5 class="modal-title">Send to <span><?php echo $name ?></span></h5>
</div>
<form id="user_m_form" name="user_m_form" action="#" method="POST">
<div class="modal-body">
<div class="row">
<div class="col-md-12">
<div id="s_row" class="col_full">
<h5>Asunto</h5>
<input id="m_subject" name="m_subject" class="required sm-form-control" type="text" maxlength="40"></input>
</div>
<div id="m_row" class="col_full">
<h5>Mensaje</h5>
<textarea id="m_content" name="m_content" class="required sm-form-control" type="text"></textarea>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="submit" id="send_m" name="user_m_form" class="button button-mini button-blue" value="<?php echo $r_id ?>">Send</button>
<button type="button" id="can_m" name="can_m" data-dismiss="modal" class="button button-mini button-blue">Cancel</button>
</div>
</form>
</div>
<?php } ?>
This is a sample of the jquery/ajax call.
<script type="text/javascript">
$(document).on('click', '#send_m', function(e){
e.preventDefault();
var r_id = $('#send_m').val();
var subject = $('#m_subject').val();
var content = $('#m_content').val();
//var form_data = $('#user_m_form').serialize(); test with serialized form data.
//alert(subject); test what is being sent as an alert
//console.log(subject); test what is being sent to view in console
$.ajax({
type:'POST',
url:'test_file.php',
data: { r_id : recipient_id,
subject : subject,
content : content,
},
cache: false,
success:function(res){
//do some stuff
}
});
return false;
});
</script>
Any ideas on why and/or how to POST these values?
Thanks in advance,
Note: Excuse the formatting for the HTML portion. It's hard to properly format everything using a phone.
Note: If you are trying to define multiple modal using while loop,
Your HTML
<div id="m_f_box" class="modal fade msg-box-custom" tabindex="-1" data-width="400">
<div class="modal-header-custom">
<h5 class="modal-title">Send to <span><?php echo $name ?></span></h5>
</div>
<form id="user_m_form" name="user_m_form" action="#" method="POST">
<div class="modal-body">
<div class="row">
<div class="col-md-12">
<div id="s_row" class="col_full">
<h5>Asunto</h5>
<input id="m_subject_<?php echo $row['r_id']; ?>" name="m_subject" class="required sm-form-control" type="text" maxlength="40"></input>
</div>
<div id="m_row" class="col_full">
<h5>Mensaje</h5>
<textarea id="m_content_<?php echo $row['r_id']; ?>" name="m_content" class="required sm-form-control" type="text"></textarea>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="submit" id="<?php echo $row['r_id']; ?>" name="user_m_form" class="button button-mini button-blue send" value="<?php echo $r_id ?>">Send</button>
<button type="button" id="can_m" name="can_m" data-dismiss="modal" class="button button-mini button-blue">Cancel</button>
</div>
</form>
</div>
<?php } ?>
Your Script
<script type="text/javascript">
$(document).on('click', '.send', function(e){
var r_id = (this).attr('id');
var subject = $('#m_subject'+id).val();
var content = $('#m_content'+id).val();
//......
//.......
});
</script>
Note: You just need to understand the JQuery selector (ID and Class). See the difference between #ID and .Class selectors in Jquery.
You are generating multiple Modal with same ID and try to get those field values using IDs (selector). But the IDs for each modal input fields must be unique to get the value. and for on click operation the button must have unique ID or you just need to define class as above code has.
See how I have defined class for click button and getting ID for each modal. Modal must be unique in the sense of their field.
Example done POST request with Ajax call on ModalBox
My View page code:
<div class="panel panel-default">
<div class="panel-body">
<p class="text-danger">There are total <span class="text-bold">{{ sizeof($data) }}</span> Departments.</p>
<table class="table" id="department-data">
<tr>
<th>#</th>
<th>Name</th>
<th>Edit</th>
<th>Delete</th>
<th>Manage</th>
</tr>
<?php $order = 0; ?>
#foreach($data as $key)
<tr>
<td>{{ ++$order }}</td>
<td id="department_name">{{ $key->title }}</td>
<td>
<a href="#">
<button class="btn btn-default edit-department" data-toggle="modal" data-target="#updateDepartmentModel" id="{{ $key->id }}"><i class="fa fa-pencil"></i></button>
</a>
</td>
<td>
<button id="{{ $key->id }}" class="btn btn-danger delete-btn" data-toggle="modal" data-target="#delete-department"><i class="fa fa-trash"></i></button>
</td>
<td>
<a href='program/{{ $key->id }}' id="delete-id">
<button class="btn btn-primary"><i class="fa fa-cog"></i></button>
</a>
</td>
</tr>
#endforeach
</table>
</div>
</div>
Modals on the same page:
<!-- Add New Department Modal -->
<div class="modal fade" id="newDepartmentModel" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Add New Department</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<form action="{{ url('department/create') }}" method="POST">
{{ csrf_field() }}
<div class="modal-body">
<div class="form-group">
<label for="title">Department Name:</label>
<input type="text" class="form-control" id="title" name="title">
</div>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-primary">Add</button>
<button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
</div>
</form>
</div>
</div>
</div>
<!-- ./Model -->
<!-- Update Department Modal -->
<div class="modal fade" id="updateDepartmentModel" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Update a Department</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<form>
<div class="modal-body">
<div class="form-group">
<label for="old-title">Department Name:</label>
<input type="text" class="form-control" id="old-title" name="title">
</div>
</div>
<div class="modal-footer">
<button type="submit" id="edit-dept" class="btn btn-primary edit-dept">Edit</button>
<button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
</div>
</form>
</div>
</div>
</div>
<!-- ./Model -->
Here is Script:
<script type="text/javascript">
var token = '{{ Session::token() }}';
var url = '{{ route('department/edit') }}';
var dept_id = '';
// Setup Ajax
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
// Update the department
$(document).on('click', '.edit-department', function () {
var department_id = $(this).attr('id');
console.log(department_id);
dept_id = department_id;
$.get('department/'+department_id, function (data) {
$('#old-title').val(data.title);
console.log(data);
});
});
// Handle Post Request (Edit Department)
$(document).ready(function () {
$('#edit-dept').click(function () {
$.ajax({
method: 'POST',
url: url,
data: { id: dept_id, title: $('#old-title').val(), _token: token},
dataType: 'JSON'
}).done(function (data) {
//console.log(data);
});
});
});
</script>
Routes is:
Route::post('department/edit', function (\Illuminate\Http\Request $request){
$id = $request->input('id');
$title = $request->input('title');
DB::table('departments')->where('id', $id)->update(array('title' => $title));
return redirect('department')->with('alert_info', 'Department was successful Update!');})->name('department/edit');
Put this line on page Head:
<!-- csrf for meta -->
<meta name="csrf-token" content="{{ csrf_token() }}" />
And in Controller Create and Get routes:
Route::post('department/create', 'DepartmentController#store');
Route::get('department/{id}', 'DepartmentController#showById');
And these definitions:
public function store(StoreDepartmentPostRequest $request)
{
$department = new Department;
$department->title = $request->input('title');
$department->timestamps = time();
$department->save();
return redirect('department')->with('alert_success', 'Department was successful added!');
}
public function showById($id){
$data = Department::findOrFail($id);
return response()->json($data);
}
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);
});
});
});