Laravel 8 updating Data using modal - modal-dialog

recently m learning how to update data by using modal.i can successfully inserted data by modal but can not update before that when i press on edit button my modal pops up without fetched data.
here is my blade file with script-
<div>
<h5 style="text-align: center">Modal Practice</h5>
</div>
{{-- ................................... --}}
<div class="container">
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#staticBackdrop">
Add data
</button>
{{-- #dd($alldata) --}}
<table id="myTable" class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">First</th>
<th scope="col">Last</th>
<th scope="col">Common Name</th>
<th scope="col">Action</th>
</tr>
</thead>
<tbody>
#foreach ($alldata as $key => $data)
<tr>
<th scope="row">{{ $key + 1 }}</th>
<td>{{ $data->first_name }}</td>
<td>{{ $data->last_name }}</td>
<td>{{ $data->common_name }}</td>
<td>
<a href="#" class="btn btn-warning btn-sm edit" data-bs-toggle="modal"
data-bs-target="#editModal">Edit</a>
Delete
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
<!-- Add Modal -->
<div class="modal fade" id="staticBackdrop" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1"
aria-labelledby="staticBackdropLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="staticBackdropLabel">Modal title</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<form action="{{ route('savedata') }}" method="POST" class="row g-3">
<div class="modal-body">
#if (session()->has('success'))
<div class="alert alert-success">
{{ session()->get('success') }}
</div>
#endif
#csrf
<div class="col-md-6">
<label for="inputEmail4" class="form-label">First Name</label>
<input type="text" class="form-control" name="first_name">
</div>
<div class="col-md-6">
<label for="inputEmail4" class="form-label">Last Name</label>
<input type="text" class="form-control" name="last_name">
</div>
<div class="col-md-6">
<label for="inputEmail4" class="form-label">Common Name</label>
<input type="text" class="form-control" name="common_name">
</div>
<div class="col-12">
<button type="submit" class="btn btn-primary">Save</button>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Understood</button>
</div>
</form>
</div>
</div>
</div>
{{-- end Add modal --}}
<!-- Edit Modal -->
<div class="modal fade" id="editModal" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1"
aria-labelledby="staticBackdropLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="staticBackdropLabel">Modal title</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<form action="#" method="POST" id="editForm" class="row g-3">
<div class="modal-body">
#csrf
#method('put')
<div class="col-md-6">
<label for="inputEmail4" class="form-label">First Name</label>
<input type="text" class="form-control" id="fname" name="first_name">
</div>
<div class="col-md-6">
<label for="inputEmail4" class="form-label">Last Name</label>
<input type="text" class="form-control" id="lname" name="last_name">
</div>
<div class="col-md-6">
<label for="inputEmail4" class="form-label">Common Name</label>
<input type="text" class="form-control" id="cname" name="common_name">
</div>
<div class="col-12">
<button type="submit" class="btn btn-primary">Update</button>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
</div>
</form>
</div>
</div>
</div>
{{-- End Edit Modal --}}
<!-- JavaScript Bundle with Popper -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.0.2/dist/js/bootstrap.bundle.min.js"
integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous">
</script>
<script src="https://cdn.datatables.net/1.10.25/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.10.25/js/dataTables.bootstrap5.min.js"></script>
<script src="https://cdn.datatables.net/1.10.25/js/dataTables.jqueryui.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var table = $('#myTable').DataTable();
table.on('click', '.edit', function() {
$tr = $(this).closest('tr');
if ($($tr).hasClass('child')) {
$tr = $tr.prev('.parent');
}
var data = table.row($tr).data();
console.log(data);
$('#first_name').val(data[1]);
$('#last_name').val(data[2]);
$('#common_name').val(data[3]);
$('#editForm').attr('action', '/savedata' + data[0]);
$('#editModal').modal('show');
});
});
</script>
here is controller-
public function update(Request $request,$id)
{
$alldata = DataSave::find($id);
$alldata->update([
'first_name'=>$request->first_name,
'last_name'=>$request->last_name,
'common_name'=>$request->common_name
]);
return redirect()->route('index')->with('success', 'Update Successful');;
}
}
and here is my route-
Route::get('/index',[SaveController::class,'index'])->name('index');
Route::post('/save',[SaveController::class,'savedata'])->name('savedata');

to get a modal pop-up with fetched data i use something like (look id=""):
<!-- Add Modal -->
<div class="modal fade" id="editModal{{ $id }}" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1"
aria-labelledby="staticBackdropLabel" aria-hidden="true">
</div>
And button link to modal should be like (look data-target=""):
<td>
<a href="#" class="btn btn-warning btn-sm edit" data-toggle="modal"
data-target="#editModal{{ $id }}">Edit</a>
Delete
</td>

Related

stop reloading the entire page after Modal from submission in razor pages

I have a edit page like this
i have a comment button in this page,which is a modal form ,where users can leave their comment
when i submit the Comment form,the entire page is reloading and im losing the edit page content.
Im handling edit page content and comment box in two different forms.
So when i submit the comment form,the modal should open as it is and also the edit page content should not be gone.
This is my modal
<div class="modal modal_outer right_modal fade" id="information_modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel2">
<div class="modal-dialog" role="document">
<div class="modal-content ">
<div class="modal-header bg-warning" style="height:50px;padding:10px 5px;font-family:'Delivery';">
<h4 class="modal-title">Comments:</h4>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body get_quote_view_modal_body">
<div class="list-group">
<a href="#" class="list-group-item list-group-item-action">
<div class="d-flex w-100">
<h5><i class="fas fa-user"></i> User 1</h5>
<small class="ml-auto">1 days ago</small>
</div>
<p>This is for testing.Please ignore this.</p>
</a>
</div>
<form asp-page-handler="SubmitChat">
<div class="form-group mt-2">
<label for="TxtAreaChatBox"><b>Leave Your Comments</b></label>
<textarea asp-for="RequestChatBox.Comments" class="form-control" id="TxtAreaChatBox" rows="4"></textarea>
<input id="hdnCommentReqID" type="hidden" asp-for="RequestChatBox.RequestID" value='#Request.Query["RequestID"]' />
</div>
<div class="modal-footer">
<button type="reset" class="btn btn-danger" data-dismiss="modal"><i class="fas fa-window-close mr-2"></i> Cancel</button>
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
</div>
</div>
</div>
------------Edited-------------------
<!---COMMENT BOX STARTS HERE-->
<div class="modal modal_outer right_modal fade" id="information_modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel2">
<div class="modal-dialog" role="document">
<div class="modal-content ">
<div class="modal-header bg-warning" style="height:50px;padding:10px 5px;font-family:'Delivery';">
<h4 class="modal-title">Comments:</h4>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body get_quote_view_modal_body">
<div class="list-group">
#if (Model.ShowRequestChatBox != null){
#foreach (var item in Model.ShowRequestChatBox)
{
<a href="#" class="list-group-item list-group-item-action">
<div class="d-flex w-100">
<h6><i class="fas fa-user"></i> <i>#Html.DisplayFor(modelItem => item.CommentedUser)</i></h6>
<small class="ml-auto">#Html.DisplayFor(modelItem => item.LastModifiedTimeStamp)</small>
</div>
<p>#Html.DisplayFor(modelItem => item.Comments)</p>
</a>
}
}
</div>
<form asp-page-handler="SubmitChat" data-ajax="true" data-ajax-method="post" data-ajax-complete="Completed">
<div class="form-group mt-2">
<label for="TxtAreaChatBox"><b>Leave Your Comments</b></label>
<textarea asp-for="RequestChatBox.Comments" class="form-control" id="TxtAreaChatBox" rows="4"></textarea>
<input id="hdnCommentReqID" type="hidden" asp-for="RequestChatBox.RequestID" value='#Request.Query["RequestID"]' />
</div>
<div class="modal-footer">
<button type="reset" class="btn btn-danger" data-dismiss="modal"><i class="fas fa-window-close mr-2"></i> Cancel</button>
<button type="submit" id="BtnChatSubmit" class="btn btn-primary">Submit</button>
</div>
</form>
</div>
</div>
</div>
</div>
<!--COMMENT BOX ENDS HERE-->
Javascript
function Completed(event) {
if (event.responseText != "") {
alert("Some Error Has Occured.Please Check Your Entered Values.");
} else {
alert("Your Comment Has Been Added Successfully");
}
}
Thanks,
Teena

Form not submitting after form validation

I have a form in a modal window :
<div class="modal fade bs-example-modal-lg" id="vp" >
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title" id="myModalLabel">Create Job</h4>
</div>
<div class="modal-body container-fluid">
<form id="create_job" method="post" action="?create">
<div class="form-group">
<div class="row">
<div class="col-md-4">
<input type="text" class="form-control" placeholder="Job number - Jxxxx" name="job_number">
</div>
<div class="col-md-4">
<input type="text" class="form-control" placeholder="Part number - xxxxxx" name="part_number">
</div>
<div class="col-md-2"><p class="form-control-static text-right"><b>Cable length (m):</b></p></div>
<div class="col-md-2">
<input type="text" class="form-control" placeholder="Cable" value="06" name="cable">
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" name="submitButton" class="btn btn-primary">Submit</button></form>
</div>
</div>
</div>
</div>
This is the validation js:
$(document).ready(function() {
$('#create_vpf_job').formValidation({
framework: 'bootstrap',
excluded: ':disabled',
icon: {
valid: 'glyphicon glyphicon-ok',
invalid: 'glyphicon glyphicon-remove',
validating: 'glyphicon glyphicon-refresh'
},
fields: {
job_number: {
validators: {
notEmpty: {
message: ' '
}
}
},
part_number: {
validators: {
notEmpty: {
message: ' '
}
}
},
cable: {
validators: {
notEmpty: {
message: ' '
}
}
}
}
});
});
I've look online for same problems but I haven't found a solution. The submit button doesn't submit. I'm using formvalidation.io for validation and nothing from their docs helped me.
Also tried adding with no luck
$('#create_job').submit();
Your HTML is bad formatted, and the form is closing after the container div closes, so the submit button doesn't know what to submit. Change it to:
<div id="vp" class="modal fade bs-example-modal-lg">
<div role="document" class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h4 id="myModalLabel" class="modal-title">Create Job</h4>
</div>
<div class="modal-body container-fluid">
<form action="?create" method="post" id="create_job">
<div class="form-group">
<div class="row">
<div class="col-md-4">
<input type="text" name="job_number" placeholder="Job number - Jxxxx" class="form-control" />
</div>
<div class="col-md-4">
<input type="text" name="part_number" placeholder="Part number - xxxxxx" class="form-control" />
</div>
<div class="col-md-2">
<p class="form-control-static text-right">
<b>Cable length (m):</b>
</p>
</div>
<div class="col-md-2">
<input type="text" name="cable" value="06" placeholder="Cable" class="form-control" />
</div>
</div>
</div>
<div class="modal-footer">
<button data-dismiss="modal" class="btn btn-default" type="button">Close</button>
<button class="btn btn-primary" name="submitButton" type="submit">Submit</button>
</div>
</form>
</div>
</div>
</div>
</div>
And it will work. On this plunkr you can check that it's giving a bad-request response, which is good news, because it means the request is being done, and so, the form is being submitted once the correction made

Bootstrap 3 contact modal is not showing up

I have a small contact modal on my website that does not appear. It was working before, but I must have changed something and now it won't work.
I've looked over it multiple times and I can't figure out why it's not working.
HTML:
<span data-toggle="modal">Contact</span>
<div class="modal fade" id="contact" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<form class="form-horizontal">
<div class="modal-header">
<h4>Contact the Developers</h4>
</div>
<div class="modal-body">
<div class="form-group">
<label for="contact-name" class="col-lg-2 control-label">Name:</label>
<div class="col-lg-10">
<input type="text" class="form-control" id="contact-name" placeholder="Full Name">
</div>
</div>
<div class="form-group">
<label for="contact-email" class="col-lg-2 control-label">Email:</label>
<div class="col-lg-10">
<input type="email" class="form-control" id="contact-email" placeholder="you#example.com">
</div>
</div>
<div class="form-group">
<label for="contact-msg" class="col-lg-2 control-label">Message:</label>
<div class="col-lg-10">
<textarea class="form-control" rows="8"></textarea>
</div>
</div>
</div>
<div class="modal-footer">
<a class="btn btn-default" data-dismiss="modal">Close</a>
<button class="btn btn-primary" type="submit">Send</button>
</div>
</form>
</div>
</div>
</div>
You are missing the data-target attribute. Plus, you shouldn't be linking to a page when you want to open a modal ;)
<span data-toggle="modal" data-target="#contact">Contact</span>
Should fix this. See http://www.bootply.com/UED0aevXsG

Bootstrap Modal Form Post/GET doesn't send field values

I have a simple modal form :
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content modal-lg">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="myModalLabel">Address Maintenance</h4>
</div>
<div class="modal-body">
<form class="form-horizontal" role="form" id="maintainAddress" method='GET' action="test.php">
<div class="form-group">
<label for="addressID" class="col-sm-2 control-label input-sm">Address ID</label>
<div class="col-sm-4">
<input type="text" class="form-control input-sm" id="addressID" placeholder="Address ID">
</div>
<label for="houseNo" class="col-sm-2 control-label input-sm">House No</label>
<div class="col-sm-4">
<input type="text" class="form-control input-sm" id="houseNo" placeholder="House No">
</div>
</div>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-default">SAVE</button>
<button type="button" class="btn btn-default" data-dismiss="modal">CANCEL</button>
</div>
</form>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
test.php is :
<?
$addressID=$_GET["addressID"];
$houseNo=$_GET["houseNo"];
echo $addressID . " " . $houseNo;
?>
test.php does get invoked on clicking 'SAVE' button but test.php is unable to receive any field values of addressID and houseNo. Error message is "
Notice: Undefined index: addressID in D:\proto\xampp\htdocs\scorecard\test.php on line 3
Notice: Undefined index: houseNo in D:\proto\xampp\htdocs\scorecard\test.php on line 4
I have tried changing GET to POST but still it doesn't work.
The form-elements should have the name-attribute set. So this:
<input type="text" class="form-control input-sm" id="houseNo" placeholder="House No">
Should be changed to:
<input type="text" class="form-control input-sm" id="houseNo" name = "houseNo" placeholder="House No">

How to align label on same line as form?

I have the following code for login screen using bootstrap. Please help in aligning the label in the same line as the form input field.
<div class="container">
<div class="row">
<div class="col-md-6">
<div>
<form class="form-horizontal">
<fieldset>
<legend>Existing Customer</legend>
<div class="control-group">
<label class="control-label" for="user">User Name:</label>
<div class="control-group">
<input type="text" class="textbox" id="user">
</div>
<label class="control-label" for="pwd">Password:</label>
<div class="controls">
<input type="text" class="textbox" id="pwd">
</div>
<div class="controls">
<input class="btn btn-primary" name="commit" type="submit" value="Log In" />
</div>
</div>
</fieldset>
</form>
</div>
</div>
<div class="col-md-6">
<legend>New Customer</legend>
</div>
Hi use table which solves your alignment issues.
-------------------------------------------------------------
<html>
<head>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-6">
<div>
<form class="form-horizontal">
<fieldset>
<legend>Existing Customer</legend>
<table>
<tr>
<td>
<div class="control-group">
<label class="control-label" for="user">User Name:</label>
</td>
<td>
<div class="control-group">
<input type="text" class="textbox" id="user">
</div>
</td>
</tr>
<tr>
<td>
<label class="control-label" for="pwd">Password:</label>
</td>
<td>
<div class="controls">
<input type="text" class="textbox" id="pwd">
</div>
</td>
</tr>
<div class="controls">
<input class="btn btn-primary" name="commit" type="submit" value="Log In" />
</div>
</div>
</table>
</fieldset>
</form>
</div>
</div>
<div class="col-md-6">
<legend>New Customer</legend>
</div>
</body>
i think you should take css from ui expert and move forward in your project. the page
lay out should be designed as the developer only should be able to use(apply) the css in
the UI layer
Here is the code using Bootstrap 3:
<div class="container">
<div class="row">
<div class="col-md-6">
<form class="form-horizontal" role="form">
<fieldset>
<legend>Existing Customer</legend>
<div class="form-group">
<label class="col-sm-2 control-label" for="user">User Name:</label>
<div class="col-sm-4">
<input type="text" class="textbox form-control" id="user">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label" for="pwd">Password:</label>
<div class="col-sm-4">
<input type="text" class="textbox form-control" id="pwd">
</div>
</div>
<div class="form-group">
<input class="btn btn-primary" name="commit" type="submit" value="Log In" />
</div>
</fieldset>
</form>
</div>
<div class="col-md-6">
<legend>New Customer</legend>
</div>
</div>
and the respective fiddle: http://jsfiddle.net/G2cgf/
Submit a margin-left to the button, like this:
<input class="btn btn-primary" name="commit" type="submit" value="Log In" style="margin-left: 15px;"/>