UPDATE : now when i change the poule_id i get a blank table with nothing inside , when i go directly to the url like :
https://mydomaine.eu/go/public/competition/search/equipes?poule_id=2
i get :
table "\n\n\n\n\n"
I try to make a function who change the result of my blade table who display equipes(teams) with a select box but nothing happen at the moment i don't know where i did a mistake , it's maybe in the javascript code
Hope someone could help to fix that , thanks a lot in advance
I try to change my table depending on poule_id with a select box like this :
<select id="poule">
#foreach($select_poules as $select_poule)
<option value="{{$select_poule->id}}">{{$select_poule->lb_poule}}</option>
#endforeach
</select>
My table contain the teams who depend on a poule_id :
#foreach($equipes as $equipe)
<tr>
<td>
{{$equipe->equipe->structure->nom_structure}}
</td>
<td>
{{$equipe->equipe->lb_equipe}}
</td>
<td>
{!! Form::text('nb_bonus') !!}
</td>
</tr>
#endforeach
Here my controller :
public function searchEquipes(Request $request)
{
if ($request->has('poule_id')) {
$equipes = EquipePoule::where('poule_id' , $request->poule_id)->get();
$competition = Compet::where('id' , 1)->first();
$categorie_compet = CategorieCompet::pluck('lb_categorie_compet' , 'id');
$categorie_age = CatgEquipe::pluck('lb_catg_equipe' , 'id');
$structure_rattachement = Structure::select('num_structure', 'nom_structure' , 'id')
->where('type_structure_id' , '1')
->orWhere('type_structure_id' , '2')
->orWhere('type_structure_id' , '3')
->get()
->mapWithKeys(function($i) {
return [$i->id => $i->num_structure.' - '.$i->nom_structure];
});
$poules = Poule::where(['compet_id' => $competition->id])->get();
$rencontres = Rencontre::where(['compet_id' => $competition->id])->orderBy('dt_rencontre' , 'DESC')->get();
$designations = RencontreOfficiel::where(['compet_id' => $competition->id])->get();
$classements = Classement::where(['compet_id' => $competition->id])->orderBy('nb_point_classement' , 'DESC')->get();
$equipe_to_select = Equipe::select('lb_equipe', 'structure_id' , 'catg_equipe_id' ,'id')
->get()
->mapWithKeys(function($i) {
return [$i->id => $i->lb_equipe.' - '.$i->structure->nom_structure.' - ' .$i->catg_equipe->lb_catg_equipe];
});
$stade = Stade::select('lb_nom', 'ville_stade' , 'cd_post_stade' , 'id')
->get()
->mapWithKeys(function($i) {
return [$i->id => $i->lb_nom.' - '.$i->ville_stade.' - '.$i->cd_post_stade];
});
$find_equipes = $competition->equipes;
$domicile = $find_equipes->mapWithKeys(function ($i) {
return [$i->id => $i->lb_equipe.' - '.$i->structure->nom_structure.' - ' .$i->catg_equipe->lb_catg_equipe];
});
//ON AFFICHE QUE LES EQUIPES ENREGSITRER DANS LE COMPETITION
$find_equipes = $competition->equipes;
$visiteur = $find_equipes->mapWithKeys(function ($i) {
return [$i->id => $i->lb_equipe.' - '.$i->structure->nom_structure.' - ' .$i->catg_equipe->lb_catg_equipe];
});
$select_poules = Poule::where('compet_id' , 1)->get();
$journees = Journee::where('compet_id' , 1)->get();
$journee_to_select = Journee::where('compet_id' , 1)->pluck('nm_journee' , 'id');
return response()->json([
'table' => view("competitions/show", compact('equipes' , 'competition' , 'categorie_age' , 'categorie_compet' , 'classements' , 'equipe_to_select' , 'structure_rattachement' , 'poules' , 'select_poules' , 'journee_to_select' , 'journees' , 'rencontres', 'designations' , 'stade', 'domicile' , 'visiteur'))->render(),
]);
}else {
echo 'on trouve rien ';
}
Here my javascript :
<script>
$('#poule').change(function () {
$.ajax({
type: 'GET',
dataType: "json",
url : 'search/equipes/',
data : {
poule_id : document.getElementById('poule').value
},
success:function(data){
$('#equipes').html(data.table);
},
});
});
</script>
Getting no error feedback is quite weird, but I would start by changing the dataType you expect in the ajax function to html, because is unnecessary to cast to json and cast it back to html once in javascript. So it would look like this:
Your controller:
public function searchEquipes(Request $request)
{
if ($request->has('poule_id')) {
$equipes = $equipes = EquipePoule::wherePouleId($request->poule_id)->get();
return view("competitions/show", compact('equipes'))->render();
}
}
Your ajax:
<script>
$('#poule').change(function () {
$.ajax({
type: 'GET',
dataType: "html",
url : 'search/equipes/',
data : {
poule_id : document.getElementById('poule').value
},
success:function(data){
$('#equipes').html(data);
}
});
});
</script>
Then I would follow some steps to check where the fail is:
Is the event reaching the ajax?
Is the ajax reaching the server?
Is the server routing that call to the controller?
Is the controller returning the view correctly?
Is the ajax updating the dom container?
Tell me the point where it fails and I will edit with more info.
Related
I have a dropdown list generated by the following code in controller (FocalController.php):
$focalData = DB::table('role_users')->orderBy('users.id','DESC')
->leftJoin('users', function($join){
$join->on('role_users.user_id', '=', 'users.id');
$join->on('role_users.role_id', '=', DB::raw("'1'"));
})
->select('users.id',DB::raw('CONCAT(users.name) AS focal'))
->groupBy('users.id')
->get();
foreach($focalData as $data):
$focals[$data->id] = $data->focal;
endforeach;
in the view, I have the following block generating the drop-down list:
{!! Form::select('focal', [''=>'select focal']+ collect($focals)->toArray() , $project_focal, array('class' => 'form-control','onchange'=>'changeFocal(this, '.$project->id.')' ))!!}
I want to submit the drop-down value onChange and save the value using AJAX.
My ajax for the form submission is following:
function changeFocal(e,project_id) {
var focal_id = $("#e").val();
var project_id = $("#project_id").val();
$.ajax({
type: "PUT",
data: "focal_id=" + e + "&project_id=" + project_id,
url: "{{ URL::to('admin/focal') }}",
success:function(data){
console.log(data);$("#msg").html("New Focal Assigned.");
}
});
}
My route is:
Route::post('admin/focal/','FocalController#saveFocal');
the saveFocal function in my FocalController is:
public function saveFocal(Request $request){
$focal_id = $request->focal_id;
$project_id = $request->project_id;
$project = Project::find('idea_id', $project_id)
->update([
'focal' => $focal_id,
'updated_at' => \Carbon\Carbon::now()->toDateTimeString(),
]);
#\App\Common::createAccessLog($project->focal, "Update Project Focal for Project#" . $project->idea_id . "(" . $project->name . ")");
return view('admin/focal');
}
I am getting the following error in console:
Can anyone please tell me what am I doing wrong and how can I save the select data to database with ajax along with a success message.
Use POST instead of PUT
Also make sure the route you are sending your request to is valid and exists
Below are few corrections:
function changeFocal(e,project_id) {
var focal_id = $(e).val(); //correction
var project_id = $("#project_id").val();
$.ajax({
type: "PUT",
data: "focal_id=" + focal_id + "&project_id=" + project_id, //correction
url: "{{ URL::to('admin/focal') }}",
success:function(data){
console.log(data);$("#msg").html("New Focal Assigned.");
}
});
}
Ude where Clause instead find
Project::where(
find works on primary key
I have table with a column for checkbox, When a certain button is clicked i send checked checkboxs with ajax request to a controller to download a pdf file after it has been made ,but unfortunately it doesn't fire the download box .i hope you help me out.
My Ajax :
<script>
$(document).ready(function()
{
$(document).on('click', '#minvoice', function(event)
{
//var alert = confirm('Are You Sure?');
var checkedValues = $('#chkbx:checked').map(function() {
return this.value;
}).get();
$.post('{{route('payments.multiple_invoices')}}', {'checkboxs' : checkedValues, '_token' : $('input[name=_token]').val() }, function(data){
console.log(data);
});
});
});
</script>
My Controller :
public function multiple_invoices(Request $request)
{
if ($request->isMethod('post'))
{
//export invoice
$requestData[] = $request->checkboxs;
$filenamepdf = public_path().'/export/invoices/multiple_invoices.pdf';
$data = self::getMulInvoiceData($requestData);
PDFHelper::export($data, $filenamepdf, true);
return Response::download($filenamepdf, 'Multiple_invoice_'.date('Y-m-d').'_'. time().'.pdf', []);
}
}
public static function getMulInvoiceData($requestData)
{
$payments = UnitPayment::whereIn('id', $requestData)->get();
return View::make('payments.mulInvoice', compact('payments'));
}
My Views:
<td><input id="chkbx" type="checkbox" value="{{ $payment->id }}" /></td>
MulInovices.blade.php :
#foreach($payments as $payment)
<div align="left" style="text-align: left;"><b>payment : {{$payment->id}} </b></div>
#endforeach
My Route:
Route::any('payments/multiple_invoices', ['uses' => 'PaymentsController#multiple_invoices','as' => 'payments.multiple_invoices']);
i hope you tell me how to get this to work . thanks
Try changing
return Response::download($filenamepdf, 'Multiple_invoice_'.date('Y-m-d').'_'. time().'.pdf', []);
to
return Response::download($filenamepdf, 'Multiple_invoice_'.date('Y-m-d').'_'. time().'.pdf', ['Content-Type' => 'application/pdf']);
(Difference is code below is added in to third parameter [] of Response::download)
'Content-Type' => 'application/pdf'
If you are using Laravel 5+ you can use code below
response()->($filenamepdf, 'Multiple_invoice_'.date('Y-m-d').'_'. time().'.pdf', ['Content-Type' => 'application/pdf']);
What we did is add headers to recognise response as a pdf file.
Controller:
public function index()
{
.
.
.
$search_result = $this->m_results->search_people(array('country'=>$country,'state'=>$state,'city'=>$city,'sort'=>$this->sort,'id'=>$data['id']),$keyword,$s_id);
$this->load->view('include/header',$data);
if($search_result != FALSE)
{
$data['results']['freelancers'] = $search_result;
// print_r($data['results']['freelancers']); exit();
$data['sort_value'] = $this->sort;
$this->load->view('search',$data);
}else{
$data['string'] = $this->input->post('keyword');
$this->load->view('search_empty',$data);
}
$this->load->view('include/footer',$data);
}
Ajax function:
$("body").on("change", "#sort", function () {
$.ajax({
method: "get"
, url: base_url + "search"
// , cache: false
, data: {
skills: $("#inputtags").val()
, sort: $("#sort").val()
, city: $("#locationTextField").val()
}
, success: function (data) {
// console.log(data);
alert(data);
$("body").html(data);
}
})
});
View:
<div class="form-group sidemargin">
<select name="sort" id="sort" class="form-control select color2 bold">
<option value="rating" <?php echo set_select('sort','rating',( $sort_value == "rating" ? TRUE : FALSE )); ?>>Ratings</option>
<option value="review_count" <?php echo set_select('sort','review_count',( $sort_value == "review_count" ? TRUE : FALSE )); ?>>Reviews</option>
</select>
</div>
Above are the code I'm using for display the results after sorting by Ajax. But now the problem is after sorting the result, when I alert the success data its coming the entire html code but not the result from database.
I can't understand why its showing the html code instead of the sorted data from db. When I try to print_r() those result in controller, its showing correct value. When it come to Ajax success the values are different.
So folks help me to solve this. Answers will are appreciated. Thanks in advance.
So, recently I have been dabbling a bit in cakephp but I have run into an issue I just can't seem to solve. I suspect it's something simple I'm missing (as most cases).
The premise of what I'm trying to do is somewhat self-explanatory in the title: I have a view (my add.ctp) within this add I have a form created with the form helper.
I have multiple selects, the first select is a list of companies. Once the company has been selected, I now want to update the next select based on the selected value using jquery and ajax sending a get request to the controller.
The AJAX request completes successfully however, I cannot seem to access the desirable returned data (a list of projects that belong to the selected company).
To be clear I'm trying to return an array in the success callback
I have read a lot and searched around, but I feel that maybe examples are lacking for v3 cakephp.
Some of my code :
Controller
$projectphase = $this->Projectphases->newEntity();
$data = ['content' => 'hi', 'error' => 'nothing found'];
$projects = array('0' => 'Select something');
if($this->request->is('ajax')){
$company_id = $this->request->data('company_id');
$projects = $this->Projectphases->Projects->find('list', ['limit' => 2, 'conditions' => ['company_id' => $company_id]]);
$arr = array();
$arr = $this->chainedProjects($company_id);
$data = ['content' => 'hello', 'error' => ''];
$this->set(compact('projects', 'data'));
$this->set('_serialize', ['projects', 'data']);
}elseif($this->request->is('post')){
debug("hit post");
die();
}
Public function chainedProjects
$projects = $this->Projectphases->Projects->find('list', ['limit' => 2, 'conditions' => ['company_id' => $id]]);
$projs = $projects->toArray();
$values = array();
$x = 0;
foreach ($projs as $key => $value) {
$values[$x] = "<option value='$key'>$value</option>";
$x++;
}
return $values;
Javascript [jquery ajax]
$(document).ready(function(){
$('#company').change(function () {
$company_id = $('#company').val();
var data = {
'type' : 'dropdown',
'company_id' : $company_id
};
$.ajax({
type : 'GET',
//url : 'delegate1/projectphases/add',
data : data,
encode : true,
beforeSend: function(xhr){
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
},
success: function(response){
console.log('success');
console.log(response.content);
console.log(response.message);
//console.log(data.response);
//console.log(result.content);
console.log(response);
}
}).done(function(){
console.log('done');
})
});
})
Jquery 2.1.3
Any help whatsoever will be appreciated!
I am trying to implement a "like" button for my website. I am using Codigniter, Ajax and Jquery. When the like button is clicked data should be entered to database and if unlike button is pressed data should delete from database. But I am facing a problem in model, data is not added to database when I click like button. Please help me to find a solution.
This is my Jquery file "likeitem.js"
function likeItem(postId)
{
if ($("#likeItem_"+postId).text() == "Like")
{
$("#likeItem_"+postId).html('Unlike');
var purpose = "Like";
}
else
{
$("#likeItem_"+postId).html('Like');
var purpose = "UnLike";
}
$.ajax({
type : "POST",
url : "http://localhost/codeig_smarty/index.php/user/likeItem",
data : "postId=" + postId + "purpose=" + purpose,
success : function()
{
}
});
return false;
}
This is my model "usermodel.php"
public function itemLike()
{
$id = $this->session->userdata('userID');
$postId = $this->input->post('postId');
$purpose = $this->input->post('purpose');
if($purpose=="Like")
{
// echo 'test';
// exit();
$data = array(
"userID" => $id,
"postTextID" => $postId,
);
$this->db->insert('like', $data);
}
else
{
echo 'hai';
}
}
This is my view file "home.tpl"
<li><a class="like-unlike" href="#" id="likeItem_{$item['postID']}" onclick="likeItem({$item['postID']})">Like</a></li>
This is my Controller "user.php"
public function likeItem()
{
$this->usermodel->itemLike();
}
You mistake here. You forgot to put & symbol. Try this code.
data : "postId=" + postId + "&purpose=" + purpose,
Full code:
If you want to manipulate result returned from ajax, you can do something on success block as following code :
$.ajax({
type : "POST",
url : "http://localhost/codeig_smarty/index.php/user/likeItem",
data : "postId=" + postId + "&purpose=" + purpose,
success : function(data)
{
// do seomthing here with data
// console.log(data) --> to see data return or not
}
});