i'm studying the Symfony2 framework and i want to create a simple application with a very simple form (one text field) and save the submitted data (using ajax) in a very simple table on the database. My goal is to view and update (in the same page using ajax) the entire database table every time a new submitted data is sent and saved. This is what i done until now, i'm not able to save anything and a 500
DefaultController.php
class DefaultController extends Controller
{
public function indexAction(Request $request)
{
$item = New Item();
$form = $this->createForm(new ItemType(), $item, array('action'=>$this->generateUrl('vendor_name_simple_homepage'),'method'=>'POST'));
$form->add('Submit','submit',array('label'=>'Add'));
$form->handleRequest($request);
if ($request->isXmlHttpRequest()) {
$this->saveAction($item);
}
return $this->render('VendorNameSimpleBundle:Default:lista.html.twig',array('form' => $form->createView()));
}
public function saveAction($item){
$em = $this->getDoctrine()->getManager();
$em->persist($item);
$em->flush();
return new Response('success');
}
}
The JQuery script
<script>
$(document).ready(function() {
$('form').submit(function(event) {
// prevents the browser from "following" the link
event.preventDefault();
var $url = $("form").attr("action");// + '.json';
var $value = $('#item_itemName').val();
$.ajax({
type: "POST",
url: $url,
data: $value,
success: function(data) {
$('ul').html(data);
}/*,
dataType: 'json'*/
});
});
});
</script>
EDIT: This is the Network console output, there is no error in the JS console
An exception occurred while executing 'INSERT INTO item (itemName) VALUES (?)' with params [null]: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'itemName' cannot be null (500 Internal Server Error)
UPDATE: seems that the request is successfully sent but i can't access (or i don't exactly know how to do) to the content request because the controller receive this Request:
/app_dev.php/myapp/path/ HTTP/1.1 Accept: / Accept-Encoding: gzip, deflate Accept-Language: it-IT,it;q=0.8,en-US;q=0.5,en;q=0.3 Cache-Control: no-cache Connection: keep-alive Content-Length: 11 Content-Type: application/x-www-form-urlencoded; charset=UTF-8 Cookie: PHPSESSID=qhou8f9lias0i9fh3besdpbii7 Host: localhost:8000 Pragma: no-cache Referer: http://localhost:8000/app_dev.php/myapp/path/ User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0 X-Php-Ob-Level: 1 X-Requested-With: XMLHttpRequest MyInputData