I'm trying to create a modal popup that contains a confirmation delete button when I press delete on my index view, that button contains the ID of the object you are going to delete, I get it with jquery and replace the form action with it.
My problem is replace it not just replacing the placeholder ID on my action form, its replacing whole route with something like http://localhost/project/public/logout.
Delete button on my index.blade.php:
<button type="button" class="btn btn-outline-danger" data-toggle="modal" data-target="#deleteModal" data-id="{{$category->id}}"><i class="far fa-trash-alt"></i> Delete</button>
Form on modal:
<form action="{{route('categories.destroy', ['id' => 'placeHolderId'])}}" method="POST">
#csrf
#method('DELETE')
<button type="button" class="btn" data-dismiss="modal">Cancel</button>
<button class="btn btn-danger" type="submit">Delete</button>
</form>
Jquery function that replaces the placeholderId on form action attribute:
$(document).ready(function() {
$('#deleteModal').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget);
var placeHolderId = button.data('id');
var modal = $(this);
var action = $('form').attr('action');
modal.find('form').attr("action", action.replace('placeHolderId', placeHolderId));
});
});
Result:
<form action="http://localhost/project/public/logout" method="POST">
Expected result:
<form action="http://localhost/project/public/manager/categories/1" method="POST">
give the submit form another id, and on jQuery find the form by that Id, cause right now, its gettign the first form on the DOM and thats logout form
basically put id="categoriesForm" and on jQuery $("#categoriesForm").submit()
$(document).ready(function() {
$('#deleteModal').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget);
var placeHolderId = button.data('id');
var modal = $(this);
var action = $('#categoryForm').attr('action');
modal.find('#categoryForm').attr("action", action.replace('placeHolderId', placeHolderId));
});
});
or what you can do, is capture when the form is submit, then prevent the default, do the confirmation pop up and then submit
<form action="{{route('categories.destroy', ['id' => 'placeHolderId'])}}" method="POST" id="categoryForm">
#csrf
#method('DELETE')
<button type="button" class="btn" data-dismiss="modal">Cancel</button>
<button class="btn btn-danger" type="submit">Delete</button>
</form>
$(document).ready(function() {
$("#categoryForm").on('submit', function(e) {
e.preventDefault();
var confirmDelete = confirm("Are you sure you want to delete this category!");
if(confirmDelete) {
$(this).submit()
}
})
});
Related
I want to get clickMe buttons value to confirm thing's from users by showing them confirm's modal.
Button:
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<button type="submit" class="btn btn-primary" value="clickMeValue" name="clickMe">Click Me</button>
</form>
PHP:
<?php
if (isset($_POST['clickMe'])){
echo "<script>$(document).ready(function(){
$('#confirmModal').modal('show');
});</script>"; // showing modal from php
//after shoeing modal from php
if(isset($_POST['confirm'])){
echo $_POST['confirm'];
echo "<br/>";
echo $_POST['clickMe']; //this is not echoing
}
}
?>
How can i get clickMe value inside of bootstraps modals confirm thing.
Add an ID to the button and then here is how you can access the value of the clicked button ..
<button type="submit" id="my-button" class="btn btn-primary" value="clickMeValue" name="clickMe">Click Me</button>
And then ..
$(document).ready(function() {
$('#my-button').click(function() {
var the_value = $(this).val();
alert(the_value);
});
});
How do you show/hide content in a form with jquery? When I click this button it just wants to submit.
$(document).ready(function(){
$( ".hide1" ).toggle();
$( ".show_next_div" ).click(function() {
$( ".hide1" ).toggle();
});
});
<form method="post" action="form_submit_action.php">
<button class="btn btn-primary show_next_div">Next Step</button>
<div class="hide1">
<p>something here hidden, now show with button press</p>
</div>
</form>
just add type="button" to the button tag to stop the submit action
<form method="post" action="form_submit_action.php">
<button type="button" class="btn btn-primary show_next_div">Next Step</button>
<div class="hide1">
<p>something here hidden, now show with button press</p>
</div>
</form>
Note : there is another option like stop event or return false onsubmit at the form
A button usually triggers the form action unless you override it.
So it is better to use input type="button"
Use the below in the form code - This should work !
<form method="post" action="form_submit_action.php">
<input type = "button" class="btn btn-primary show_next_div" value="Next Step" ></input>
<div class="hide1">
<p>something here hidden, now show with button press</p>
</div>
</form>
You can stop the propagation and default action of the click event in the event listener closure.
$(".show_next_div").click(function(e) {
e.preventDefault();
e.stopPropagation();
$(".hide1").toggle();
});
I have a form that can delete records from a MySql database using ajax and jQuery. I'm trying to get the jQuery to only select the relevant record passed to it and not just delete the top row of records which it does at the moment. I think I need to get
<div class="'.$id.'">
from my form and make it a data attribute that can be selected. Hope I'm making myself clear. Many thanks.
<script type="text/javascript">
//Delete Review
$(document).ready(function(){
//Need to get $id here.
$(".deleteReview").click(function (e) {
e.preventDefault();
var username = $("#username").val();
var film_id = $("#film_id").val();
var id = $("#id").val();
$.post('ajax_deleteReview.php', {
username: username,
film_id: film_id,
id: id
}, function(data) {
$("#message").html(data);
$("#message").hide();
$("#message").fadeIn(500);
$("#message").fadeOut(2500);
});
return false;
});
});
</script>
<form>
<div class="'.$id.'">
<input type="hidden" id="username" name="username" value="'. $username.'">
<input type="hidden" id="film_id" name="film_id" value="'.$film_id .'">
<input type="hidden" id="id" name="id" value="'.$id .'">
<button type="submit" id="" class="btn btn-danger btn-xs pull-right">delete</button>
</form>
</div>
I would recommend you to use data-* prefixed attributes to persists your data.
HTML
<div data-id="'.$id.'" data-username="'. $username.'" data-filmid="'.$film_id .'" class="deleteReview">
<button type="submit" id="" class="btn btn-danger btn-xs pull-right">delete</button>
<div >
Then you can fetch it using .data(key)
Return the value at the named data store for the first element in the jQuery collection, as set by data(name, value) or by an HTML5 data-* attribute
Script
//Delete Review
$(document).ready(function(){
//Need to get $id here.
$(".deleteReview").click(function (e) {
e.preventDefault();
var username = $(this).data("username");
var film_id = $(this).data('filmid');
var id = $(this).data('id');
......
return false;
});
});
First in your PHP do something like this, you add the data attributes to your delete button since you are not actually submitting a real form. I also moved the deleteReview class to the button.
print '<button type="submit" class="deleteReview" data-id="'.$id.'" data-film-id="'.$film_id.'" data-username="'.htmlentities($username).'" class="btn btn-danger btn-xs pull-right">delete</button>';
Then, in your jQuery, update click event callback like so:
$(".deleteReview").click(function (e) {
e.preventDefault();
var username=$(this).data('username');
var film_id=$(this).data("film-id");
var id=$(this).data('id');
/* the rest of your code */
The data attributes become accessible easily with jQuery using $(this).data('some-attribute-name')
I have a problem in my codes. So the idea is I have two modals that triggered when user Click OK or Fail
So when the user click ok the modal is
<div class="modal-body">
This is the confirmation window to PASS all the Quality Control for, <br/><br/>
Project : <b>'.$row['PROJECT_NAME'].'</b><br/>
HeadMark : <b>'.$row['HEAD_MARK'].'</b><br/>
ID : <b>'.$row['ID'].'</b>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="button" name="acceptButton" id="acceptButton" class="btn btn-success">Confirm PASS!</button>
and when user click FAIL its still the same body but with different button action,
<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>
and my jQuery corresponded with those buttons are,
$('#acceptButton').click(function() {
var myData = $('#data_field');
$.post("acceptClass.php",{
acceptProjectName : myData.attr("data_project"),
acceptHeadMark : myData.attr("data_headmark"),
acceptId : myData.attr("data_id")
},function callback(result){
window.location = "update_fabrication_qc_pass.php";
});
});
$('#rejectButton').click(function() {
var myData = $('#data_field');
$.post("rejectClass.php",{
projectName : myData.attr("data_project"),
headMark : myData.attr("data_headmark"),
id : myData.attr("data_id"),
reasonReject : $("#reasonForRejection").val()
},function callback(result){
window.location = "update_fabrication_qc_pass.php";
});
});
Variables that are to be passed are stored in the hidden input,
echo "<input type='hidden' data_project='$row[PROJECT_NAME]' data_headmark='$row[HEAD_MARK]' data_id='$row[ID]' id='data_field'></input>";
So the problem is, all the buttons only work with the (#rejectButton). So even if I click accept button, only the action in the rejectClass.php are applied to the database. It supposed to be when user click acceptButton, actions in the acceptClass.php that are applied.
Please help me, im so confused.
I'm setting up a form with a "Submit" button and a "Preview" button.
Everything is working fine with the submit button. However, the Preview button does indeed open up a new page with the preview but it also submits to the same preview on the actual form page, which is not what I intend, since the user loses all working data.
The JQuery code to allow for this is the following:
<script type="text/javascript">
jQuery(document).ready(function ($) {
$( ":button" ).click(function(){
var url = $(this).attr('data-url');
if(url=="{{ URL::to('customdownloadpage/preview') }}") {
$('#formCustom').target = "_blank";
}
$('#formCustom').attr('action',url);
$('#formCustom').submit();
});
});
</script>
The HTML for the buttons is the following:
<button data-url="{{ URL::to('customdownloadpage/new') }}" class="btn btn-primary">I'm Finished</button>
<button data-url="{{ URL::to('customdownloadpage/draft') }}" class="btn btn-primary">Save Draft</button>
<button style="float:right" data-url="{{ URL::to('customdownloadpage/preview') }}" class="btn btn-default">Preview</button>
Why is the form submitting twice (on a _blank page and on the self)?
Try preventing the default submission:
$( ":button" ).click(function(e){
var url = $(this).attr('data-url');
if(url=="{{ URL::to('customdownloadpage/preview') }}") {
$('#formCustom').target = "_blank";
e.preventDefault();
}
$('#formCustom').attr('action',url);
$('#formCustom').submit();
});
Make the preview button an anchor tag instead of a button. Buttons always seem to serve as submit triggers inside forms, so if you use a tags with the btn class you get the looks of a button without all the events.