Iam trying to make update option with modal form, I already made "ADD New" - works fine, but have alot problems with Update - I want to see actual data on modal form to edit this. Can u maybe help me with this one? I need help only with function part.
Thats my code for Add New one:
public function dodajAction()
{
$request = $this->getRequest();
$jsonModel = new JsonModel();
if ($request->isPost()) {
$dane = $request->getPost();
$formDodaj = new FormAdm\OfertaForm();
$formDodaj->setData($dane);
if ($formDodaj->isValid()) {
// dodaj
$this->oferta->dodaj($dane);
$jsonModel->setVariables(['wynik' => true]);
} else {
// w razie błędu renderuj formularz
$viewModel = new ViewModel(['form_dodaj' => $formDodaj]);
$viewModel->setTerminal(true);
$viewModel->setTemplate('admin/oferty/dodaj');
$jsonModel->setVariables(['wynik' => false, 'html' => $this->renderer->render($viewModel)]);
}
}
return $jsonModel;
}
And thats modal form:
<div class="modal fade" id="modalDodajOferte" tabindex="-1" role="dialog" aria-labelledby="modalDodajOferteLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="modalDodajOferteLabel">Dodaj ofertę</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<?=$this->partial('admin/oferty/dodaj', ['form_dodaj' => $this->form_dodaj]) ?>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" id="btnDodajOferte">Dodaj</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Zamknij</button>
</div>
</div>
</div>
</div>
Just an advice from a fellow Pole, PLEASE!!! use english words in your code. This improves readability, especially that later on you might find tools that help you by using defaults if you follow naming standards. e.g. $form->offer->add() is way more readable to anyone on stack overflow, than our native ->oferta->dodaj().
Coming back to the question though, you have not supplied us with any way to see how the update is performed on the system
I believe that
if ($formDodaj->isValid()) {
// dodaj
$this->oferta->dodaj($dane);
$jsonModel->setVariables(['wynik' => true]);
is responsible for sending the actual data to the database, however code for updating that should be completely in a different function, same for viewing.
It sounds more like an XY problem here, so you might need to rethink what you actually need us to help you with.
Related
I have a list (displayed in a tabulated format). The list consists of names of various departments.
The same appears as below:
-------------------------------------------------------
Sr. No. Department Name Action
-------------------------------------------------------
1 Department A EDIT | DELETE
2 Department B EDIT | DELETE
3 Department C EDIT | DELETE
4 Department D EDIT | DELETE
5 Department E EDIT | DELETE
-------------------------------------------------------
Now I am trying to open a single modal when "EDIT" or "DELETE" is clicked. In the modal I want to display the EDIT Form (for the particular Department) or DELETE Form (for the particular Department).
Please Note: I am using CouchCMS so even a custom PHP code can be helpful which can be changed to fit the CouchCMS Purview.
I have generate a clonable template with custom routes using CouchCMS. The tag helps me generate the table (as shown above). Where in the buttons for EDIT and DELETE are also generated. I can easily provide to the button the page id using the of CouchCMS (if that helps).
I am using the native code of bootstrap to popup the Bootstrap v3 modal. Wherein, a single modal opens for every button that is clicked, hence saving me the memory issue that i might run into if I generate as many modals as the rows in the table.
The code for Bootstrap v3 modal that i am referring to is available here.
In CouchCMS we have a edit form (for editing the data queried from the database) where the page_id parameter needs to be set in order to edit that particular data item. I need to set this page_id. But am unable to figure out how to?
Code to generate the Table list:
<table class="table table-striped table-hover">
<thead>
<tr>
<th>Sr. No.</th>
<th>Name</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<cms:pages masterpage='department/department.php' limit='10' paginate='1' show_future_entries='1'>
<tr>
<td><cms:show absolute_count /></td>
<td><cms:show k_page_title /></td>
<td>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-id="<cms:show k_page_id />">
EDIT
</button>
<button type="button" class="btn btn-danger" data-toggle="modal" data-target="#exampleModal" data-id="<cms:show k_page_id />">
DELETE
</button>
</td>
</tr>
</cms:pages>
</tbody>
</table>
Code to open a single modal for every button that is clicked, EDIT or DELETE button:
$('#exampleModal').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget) // Button that triggered the modal
var page = button.data('id') // Extract info from data-* attributes
// If necessary, you could initiate an AJAX request here (and then do the updating in a callback).
var modal = $(this)
modal.find('.modal-title').text('New message to ' + page)
modal.find('.modal-body input').val(page)
})
Code for the modal is:
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
<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="exampleModalLabel">New message</h4>
</div>
<div class="modal-body">
<!-- Edit Form Content Here -->
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">CANCEL</button>
<button type="button" class="btn btn-primary">SAVE</button>
</div>
</div>
</div>
</div>
An edit form in CouchCMS is defined as:
In this form I need to have the page_id=k_page_id set. The k_page_id is available to the button in the table under the data-* (data-id) attribute. If only this data-id could be supplied to the page_id, the rest can be taken forward from there using CouchCMS.
I am also open to the option of using AJAX, if required. But if it is possible without AJAX, it would still be fine.
Any help in this regards will be greatly appreciated. If any clarification or post editing is required or any more inputs are required, I shall do show if someone points out the short coming in my description/code.
Thanks to the community, in advance.
I have to display a content based on their id but get id is not working. But I can see the url that it retrieves the id and it will change if I click the button. Should I force myself to use ajax or there is alternative way to retrieve the id?
I really appreciate any help.
$viewquery= mysqli_query($connection,"SELECT * from orders");
while($row = mysqli_fetch_assoc($viewquery)) {
$id =$row['id'];
$full_name =$row['full_name'];
$email_address =$row['email_address'];
$contact_number =$row['contact_number'];
$address =$row['address'];
$user_id =$row['user_id'];
$reference_number =$row['reference_number'];
$additional =$row['additional'];
$payment_method =$row['payment_method'];
$transaction_status = $row['transaction_status'];
echo' <tr>';
echo '<td>';echo "<a href='#?idrequest=$id'><button type='button'
class='btn btn-primary' data-toggle='modal' data-target='#exampleModalLong'
data-formid='.$id.'>
$reference_number
</button></a>";
echo'</td>';
echo "
<td> $id </td>
<td> $full_name </td>
<td> $email_address </td>
<td> $contact_number </td>
<td> $address </td>
<td> $user_id</td>
<td> $additional </td>
<td> $payment_method </td>
<td> $transaction_status </td>
<td>
<a href='delete.php'>
<i class='material-icons' id='coloricon'>delete </i>
</a>
</td>
</tr>
";
}
?>
<div class="modal fade" id="exampleModalLong" tabindex="-1" role="dialog"
aria-labelledby="exampleModalLongTitle" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLongTitle">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-
label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<?php
include("conn.php");
$idrequest = $_GET['idrequest'];
$query = "SELECT * from orders where id ='$idrequest'";
$result = mysqli_query($connection,$query);
$view = mysqli_fetch_assoc($result);
$details = $view['summary_all'];
echo $details;
?>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-
dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
This code above is for modal which I used bootstrap modal.
I really didn't want to write a post on this as there is so much going on that needs to be fixed but your immediate issue is
<a href='#?idrequest=$id'> .... </a>
This hashtag # or pound sign, will create what is called a URL fragment. Now you have to understand that fragments are client side only and are not sent to the server. So anything following the # will not be sent. Therefore there is no way to access this "pseudo" query string. So at the very least toss that #.
<a href='?idrequest=$id'> .... </a>
If you had error reporting turned on you would see a warning for an undefined index.
<?php
error_reporting(-1);
ini_set('display_errors', '1');
Because $_GET['idrequest'] doesn't exist, in this codes current form.
Beyond that, your SQL is wide open for SQLInjection, as I said in the comment if someone put the URL in with
$_GET['idrequest'] = "1' OR 1=1"
$idrequest = $_GET['idrequest'];
"SELECT * from orders where id ='$idrequest'"
Your SQL query is modified to this:
"SELECT * from orders where id ='1' OR 1=1"
Because 1 is always equal to 1 this "hack" will always return some rows. Therefor always use prepared statements.
Beyond that, if this is publicly accessible any user could simply iterate though your user table to look at orders from other users. These may include personal identifiable information, such as phone and address information. You could add the user_id into this search which should be a foreign key on this table anyway. That user id would then come from whatever system you are using for login (not user entered data). Which would only allow them to see their own orders.
I won't even touch on the styling/formatting issues, with wrapping a button in a link. Or this ad-hoc mixing of HTML and HTML in PHP strings, which make it a real challenge to read the code.
Or this
include("conn.php");
Which should be
require "conn.php";
include and require don't need the () and you should use require if the script won't execute without the included file. This being a DB connection, I would say things won't go as planed if that file is missing.
Cheers.
I am fetching the details of a user according to Id on button click in a popup in laravel but It is showing some error.
This is viewuser.blade.php
<div class="modal fade" id="myModal">
<div class="modal-dialog">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header">
<h4 class="modal-title">Modal Heading</h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<!-- Modal body -->
<div class="modal-body">
<table class="table table-bordered">
<tr>
<td>{{$usersview->id}}</td>
<td>{{$usersview->firstname}}</td>
<td>{{$usersview->filename}}</td>
</tr>
</table>
</div>
<!-- Modal footer -->
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
</div>
</div>
</div>
This is viewparticipant.blade.php in which I have a button View. When the user will click on this button a popup will open and show the details according to Id.
<td>
View
</td>
My Controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Session;
use App\Imageupload;
class ParticipantController extends Controller
{
public function show($id)
{
$usersview = Imageupload::find($id);
return View::make('viewuser', compact('usersview'));
}
}
My Web.php
Route::get('/participant/show/{id}','ParticipantController#show');
The error is showing that unknown variables in modal.
well you only have $usersview in your blade file and you're calling $row->id in viewparticipant.blade.php , which should be the $usersview->id instead
after Edit -->
so you need to make an ajax call to the function you want to get the results. the ajax must contain the id of the user so you should get the id some how. I usually define an on click function and get the id. then on the same click trigger an ajax call, something like this:
$('#user').on('click', function (event) {
$.ajax({
url: '/users?user_id=' + $(this).val(),
success: function (data) {
// parse the data
},
fail: function () {
// do something in case of failure
}
});
});
$(this).val(); is the id here, I assumed you would store that in an input
Try this:
<td>
View
</td>
Because laravel is figuring out where you want to place your $row->id variable, and to my guessing it's not passing the variable correctly in your route.
try
#foreach($usersview as $usersview)
<tr>
<td>{{$usersview->id}}</td>
<td>{{$usersview->firstname}}</td>
<td>{{$usersview->filename}}</td>
</tr>
#endforeach
I have a web app project I've been working on, most of the applications functionality works except for submitting a flag on a resource. I want to use a modal form to submit the data to the database, then on the Flagged view page display all flagged resources Name & Description(from the Resources table) Flag_Reason & Other_Comments(from the Flagged table) I had it working to where it was submitting only the Flag_Reason and Other_Comments and not updating my Resources Table at all. I believe I'm having issues with routes now, because after changing my function to update my Resources table AND create a new Flag entry in the DB I get an error like this
Missing argument 1 for App\Http\Controllers\FlagsController::addFlag()
Here's some of my code, hopefully someone can help me finally figure this out once and for all.
Routes
Route::get('resource', array('as'=>'viewResource', 'uses' => 'ResourceController#resource'));
Route::get('flags', 'FlagsController#index');
Route::post('resource', ['as' => 'resource', 'uses'=>'FlagsController#addFlag']);
///Route::post('resource', ['as' => 'resource', 'uses'=>'FlagsController#postFlag']);///
This route works fine, and only inserts the Flagged table data into the database.
If I modify my route to look like this Route::post('resource/{Resource_ID}', ['as' => 'resource', 'uses'=>'FlagsController#addFlag'])
I receive an error like this
Missing required parameters for [Route: resource] [URI: resource/{Resource_ID}].
Flags Controller
class FlagsController extends Controller
{
public function index()
{
$resources = Resources::where('Flagged', 1)->with('flags')->get();
return view('pages.flags', ['resource' => $resources]);
}
public function addFlag($id)
{
$flag = Flagged::create(Request::all());
$resource = Resources::findOrFail($id);
$resource->update(array('Flagged' => 1));
$resource->flags()->attach([$flag->id]);
dd($resource::all());
return back();
}
//////// This function inserts only the Flagged table data into the Flagged table, It doesnt do what I want it to do, so i've commented it out/////
public function postFlag()
{
$flag = Flagged::create([
'Flag_Reason' => Input::get('reason'),
'Other_Comments' =>Input::get('comments')]);
$flag->save();
\Session::flash('flash_message', 'Flagged!');
return redirect('resource');
}
}
Resource View
...
#foreach($resources as $resource) #foreach ($resource->locations as $location)
<tr>
<td> <a class="btn btn-small btn-default" style="float:right; margin-right:5px;" href="{{ URL::to('resource/addToCart/' .$resource->Resource_ID) }}">+</a> {{ $resource->Name }}</td>
<td>{{ $resource->Description }}</td>
<td>{{ $location->Address }}</td>
<td>{{ $location->City }}</td>
<td>{{ $location->Zip_Code }}</td>
<td>{{ $location->County }}</td>
<td>
<button type="button" class=" msgBtn btn btn-default" style=" display:inline; margin-right:auto;">Edit
</button>
<button type="button" id="submitFlag" class=" msgBtn btn btn-default" style=" display:inline; margin-right:auto;">Flag
</button>
<button type="button" class=" msgBtn3 btn btn-default pull-right" style="display:inline; margin-right:auto;">Delete
</button>
</td>
</tr>
#endforeach
#endforeach
Modal, inside the Resources View
<div class="modal fade" id="flagResource" tabindex="-1" role="dialog" aria-labelledby="flagModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span></button>
<h4 class="modal-title"
id="flagResourceLabel" style="text-align:center;"> Flagged
</h4>
</div>
<div class="modal-body">
{!! Form::open(array('route'=>'resource', 'class'=>'form', 'method'=>'POST')) !!}
<div class="form-group">
<label for="reason" class="control-label">Reason for Flagging:</label>
{!! Form::text('reason', null, array('class'=> 'form-control', 'placeholder'=>'Reason')) !!}
</div>
<div class="form-group">
<label for="comments" class="control-label">Other Comments:</label>
{!! Form::text('comments', null, array('class'=> 'form-control', 'placeholder'=>'Comments')) !!}
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<span class="pull-right">
<button id="submitFlag" type="submit" class="btn btn-primary" style="margin-left:5px;">Flag</button>
</span>
</div>
{!! Form::close() !!}
</div>
</div>
</div>
<script>
$('#flagResource').on('show.bs.modal', function(e) {
//var submitFlag = $(e.relatedTarget);
var resourceName = $(e.relatedTarget).data('resource-name');
var resourceId = $(e.relatedTarget).data('resource-id');
var modal = $(this);
modal.find('.modal-title').text(resourceName);
});
</script>
The problem I think is happening is my form open inside my modal
{!! Form::open(array('route'=>'resource', 'class'=>'form', 'method'=>'POST')) !!}, and my addFlag function accepts an ID, but my resource route doesn't need an {id} on it.
If someone could take a look at my routes and help me debug it, it would be great. Thanks in advance.
You need to keep the route how you had it so it passes the Resource_ID but then you need to also pass it when you setup your form.
{!! Form::open(array('route'=>'resource' array('Resource_ID' => $yourId), 'class'=>'form', 'method'=>'POST')) !!}
Regarding your comment, and looking closer at your code, I think it might make sense to re-consider how this works.
You are passing multiple resources to this view so I think adding the resource id to the URL like I originally suggested is not the best idea because it needs to be dynamic depending on what resource was clicked on and the URL isn't the easiest thing to change via javascript.
I think a better solution would be to go back to using Form::open(array('route'=>'resource', 'class'=>'form', 'method'=>'POST')) !!}, then removing the /{Resource_ID} portion from your route and also removing the $id from public function addFlag() since we are no longer passing it via the URL.
Then in your form, add a hidden valid for resource_id
<input type="hidden" name="resource_id" id="resource_id" value="" />
Then you are already listening for the bootstrap show event and grabbing the right resource id, we just need to add it to the value.
$('#flagResource').on('show.bs.modal', function(e) {
//var submitFlag = $(e.relatedTarget);
var resourceName = $(e.relatedTarget).data('resource-name');
var resourceId = $(e.relatedTarget).data('resource-id');
var modal = $(this);
modal.find('.modal-title').text(resourceName);
$('#resource_id').val(resourceId);
});
No in your addFlag() method, you can grab the resource id via...
$id = \Input::get('resource_id');
Your addFlag function inside FlagsController accepts a parameter called $id the exception you are receiving is because you have forgotten to add the parameter to your route.
Route::post('resource', ['as' => 'resource', 'uses'=>'FlagsController#addFlag']);
Should Be
Route::post('resource/{id}', ['as' => 'resource', 'uses'=>'FlagsController#addFlag']);
I believe Laravel Route parameters are name specific which is why {Resource_ID} does not work.
You can read more about routing with parameters in Laravel here
As for your modal inside the resources view you need to also pass the parameter of the resource you're updating on the form
{!! Form::open(array('route'=>'resource', array('id' => $yourIdHere), 'class'=>'form', 'method'=>'POST')) !!}
Make sure to replace $yourIdHere with the resource you want to up.
Everything else appears to be in order.
I am implementing an edit feature for 3 related models..
and to divide the work .. i implemented 3 tabs using bootstrap extensions...now each tab contains textfields, drop downs, etc., now the contents of the text and the dropdowns, radio buttons etc are pre-loaded for the user; since this is an edit feature, it would be convenient for them if their choices were retained, and that they only need to change specific fields that they want to change without really having to re-do the entire form.
Each form in a tab has its own independent "Save" button that saves the model and then switches to the next tab after refresh.
Here's what i'm trying to do, (and quite having a hard time )..
I'm sure everybody has a facebook account here.. remember when you try to write a status update, or when you are chatting with somebody and then you click a button that redirects to another page?
Then a dialog box (or modal?) will alert you that you haven't finished your post yet, and that it gives you an option whether to leave or to stay in the page?
Similar to what i'm trying to do; i'm trying to alert the user if he/she wants to save or discard his changes (if there are changes made) before he switches to another tab.. I really have no idea how to do this..
on how the widget works..is abstracted (or i haven't found it yet atleast), and unlike in java where i can easily search for "Events" onChange() or something..i'm new to php and yii.. any idea ,on how to do it, what to use, or a sample code will be really helpful guys.. Thanks a lot!
You could use a global var like var saved=false to check your form has been saved.
To catch the on change of your forms:
$('form :input').change(function(){saved=false;});
See: jquery get all form elements: input, textarea & select
When the user left the page you could fire a beforeunload:
$(window).on('beforeunload', function() {
if(!saved) return 'Leave page?';
});
See also:
Dialog box runs for 1 sec and disappears?
catching beforeunload confirmation canceled?
To catch the tab change you could use a default confirm (http://www.w3schools.com/js/js_popup.asp):
$('#myTab a').click(function (e)
{
e.preventDefault();
if(!saved)
{
if(confirm('Leave tab?'))
{
$(this).tab('show');
}
}
else
{
$(this).tab('show');
}
});
Or you could try to build your own confirm dialog:
Note it seems you can't catch the window beforeunload but for the tab change:
html:
<ul class="nav nav-tabs" id="myTab">
<li class="active">Home</li>
<li>Profile</li>
<li>Messages</li>
<li>Settings</li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="home">...</div>
<div class="tab-pane" id="profile"><form><input type="text"><input type="submit" class="btn btn-success"></form></div>
<div class="tab-pane" id="messages">...</div>
<div class="tab-pane" id="settings">...</div>
</div>
<div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="myModalLabel">Modal header</h3>
</div>
<div class="modal-body">
<p>Form not saved!</p>
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
<button id="leavetab" class="btn btn-primary">Leave tab</button>
</div>
</div>
javascript:
var saved = true;
var confirmed = false;
var thistab = null;
function checksave()
{
return saved;
}
$(function () {
$('#myTab a:last').tab('show');
$('#myTab a').click(function (e)
{
e.preventDefault();
thistab = $(this);
if(!checksave()){ $('#myModal').modal(); return false;}
thistab = null;
if(saved || confirmed)
{
$(this).tab('show');
saved = false;
confirmed = false;
}
});
//see: https://stackoverflow.com/questions/12862601/jquery-get-all-form-elements-input-textarea-select
$('form :input').change(function(){saved=false; confirmed = false;});
$('#myModal').on('hidden', function () {
return false;
});
// modal click set confirmed to true
$('#leavetab').click(function()
{
{
$('#myModal').modal('hide');
thistab.tab('show');
saved = false;
confirmed = false;
}
});
});