Getting undefined when calling search results through ajax - php

I have a Symfony app which allows CRUD operations on some events and also searching for them. The problem I get is when trying to get the results I'm searching for displayed without refreshing the page. It's the first time I'm using ajax and I think it's something wrong with the function. When I search for a word in any event name, the page is not refreshing and it shows undefined instead of showing the entries.
I appreciate any help!
Here's the method from the Controller:
public function ajaxListAction(Request $request){
//fetch the data from the database and pass it to the view
$em = $this->getDoctrine()->getManager();
$searchTerm = $request->get('search');
$form = $this->createFormBuilder()
->add('search', SubmitType::class, array('label' => 'Search', 'attr' => array('class' => 'btn btn-primary', 'style' => 'margin-bottom:15px')))->getForm();
$organizer = array();
if($searchTerm == ''){
$organizer = $this->getDoctrine()->getRepository('AppBundle:Organizer')->findAll();
}
elseif ($request->getMethod() == 'GET') {
$form->handleRequest($request);
$em = $this->getDoctrine()->getManager();
$organizer = $em->getRepository('AppBundle:Organizer')->findAllOrderedByName($searchTerm);
}
$response = new JsonResponse();
$results = array();
foreach ($organizer as $value) {
$results[] = json_encode($value);
}
return $response->setData(array(
'results' => $results
));
}
and here's the script for the search:
$(document).ready( function(event) {
$("#search").submit(function(event) {
event.preventDefault(); //prvent default submission event
$form = $(this);
var data = $('#search_term').val();
$.ajax({
url: '/ajax',
type: "GET",
data: {'search' : data },
success: function(response){
var output = '';
for (var i = 0; i < response.length; i++) {
output[i] = output + response;
}
$('#ajax_results').html('<tr><td>' + response.id + '</td></tr>' + '<tr><td>' + response.name + '</td></tr>' + '<tr><td>' + response.dueDate + '</td></tr>');
}
})
});
});
and the index.html.twig file for displaying the data:
{% extends 'base.html.twig' %}
{% block body %}
<h2 class="page-header"> Latest events </h2>
<form id="search" method="GET" action="">
<input type="text" name="search" id="search_term" />
<input type="submit" name="submit" value="Search" />
</form>
<hr />
<table class="table table-striped">
<thead>
<tr>
<th>#</th>
<th>Event</th>
<th>Due Date</th>
<th></th>
</tr>
</thead>
<tbody id="ajax_results">
{% for Events in organizer %}
<tr>
<th scope="row">{{Events.id}}</th>
<td>{{Events.name}}</td>
<td>{{Events.dueDate|date('j F, Y, g:i a')}}</td>
<td>
View
Edit
Delete
</td>
</tr>
{% endfor %}
<table class="table table-striped">
{% if organizer|length > 0 %}
{% for items in organizer %}
{% endfor %}
{% else %}
<tr>
<td colspan="2">No matching results found!</td>
</tr>
{% endif %}
</table>
</tbody>
</table>
{% endblock %}

Lets try to refactor your code first. Perhaps it will bring you near the solution.
public function ajaxListAction(Request $request){
$searchTerm = $request->get('search');
//don't need form here
if($searchTerm == ''){
$organizer = $this->getDoctrine()->getRepository('AppBundle:Organizer')->findAll();
}else{
//repository should search by searchterm in next step
$organizer = $this->getDoctrine()->getRepository('AppBundle:Organizer')->findAllOrderedByName($searchTerm);
}
return new JsonResponse($organizer);
}
and javascript:
$(document).ready( function(event) {
$("#search").submit(function(event) {
event.preventDefault(); //prvent default submission event
$form = $(this);
var data = $('#search_term').val();
$.ajax({
url: '/ajax',
type: "GET",
data: {'search' : data },
success: function(response){
$('#ajax_results').html('');
$.each(response, function(key, value) {
console.log(key, value);
$('#ajax_results').append('<tr><td>' + response[key].id + '</td></tr>' + '<tr><td>' + response[key].name + '</td></tr>' + '<tr><td>' + response[key].dueDate + '</td></tr>');
});
}
})
});
});
Please tell what do you see in js console after submit the search?

I managed to make it work. The problem was that I didn't have all the fields sent into the array in the jsonSerialize() method in the Entity file and thus the fields were showing undefined.
I also completed the append method in the .js file in order to have the whole markup replicated upon the ajax call.
Thanks to Rawburner for the suggestions!

Related

Jquery UI Sortable with Symfony , sending updates to database

I am trying to add the Jquery UI Sortable behaviour to an existing table (which is populated through a JavaScript Code).I think I followed the right steps from this link Using Jquery UI sortable to sort table rows, yet the order never changes, it's as if the controller function is never called. Here is my code
This is the code in my twig
<table class="table">
<thead>
<tr>
<th class="col-md-2" data-sort="none">{{ 'element.single'|trans }}<span class="required"> *</span></th>
<th class="col-md-2"data-sort="none">{{ 'operation.type.single'|trans }}<span class="required"> *</span></th>
<th class="col-md-4" data-sort="none">{{ 'inspection.point.label'|trans }}<span class="required"> *</span></th>
<!--09:26-----edit-->
<th class="col-md-2" data-sort="none">{{ 'inspection.point.referenceValue'|trans }}</th>
<th class="col-md-2" data-sort="none">
{#bouton import des gammes d'opérations #}
{% if 'inspection_sheet_edit' != app.request.attributes.get('_route') %}
{% if is_granted('INSPECTIONSHEET_IMPORT',equipment) %}
<div class="btn-group pull-right">
<button class="btn btn-default btn-sm" type="submit" name="import">
<i class="fa fa-file"></i>
<span>{{ 'import'|trans }}</span>
</button>
</div>
{% endif %}
{% endif %}
</th>
</tr>
</thead>
<tbody id="tabledivbody" ></tbody>
</table>
<script>
var fixHelper = function(e, ui) {
ui.children().each(function() {
$(this).width($(this).width());
});
return ui;
}
$("#tabledivbody").sortable({
items: "tr",
cursor: 'move',
opacity: 0.6,
helper: fixHelper,
update: function() {
sendOrderToServer();
}
});
function sendOrderToServer() {
var order = $("#tabledivbody").sortable("serialize");
var test='console text';
$.ajax({
type: "POST", dataType: "json", url: "{{ path('post_order') }}",
data: order,
success: function(response) {
if (response.status == "success") {
console.log(test);
window.location.href = window.location.href;
} else {
alert('Some error occurred');
}
}
});
}
</script>
This is the function in my controller
/**
* #Route("/post_order", name="post_order")
*/
public function updateInspectionPointOrder(Request $request)
{
$em=$this->getDoctrine()->getManager();
$data = json_decode($request->request->get('request'));
$count=1;
foreach ($data->getPoints() as $point) {
//$count++;
$em->getRepository('EpxInspectionBundle:InspectionPoint')->updateDisplayOrderInspectionPoint($count,$point->getId());
$em->flush();
$point->setDisplayOrder($count);
$em->getRepository('EpxInspectionBundle:InspectionPoint')->updateDisplayOrderInspectionPoint($count,$point->getId());
$em->flush();
$count++;
}
}
Any insights please.

Ajax occasionally don't work. It does depends by the browser?

I have an huge issue with my shopping cart system. The problem is when I select products and try to send through ajax to server side, products not always counted. I have to use ajax to collect all data because I'm using JQuery datatables plugin.
List of products should looks like this:
Incorrect view is a screen from my windows firefox, correct view is from my chromium broswer on linux. It looks like there is some dependency on which browser running my webservice. I'm testing my webservice on server production and it works properly only on my chromium browser.
My buddy said there is in inappropriate data sent in Ajax code.
Here's my HTML template:
{% block content %}
<div class="panel-body">
<form method="post" action="{{ path('order') }}" id="orderList">
<table class="table table-bordered table-striped mb-none" id="datatable-default">
<thead>
<tr>
<th>Nazwa</th>
<th>Kod</th>
<th>Cena netto(zł)</th>
<th class="hidden-xs">Cena brutto(zł)</th>
<th class="hidden-xs">Ilość</th>
</tr>
</thead>
<tbody>
{% for product in products %}
<tr>
<td>{{ product.productName }}</td>
<td>{{ product.code }}
</td>
<td>{{ product.priceNetto }}</td>
<td class="center hidden-xs">{{ product.priceBrutto }}</td>
<td>
<input type="number" class="spinner-input form-control" min="0" value="0" name="cart[{{ product.code }}]" />
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% if app.user != null %}
<button id="getData" type="submit" class="btn btn-primary hidden-xs">Zamów</button>
{% else %}
<p>Zaloguj się aby dokonać zamówienia</p>
{% endif %}
</form>
</div>
{% endblock %}
AJAX code:
$(document).ready(function () {
$('#getData').click( function (event) {
//event.preventDefault();
var paginator = $('#datatable-default').DataTable();
var dat = paginator.$('input').serialize();
console.log(dat);
console.log(decodeURIComponent(dat));
$.ajax({
type: "POST",
url: $('#orderList').attr('action'),
data: {'orders': decodeURIComponent(dat)},
success: function (response) {
console.log(response)
}
});
});
});
And my server side code:
public function orderAction(Request $request)
{
$session = $request->getSession();
$totalPriceNetto = 0;
$totalPriceBrutto = 0;
$user = $this->getUser();
$address = $user->getStreet();
if($request->isXmlHttpRequest())
{
$repository = $this->getDoctrine()->getRepository('AppBundle:Products');
$jsitems = $request->get('orders');
$items = [];
parse_str($jsitems, $items);
$orderItems = [];
foreach ($items['cart'] as $index=>$item)
{
if( $item != 0)
{
$orderItems[$index] = $item;
$orders[$index] = $repository->findBy(['code' => $index]);
//$orders[$index][0]->setPriceBrutto(str_replace(',', '.', str_replace('.', '', $orders[$index][0]->getPriceBrutto())));
//$orders[$index][0]->setPriceNetto(str_replace(',', '.', str_replace('.', '', $orders[$index][0]->getPriceNetto())));
$orders[$index]['value'] = $item;
}
}
$session->set('orders', $orders);
$session->set('orderItems', $orderItems);
foreach ($orders as $index=>$item)
{
$productObject = $item[0];
$totalPriceNetto += floatval(str_replace(',', '.', str_replace('.', '', $productObject->getPriceNetto()))) * (float)$item['value'];
$totalPriceBrutto += floatval(str_replace(',', '.', str_replace('.', '', $productObject->getPriceBrutto()))) * (float)$item['value'];
}
$totalPriceBrutto = round($totalPriceBrutto - ($totalPriceBrutto * $user->getPromo()/100), 2);
$session->set('dd', $totalPriceNetto);
$session->set('de', $totalPriceBrutto);
return $this->render("/default/shop/makeOrder.html.twig", ['totalPriceNetto' => $totalPriceNetto, 'totalPriceBrutto' => $totalPriceBrutto, 'address' => $address]);
}
if($session->has('dd') && $session->has('de'))
{
$totalPriceBrutto = $session->get('de');
$totalPriceNetto = $session->get('dd');
$session->remove('dd');
$session->remove('de');
}
return $this->render("/default/shop/makeOrder.html.twig", ['totalPriceNetto' => $totalPriceNetto, 'totalPriceBrutto' => $totalPriceBrutto, 'address' => $address]);
}

Symfony: pass parameter to Ajax url

I am trying to figure out how to enable/disable user with checkbox without page reload.
index.html.twig
<table>
<thead>
<tr>
<th>UserName</th>
<th>Enabled</th>
</tr>
</thead>
<tbody>
{% for user in users %}
<td><a href="{{ path('user_show', {'id': user.id}) }}">
{{ user.username }}</a>
</td>
<td><input id="user_enable_{{ user.id }}" onclick="enabledChange({{ user.id }})"
type="checkbox" {% if user.enabled %}checked{% endif %}/>
</td>
{% endfor %}
</tbody>
</table>
<script>
var changePath = {{ path('user_enable_change', {'id': user.id}) }};
function enabledChange(id)
{
var value = $(this).val();
console.log('value: ' + value);
$.ajax({
type: "POST",
url: changePath,
async: true,
data: { },
success: function () {
console.log('success');
}
});
}
</script>
UserController
/**
* #Route("/enable/{id}", name="user_enable_change")
*/
public function userDisableAction(User $user) {
if($user->isEnabled()){
$user->setEnabled(false);
}else {
$user->setEnabled(true);
}
try {
$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();
}
catch(\Exception $e) {
return new JsonResponse('error');
}
return new JsonResponse('success');
}
Question
How can I set corresponding user id to enabledChange function and change checked state depending on user state?
You need to add quotes to your variables, and pass changePath as an argument:
onclick="enabledChange('{{ user.id }}', '{{ path('user_enable_change', {'id': user.id}) }}')"
then:
function enabledChange(id, changePath) {
var value = $(this).val();
console.log('value: ' + value);
$.ajax({
type: "POST",
url: changePath,
async: true,
data: { },
success: function () {
console.log('success');
}
});
}
I hope this will help.
Another clean way of doing this is by passing parameters and routes via data attributes and accessing the button by class. In my example I had to increase product quantity without refreshing:
<button class="btn btn-sm btn-info increase"
data-id="{{ p.id }}"
data-url="{{ path('cart_add', {'pid': p.id}) }}">
Increase qty
</button>
$(document).ready(function() {
$(".increase").click(function (e) {
var value = $(this).data('id');
console.log('value: ' + value);
$.ajax({
type: "POST",
url: $(this).data('url'),
async: true,
data: {},
success: function () {
console.log('success increment');
}
});
});
});

Shopping system using Symfony

Iam learning symfony and I want to build a shop system for my school project. I want to send the list of items that the user select to another controller i don't know how.
here is my ShowItemAction
public function ShowItemAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
$form = $this->createFormBuilder()
->add('search', 'textbox', array(
'attr' => array(
'label' => false
)))->getForm();
$form->handleRequest($request);
if (($form->isSubmitted()) && ($form->isValid())) {
$formdata = $request->request->get('form');
$search = $formdata['search'];
$products_Repo = $em->getRepository('MyShopBundle:Products')->GetProducts($search);
return $this->render('MyShopBundle:Products:show.html.twig',array(
'form' => $form->createView(),
'products'=> $products_Repo
));
}
return $this->render('MyShopBundle:Products:show.html.twig',array(
'form' => $form->createView()
));
}
and my show.html.Twig
{% block body%}
<div id="cart-content">
</div>
<div class="cart-buttons">
<button id="cart">View Shopping Cart</button>
<button id="clear-cart">Clear Cart</button>
</div>
{{ form(form) }}
{% if products is defined %}
<table>
<thead>
<tr>
<th>Partner-Name</th>
<th>ProductNr</th>
<th>Description</th>
<th>price</th>
</tr>
</thead>
<tbody>
{% for product in products %}
<tr>
<td><div class="partner">{{product.partner}}</div></td>
<td><div class="partnerdata" data-value="{{ product.id }}">{{ product.productnr }}</div></td>
<td><div class="description"> {{ product.description }}</div></td>
<td><div class="price">{{ product.price }}</div></td>
<td class="counter"><input type="number" name="count" min="1" step="1"></td>
<td class="cart">
<button type='submit'>in den Warenkorb</button>
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endif %}
{% endblock %}
my javascript:
$('.cart').click(function (event) {
event.preventDefault();
var closestTr = $(this).closest('tr');
var ref = closestTr.find('.partner').text();
var data_value = closestTr.find('.partnerdata').data('value');
var productNr= closestTr.find('.partnerdata').html();
var price= closestTr.find('.price').html();
var count = closestTr.find('input').val();
if (count < 1) {
}
else {
$(".cart-content").prepend(HS_ref + "|" + data_value + "|" + herstellerNr + "|" + count + "|" + vk);
}
});
I can see the data which the user has selected(inside <div id="cart-content"></div>) but I don't know how to send those contents perhaps as a POST to the controller.
Something like this: https://www.codeofaninja.com/2013/04/shopping-cart-in-php.html
I am working on Symfony 2.7
You can do this with AJAX
First, you need to get the html data
Second, you have to send this data to the controller
Third, you have to get the data in the controller
Here is an example :
First "you need to get the html data"
HTML :
<div id="cart-content" data-route="{{ path('get_user_card_items') }}">
<span data-item-id="item1" data-quantity="2"></span>
<span data-item-id="item2" data-quantity="1"></span>
<span data-item-id="item3" data-quantity="5"></span>
</div>
JS
/**
* Get the content of #cart-content
*/
function getCartItems() {
var items = [];
$('#cart-content span').each(function(){ // loop into all span
var item = { // create an item object who get all the data
'id' : $(this).attr('data-item-id'),
'quantity' : $(this).attr('data-quantity'),
}
items.push(item); // push into an array
});
return items;
}
Second "you have to send this data to the controller"
JS
function sendCartItems() {
$.ajax({
url: $('#cart-content').attr('data-route'), // here is a route variable
method: 'POST',
data: {items: getCartItems()},
success: function (data) {
// do some stuff when it's send
}
});
}
Third "you have to get the data in the controller"
CustomController.php
class CustomController extends Controller
{
/**
* #Route("/getUserCardItems", name="get_user_card_items")
*/
public function getUserCardItemsAction(Request $request)
{
$items = $request->get('items');
var_dump($items);die; // display for you the items to see if it's works (look into the networks console tab on google chrome)
// some stuff like sending items to database...
}
}
Access network tab google chrome
And you done ;)

Symfony2: The CSRF token is invalid. Please try to resubmit the form

I have a form wher I have to fill in some information in a field.
Even if I put something inside, I am getting the error:
The CSRF token is invalid. Please try to resubmit the form
Related to this question: symfony2 CSRF invalid I am using correctly the $form->bindRequest()
if ($this->getRequest()->getMethod() == 'POST') {
$form->bindRequest($this->getRequest());
if ($form->isValid())
{
...
}
Here is my template (twig) code:
<div class="item item-last">
<h1>Create Affiliation</h1>
{% if valid == false %}
<div class="error">
{{ form_errors(form) }}
{{ form_errors(form.affiliation) }}
{{ error }}
</div>
{% endif %}
{% if app.session.flash('user-notice') != '' %}
<div class="flash-notice">
{% autoescape false %}
{{ app.session.flash('user-notice') }}
{% endautoescape %}
</div>
{% endif %}
</div>
<div class="item item-last">
<form action="{{ path('SciForumVersion2Bundle_user_submission_affiliation_create', {'hash_key' : submission.hashkey, 'author_id' : author.id }) }}?ajax=no" method="POST" class="authorForm" {{ form_enctype(form) }}>
<div style="float:left;">
<table width="100%" cellspacing="0" cellpadding="0">
<tr>
<td>
{{ form_label(form.affiliation) }}
</td>
<td>
{{ form_widget(form.affiliation, { 'attr': {'size': 40} }) }}
</td>
</tr>
<tr>
<td>
</td>
<td>
<div class="button button-left button-cancel">
<img src="{{ asset('bundles/sciforumversion2/images/design/new/button-red.png') }}"/>
cancel
</div>
<div style="float: left;"> </div>
<div class="button button-left button-cancel">
<img src="{{ asset('bundles/sciforumversion2/images/design/new/button.png') }}"/>
<input type="submit" name="login" value="submit" />
</div>
<div style="clear: both;"></div>
</td>
</tr>
</table>
</div>
{{ form_rest(form) }}
</form>
</div>
And here is the js code:
function init_submission_functions()
{
init_fck();
$(".submission_link").unbind("click").bind("click", function() {
var href = $(this).attr("href");
if( href == null || href == '' ) return false;
$.ajax({
type: "POST",
async: true,
url: href,
cache: false,
dataType: "json",
success: function(data) {
$("#content .contentwrap .itemwrap").html( data.content );
init_submission_functions();
}
});
return false;
});
$(".authorForm").unbind("submit").bind("submit", function() {
var href = $(this).attr("action");
if( href == null || href == '' ) return false;
var affiliation = "blabla";
$.ajax({
type: "POST",
async: true,
url: href,
affiliation: affiliation,
cache: false,
dataType: "json",
success: function(data) {
$("#content .contentwrap .itemwrap").html( data.content );
init_submission_functions();
}
});
return false;
});
}
But I am still getting the same error.
Send a serialized form using the serialize jQuery method:
$form.submit(function (e) {
e.preventDefault();
$this = $(this);
$.post($this.attr('action'), $this.serialize(), function (response) {
// handle the response here
});
});
This way Symfony will handle the submit request as a normal request — you don't have to do anything special to handle an Ajax form submission. All you'll need to do is to return a JsonResponse — if you need it, of course.
Here is an example of handling the form — adapt it to your needs:
if ('POST' === $request->getMethod()) {
$form->bind($request);
if ($form->isValid()) {
// do something here — like persisting or updating an entity
return new JsonResponse([
'success' => true,
]);
}
return new JsonResponse([
'success' => false,
'form' => $this->render($pathToTheTemplateHere, [
'form' => $form,
],
]);
}
The other way would be to use different templates: form.json.twig and form.html.twig — read the docs for details.

Categories