jquery - Symfony 2 with AJAX: Submitting Formatted Request to Controller Method -
i have controller method takes in json request. controller returns data submitted form , updated data asynchronously loaded page. form should able re-submitted number of times without reloading page.
the problem running has json data being read controller method differently following initial form submission.
the relevant controller code is:
/** * creates new hoursspecial entity. * * @route("/", name="hoursspecial_postcreate") * @method("post") */ public function postcreateaction(request $request){ $requestdata = $request->request->all(); $date = $requestdata['eventdate']; ... } and here ajax code:
/* handle submission of special hours form */ $('.specialhours_form').on('submit', function(event){ var targetform = $(this); event.preventdefault(); ajaxobject = { url: $(this).attr("action"), type: 'post', contenttype: "application/json; charset=utf-8", data: json.stringify({"opentime":$("#appbundle_hoursspecial_opentime", this).val(), "closetime":$("#appbundle_hoursspecial_closetime", this).val(), "status":$("input[name^='appbundle_hoursspecial']:checked", this).val(), "event":$("#appbundle_hoursspecial_event").val(), "area":$("#appbundle_hoursspecial_area", this).val(), "eventdate":$("#appbundle_hoursspecial_eventdate", this).val()}) } $.ajax(ajaxobject) .success(function(data,status,xhr) { console.log( status ); $('#special_hours_container').html(data); //show special hours chosen week $('#dynamic_daterange').html(' week of ' + $('#special_firstday').html() + ' thru ' + $('#special_lastday').html() + ' '); maketimepicker('.specialhours_form', '#appbundle_hoursspecial_opentime, #appbundle_hoursspecial_closetime'); //apply timepicker special hours elements $('span.label-success, span.label-danger').remove(); }) .fail(function(data,status,xhr) { $('span.label-success, span.label-danger').remove(); $('#appbundle_hoursspecial_submit', targetform).after('<span class="label label-danger"><span class="glyphicon glyphicon-remove"></span></span>'); console.log( status ); console.log("dangah!"); }) .always(function(data,status,xhr) { console.log( status ); }); }); so if submit form first time around , run var_dump of $requestdata, (using sample data):
array(6) { ["opentime"]=> string(5) "01:00" ["closetime"]=> string(5) "11:00" ["status"]=> string(1) "0" ["event"]=> string(1) "1" ["area"]=> string(2) "10" ["eventdate"]=> string(10) "2016-01-04" } however, if second time, data placed inside of second array key name of form submitting:
array(1) { ["appbundle_hoursspecial"]=> array(7) { ["opentime"]=> string(5) "00:30" ["closetime"]=> string(5) "02:00" ["status"]=> string(1) "0" ["eventdate"]=> string(10) "2016-01-04" ["area"]=> string(2) "10" ["event"]=> string(1) "1" ["submit"]=> string(0) "" } } and end error:
notice: undefined index: eventdate there has reason , can't figure out be! know check existence of appbundle_hoursspecial key in controller, i'd rather not if don't have to.
this has form rendering.
$('#special_hours_container').html(data); i assume data html form. in form type have like:
// hoursspecialformtype or ... public function getname() { return 'appbundle_hoursspecial'; } this cause of new forms inserted page be:
<input name="appbundle_hoursspecial[opentime]" type=text /> then when data gets submitted again wrapped in array.
to fix this
simply dont provide form name.
public function getname() { return ''; }
Comments
Post a Comment