I try to pass the 'data-id' from the code below:
<a data-toggle="modal" data-id="10" class="passingID">
<button type="button" class="btn btn-primary btn-sm" data-toggle="modal" data-target="#editkaryawan">
<i class="fas fa-pencil-alt"></i> Edit</button>
</a>
To an input form values here:
<div class="modal-body">
<form method="post" action="tambahkaryawan.php">
<input type="hidden" class="form-control" name="idkl" id="idkl" value="">
</form>
</div>
This is the jquery i use:
<script>
$(document).on("click", ".passingID", function () {
var ids = $(this).data('id');
$(".modal-body #idkl").val( ids );
});
</script>
I usually use .attr for data-* attributes and it works.
$(this).attr('data-id');
it is quite simple to pass a value or ID check this out
with this button, I will trigger the Modal and also pass the DATA ID which is being generated dynamically from PHP you can also make this a title or any other info needed.
<button data-id="<? echo $note['id'] ?>" onclick="$('#dataid').val($(this).data('id')); $('#showmodal').modal('show');" >click me</button>
My modall is called showmodal as you can see
in the modal's body add the following code
<input type="text" name="dataid" id="dataid" value=""/>
this will show the value from the button.
I hope this helps someone
You can also try like this, by using Jquery to show modal poup
$(".passingID").click(function () {
var ids = $(this).attr('data-id');
$("#idkl").val( ids );
$('#myModal').modal('show');
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
<button type="button" class="btn btn-info btn-lg passingID" data-id="10">Open Modal</button>
<!-- Modal -->
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<form method="post" action="tambahkaryawan.php">
<input type="text" class="form-control" name="idkl" id="idkl" value="">
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
Related
I want to delete and update table row with modal by using ajax technique
in
laravel 5.7 but I m quite naive with ajax.
I would be appreciated if anyone helps and explain how ajax send/get data in
controller and is there any difference between using ajax in PHP and
laravel.?
this is my table
<table class="table table-striped">
<tr>
<th>ID</th>
<th>Title</th>
<th>Description</th>
</tr>
#foreach($project as $pro)
<tr>
<td>{{$pro->id}}</td>
<td>{{$pro->title}}</td>
<td>{{$pro->description}}</td>
<td>
<button class="btn btn-info"
data-toggle="modal" data-target="#edit">Edit</button>
<button class="btn btn-danger"
data-toggle="modal" data-target="#dlt">Delete</button>
</td>
<--------------------->
<div class="modal fade" id="edit" tabindex="-1" role="dialog" aria-
labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<form method="post" action="">
#method('PATCH')
#csrf
<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">Modal title</h4>
</div>
<div class="modal-body">
<div class="form-group">
<label for="title">Title</label>
<input type="text" class="form-control" name="title" >
</div>
<div class="form-group">
<label for="description">Description</label>
<input type="text" class="form-control"
name="description">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-
dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Save
changes</button>
</div>
</form>
</div>
</div>
</div>
this is my modal code
<div class="modal fade" id="dlt" 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">Adding
Project</h4>
</div>
<form method="post" action="">
#method('delete')
#csrf
<div class="modal-body">
<p class="text-center">
Are you sure, you want to delete this.?
</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-
dismiss="modal">No, Cancel</button>
<button type="submit" class="btn btn-info">Yes,
Delete</button>
</div>
</form>
</div>
</div>
</div>
my route
Route::resource('projects', 'ProjectsController');
There no difference between using ajax in PHP and laravel.
html adjustment, add this meta inside your html head.
<meta name="csrf-token" content="{{ csrf_token() }}" />
Update your delete button and add a hidden field inside your delete modal body.
<button class="btn btn-danger btn-delete-project" data-toggle="modal" data-target="#dlt" data-project-id="{{ $pro->id}}">Delete</button>
<input type="hidden" name="project_id" id="project_id" value="">
<button type="submit" class="btn btn-info delete-project-confirm">Yes, Delete</button>
Get Your deleted row,
<tr data-row-id="{{ $pro->id }}">
</tr>
Define your ajax function, there are many defferent technique you can do it. A simple one below.
/**
* project delete confirm modal
*/
$(document).on('click', '.btn-delete-project', function (e) {
e.preventDefault();
var projectId = $(this).data('project-id');
$('#dlt #project_id').val(projectId);
});
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
/**
* ajax call
*/
// give your delete button id
$(document).on('click', '.delete-project-confirm', function (e) {
e.preventDefault();
var projectId = $('#dlt #project_id').val();
var deletedRow = $('tr[data-row-id="' + projectId + '"]');
$.ajax({
type: 'delete',
url: '/project/' + projectId,
success: function () {
$('#dlt').modal('toggle');
deletedRow.remove();
// toastr.success('Order Has Been Deleted Successfully.');
},
error: function(XMLHttpRequest) {
// toastr.error('Something Went Wrong !');
}
});
});
In your ProjectController destroy() method,
public function destroy($id)
{
// dd($id);
$project = Project::findOrFail($id);
// dd($project);
$project->delete();
}
This is my code, I was supposed to get the inputted value in db and php tag isnt working.. It is not also redirecting to "myorders.php"; My form inside is not working.. Thank you.. Really need some help :(((
<div class="container">
<form method="post">
<h2>Enter your Pin Number</h2>
<!-- Trigger the modal with a button -->
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal</button>
<!-- Modal -->
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Enter your Pin Number</h4>
</div>
<div class="modal-body text-center">
<input type="password" id="first-name" required="required" class="form-control" name="pin">
</div>
<div class="modal-footer">
<button type="submit" name="submit3" class="btn btn-default" data-dismiss="modal"><i class="fa fa-key"></i> Confirm Pin</button>
</div>
</div>
</div>
</div>
<!-- START OF PHP -->
<?php include ('connectdb.php');
if(isset($_POST['submit3']))
{
$pin = $_POST["pin"];
$pww = "123";
if ($pin!=$pww) {
echo "WRONG PIN.. TRY AGAIN..";
}
else{
header('Location: myorders.php');
}
} // SUBMIT
?>
</form>
</div>
As per my comment, remove data-dismiss="modal" from your submit button. data-dismiss="modal" is used to close the modal dialog. You already have it here
<button type="button" class="close" data-dismiss="modal">×</button>
I am trying to save data inserted from a modal. I have tried the script in localhost its works perfect. After upload to Production, The "note" field not save into DB table. But my hidden type='hidden' name='leaveID' manage to save in DB table. Not sure why? Please help...
Below is my view file..
<button type="button" class="btn-cs btn-sm-cs" data-toggle="modal" data-target="#myModal" data-html="true" href="#" id="modal"><span class="fa fa-pencil"></span> Note</button>
<!-- Modal -->
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<form action="<?php echo base_url('leave/note'); ?>" id="formid" method="POST" >
<textarea class="form-control" id="note" name="note" ><?=set_value('note')?></textarea>
<input type='hidden' name='leaveID' value="<?= set_value("leaveID", $leave->leaveID) ?>" />
</div>
<div class="modal-footer">
<button type="submit" name="submit" class="btn btn-info " >Add Note</button></div>
</div>
</form>
<script>
$("[data-toggle=modal]").popover({
html: true,
content: function() {
return $('#modal-header').html();
}
});
</script>
</div>
</div><?php } ?>
Manage to solve the issue. Just has some permission issue.
Why is this not working, because of a problem in Bootstrap or ajax?
This is my modal update:
<div class="container">
<!-- Modal update -->
<div class="modal fade" id="myModals<?php echo $row->id ;?>" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Edit Data</h4>
</div>
<div class="modal-body">
<form>
<div class="form-group">
<label for="user">Username:</label>
<input type="user" class="form-control" id="users<?php echo $row->id ;?>" value="<?php echo $row->username ;?>">
</div>
<div class="form-group">
<label for="password">Password:</label>
<input type="password" class="form-control" id="passwords<?php echo $row->id ;?>" value="<?php echo $row->password ;?>">
</div>
<button onclick='update("<?= $row->id ?>")' type="submit" class="btn btn-default">Update</button>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
And this is my ajax:
function update(id){
var users = $("#users"+id).val();
var passwords = $("#passwords"+id).val();
$.ajax({
dataType:'html',
url:"update.php",
type:"POST",
data:"users="+users+"&passwords="+passwords+"&id="+id,
}).done(function(data){
load();
$('back-drop').remove();
});
}
Why is this not working when I submit the change, the modal form is not closed completely like this:
Please recheck your selector in the hide() syntax. You've forgotten the # in front.
Change this:
$('myModals'+id).modal('hide');
To this:
$('#myModals'+id).modal('hide');
For checking such issues, you should use Google Chrome and press CTRL + Shift + J. The possible syntax errors will be shown in the console.
I want to display a number in a bootstrap modal after i use post method to submit a form .
But the bootstrap modal cannot show out the number ($_POST[number]).
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<label class="control-label" >Number</label>
<div class="controls">
<input class="form-control" name="number" type="text">
<p class="help-block">Enter a number</p>
</div>
<button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal" type="submit">submit</button>
<?php
$number = $_POST['number'];
?>
<div class="modal fade" id="myModal" 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">
number
</h4>
</div>
<div class="modal-body">
<?php
echo $number;
?>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default"
data-dismiss="modal">close
</button>
<button type="button" class="btn btn-primary">
ok
</button>
</div>
</div>
</div>
</div>
How can i do it?
A few observations and flaws with your approach:
Fix HTML Errors (e.g. you are not properly closing your form, add: </form>
PHP is a server-side scripting language.
Bootstrap cannot function without Jquery which is a client-side scripting language.
You cannot trigger a Bootstrap modal on a form submission button, unless its Ajax driven!
Show the Modal after the form has been submitted (Page load) see jsfiddle example
Complete code:
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.js"></script>
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
</head>
<body>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<label class="control-label" >Number</label>
<div class="controls">
<input class="form-control" name="number" type="text">
<p class="help-block">Enter a number</p>
</div>
<button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal" type="submit">submit</button>
</form>
<?php if (isset($_POST["number"])) : ?>
<!-- Show the Modal -->
<div class="modal fade" id="myModal" 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">
number
</h4>
</div>
<div class="modal-body">
<?php
echo htmlspecialchars($_POST["number"]);
?>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default"
data-dismiss="modal">close
</button>
<button type="button" class="btn btn-primary">
ok
</button>
</div>
</div>
</div>
</div>
</body>
</html>
<script type="text/javascript">
$(window).load(function(){
$('#myModal').modal('show');
});
</script>
<?php else : ?>
<!-- Do other stuff here -->
<?php endif; ?>
You need to add one more button ( input submit button ). that button will send the post request to self and update the variable $number.
First of all, you didn't close your form tag and you are trying to use submit button as a button for your modal...I don't know if that is gonna work.
Close your form tag and add input field of submit type...and then add a button for modal..that will work.
Here is the working code:
<html>
<head>
<link rel="stylesheet" href="../includes/css/bootstrap-theme.min.css">
<link rel="stylesheet" href="../includes/css/bootstrap.min.css">
</head>
<body>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<label class="control-label" >Number</label>
<div class="controls">
<input class="form-control" name="number" type="text">
<p class="help-block">Enter a number</p>
</div>
<input type="submit" value="submit">
</form>
<?php
$number = $_POST['number'];
?>
<button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal" type="submit">Modal</button>
<div class="modal fade" id="myModal" 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">
number
</h4>
</div>
<div class="modal-body">
<?php
echo $number;
?>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default"
data-dismiss="modal">close
</button>
<button type="button" class="btn btn-primary">
ok
</button>
</div>
</div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="../includes/js/bootstrap.min.js"></script>
</body>
</html>
Change <script> and <link> tag source according to your needs.
u can remove class model and fade from 'myModel' class div or can write style for display:block. I tried your code and checked in my browser and removed above classes. I hope you get the problem. You can also view your number with viewing page source.
I also noticed something about this Modal Problem, remove type="button" from your <button> and the code runs.
Would have posted this as comment but I don't have enough rep yet.
This is because the PHP code gets executed server side and is executed before the HTML is loaded, so the $number is an empty string.