I'm building a mobile web app that gets information from a Symfony2 application that I've built. There is a form that the user has to submit in order to view information, but when I submit the form via AJAX the controller that deals with it doesn't see the form being submitted.
The mobile app is built with Cordova, so the form originates from a static HTML form. Once this is submitted via AJAX which will then return the data needed. However, the form submits fine triggering AJAX to do it's magic, but Symfony2 doesn't see a form being submitted. I did a var_dump
on $request = $this->getRequest();
and the form is not included here. Here is the dump:
object(Symfony\Component\HttpFoundation\Request)#6 (20) { ["attributes"]=> object(Symfony\Component\HttpFoundation\ParameterBag)#9 (1) { ["parameters":protected]=> array(3) { ["_controller"]=> string(60) "CWWA\CoreBundle\Controller\AjaxController::getscheduleAction" ["_route"]=> string(17) "ajax_get_schedule" ["_route_params"]=> array(0) { } } } ["request"]=> object(Symfony\Component\HttpFoundation\ParameterBag)#7 (1) { ["parameters":protected]=> array(0) { } } ["query"]=> object(Symfony\Component\HttpFoundation\ParameterBag)#8 (1) { ["parameters":protected]=> array(0) { } } ["server"]=> object(Symfony\Component\HttpFoundation\ServerBag)#12 (1) { ["parameters":protected]=> array(35) { ["BASE"]=> string(9) "/cwwa/web" ["CONTENT_LENGTH"]=> string(2) "70" ["CONTENT_TYPE"]=> string(31) "application/json; charset=UTF-8" ["DOCUMENT_ROOT"]=> string(8) "/var/www" ["GATEWAY_INTERFACE"]=> string(7) "CGI/1.1" ["HTTP_ACCEPT"]=> string(46) "application/json, text/javascript, /; q=0.01" ["HTTP_ACCEPT_ENCODING"]=> string(13) "gzip, deflate" ["HTTP_ACCEPT_LANGUAGE"]=> string(14) "en-gb,en;q=0.5" ["HTTP_CACHE_CONTROL"]=> string(8) "no-cache" ["HTTP_CONNECTION"]=> string(10) "keep-alive" ["HTTP_COOKIE"]=> string(36) "PHPSESSID=lvfqh6hcdampcah8mi93il6922" ["HTTP_HOST"]=> string(9) "domain.com" ["HTTP_PRAGMA"]=> string(8) "no-cache" ["HTTP_REFERER"]=> string(26) "http://domain.com/cwwa-app/" ["HTTP_USER_AGENT"]=> string(85) "Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20140722 Firefox/24.0 Iceweasel/24.7.0" ["PATH"]=> string(13) "/bin:/usr/bin" ["PATH_INFO"]=> string(22) "/ajax/client/schedule/" ["PATH_TRANSLATED"]=> string(29) "/var/www/cwwa/web/app_dev.php" ["QUERY_STRING"]=> string(0) "" ["REDIRECT_STATUS"]=> string(3) "200" ["REMOTE_ADDR"]=> string(3) "::1" ["REMOTE_PORT"]=> string(5) "33568" ["REQUEST_METHOD"]=> string(4) "POST" ["REQUEST_URI"]=> string(43) "/cwwa/web/app_dev.php/ajax/client/schedule/" ["SCRIPT_FILENAME"]=> string(29) "/var/www/cwwa/web/app_dev.php" ["SCRIPT_NAME"]=> string(21) "/cwwa/web/app_dev.php" ["SERVER_ADDR"]=> string(3) "::1" ["SERVER_ADMIN"]=> string(19) "webmaster@localhost" ["SERVER_NAME"]=> string(9) "localhost" ["SERVER_PORT"]=> string(2) "80" ["SERVER_PROTOCOL"]=> string(8) "HTTP/1.1" ["SERVER_SIGNATURE"]=> string(70) " Apache/2.2.22 (Debian) Server at localhost Port 80 " ["SERVER_SOFTWARE"]=> string(22) "Apache/2.2.22 (Debian)" ["PHP_SELF"]=> string(43) "/cwwa/web/app_dev.php/ajax/client/schedule/" ["REQUEST_TIME"]=> int(1409142446) } } ["files"]=> object(Symfony\Component\HttpFoundation\FileBag)#11 (1) { ["parameters":protected]=> array(0) { } } ["cookies"]=> object(Symfony\Component\HttpFoundation\ParameterBag)#10 (1) { ["parameters":protected]=> array(1) { ["PHPSESSID"]=> string(26) "lvfqh6hcdampcah8mi93il6922" } } ["headers"]=> object(Symfony\Component\HttpFoundation\HeaderBag)#13 (2) { ["headers":protected]=> array(13) { ["content-length"]=> array(1) { [0]=> string(2) "70" } ["content-type"]=> array(1) { [0]=> string(31) "application/json; charset=UTF-8" } ["accept"]=> array(1) { [0]=> string(46) "application/json, text/javascript, /; q=0.01" } ["accept-encoding"]=> array(1) { [0]=> string(13) "gzip, deflate" } ["accept-language"]=> array(1) { [0]=> string(14) "en-gb,en;q=0.5" } ["cache-control"]=> array(1) { [0]=> string(8) "no-cache" } ["connection"]=> array(1) { [0]=> string(10) "keep-alive" } ["cookie"]=> array(1) { [0]=> string(36) "PHPSESSID=lvfqh6hcdampcah8mi93il6922" } ["host"]=> array(1) { [0]=> string(9) "localhost" } ["pragma"]=> array(1) { [0]=> string(8) "no-cache" } ["referer"]=> array(1) { [0]=> string(26) "http://domain.com/cwwa-app/" } ["user-agent"]=> array(1) { [0]=> string(85) "Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20140722 Firefox/24.0 Iceweasel/24.7.0" } ["x-php-ob-level"]=> array(1) { [0]=> int(1) } } ["cacheControl":protected]=> array(1) { ["no-cache"]=> bool(true) } } ["content":protected]=> NULL ["languages":protected]=> NULL ["charsets":protected]=> NULL ["acceptableContentTypes":protected]=> NULL ["pathInfo":protected]=> string(22) "/ajax/client/schedule/" ["requestUri":protected]=> string(43) "/cwwa/web/app_dev.php/ajax/client/schedule/" ["baseUrl":protected]=> string(21) "/cwwa/web/app_dev.php" ["basePath":protected]=> NULL ["method":protected]=> string(4) "POST" ["format":protected]=> NULL ["session":protected]=> object(Symfony\Component\HttpFoundation\Session\Session)#67 (3) { ["storage":protected]=> object(Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage)#66 (5) { ["bags":protected]=> array(2) { ["attributes"]=> object(Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag)#62 (3) { ["name":"Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag":private]=> string(10) "attributes" ["storageKey":"Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag":private]=> string(15) "_sf2_attributes" ["attributes":protected]=> array(0) { } } ["flashes"]=> object(Symfony\Component\HttpFoundation\Session\Flash\FlashBag)#61 (3) { ["name":"Symfony\Component\HttpFoundation\Session\Flash\FlashBag":private]=> string(7) "flashes" ["flashes":"Symfony\Component\HttpFoundation\Session\Flash\FlashBag":private]=> array(0) { } ["storageKey":"Symfony\Component\HttpFoundation\Session\Flash\FlashBag":private]=> string(12) "_sf2_flashes" } } ["started":protected]=> bool(false) ["closed":protected]=> bool(false) ["saveHandler":protected]=> object(Symfony\Component\HttpFoundation\Session\Storage\Proxy\NativeProxy)#63 (3) { ["wrapper":protected]=> bool(false) ["active":protected]=> bool(false) ["saveHandlerName":protected]=> string(5) "files" } ["metadataBag":protected]=> object(Symfony\Component\HttpFoundation\Session\Storage\MetadataBag)#64 (4) { ["name":"Symfony\Component\HttpFoundation\Session\Storage\MetadataBag":private]=> string(10) "__metadata" ["storageKey":"Symfony\Component\HttpFoundation\Session\Storage\MetadataBag":private]=> string(9) "_sf2_meta" ["meta":protected]=> array(3) { ["c"]=> int(0) ["u"]=> int(0) ["l"]=> int(0) } ["lastUsed":"Symfony\Component\HttpFoundation\Session\Storage\MetadataBag":private]=> NULL } } ["flashName":"Symfony\Component\HttpFoundation\Session\Session":private]=> string(7) "flashes" ["attributeName":"Symfony\Component\HttpFoundation\Session\Session":private]=> string(10) "attributes" } ["locale":protected]=> NULL ["defaultLocale":protected]=> string(2) "en" }
The HTML form is like this:
<form method="post" name="scf" id="scheduleChangeForm" action="http://localhost/cwwa/web/app_dev.php/ajax/client/schedule/">
<input type="hidden" id="scheduleChangeCustomer" class="form-control" name="scf[customer]" />
<select id="divisionSelect" class="form-control" name="scf[division]">
<option>All Divisions</option>
</select>
<select id="siteSelect" class="form-control siteDivSelect" name="scf[site]">
<option>All Sites</option>
</select>
<button type="submit" class="btn btn-primary btn-go" id="chSBtn">Submit <i class="fa fa-arrow-circle-right"></i></button>
</form>
And the AJAX is like this:
var scf = $('#scheduleChangeForm');
scf.submit(function(){
$.ajax({
type: scf.attr('method'),
url: scf.attr('action'),
dataType: 'json',
crossDomain: true,
contentType: 'application/json',
data: scf.serialize(),
success: function(data) {
// Deals with a successful AJAX submit
}
});
});
EDIT
I've added the following code to the controller:
$content = $this->get("request")->getContent();
print_r($content);
This returned the following:
scf%5Bcustomer%5D=1&scf%5Bdivision%5D=Circom+Managed&scf%5Bsite%5D=633
So the form is being submitted to the request, not sure as to why it's being displayed like this though.
$content = $this->get("request")->request->all()
– COil