[Successfull] Sending Data With Post Instead Of Get

hi everybody.

depending of the option choose on my (very) long form, we can have this error :


POST http://localhost/LayoutColloques/index.php/admin...BN%3Bs%3A14%3A%22%00CComponent%00_m%22%3BN%3B%7D

	

414 Request-URI Too Long

I can have a very long form because I have to add several times a part of my form using renderPartial and ajax.

For example, if the user have to add 3 sessions and for each session 5 activity, all the descriptions (name/date/price/ and so on…) of these sessions and activities have to be filled…

if the user choose to add 3 sessions, 3 little form are add to the main form. I made this like that :


 

<?php

     echo $form->dropDownList($eventForm, 'nbSession',

	array(

		1=>'1',

		2=>'2',

		3=>'3',

		4=>'4',

		5=>'5'),

	array('prompt' => 'Choisissez le nombre de session d\'ateliers',

		'id' => 'nbSession',

		'class'=>'eventForm',

		     'ajax' => array(

		     'type' => 'POST',

		     'url' => @Yii::app()->createUrl('admin/UpdateAjaxFormNbSession',array('form'=>serialize($form))), //this will show nbSession Times the form for the session. (and after, by session i show nbActivity time the form for the activity.) ...

	             'update' => '#session'),) 

);

?>

this is one of my ajax event.

But if I’m right, the $form item is sent by using GET method. So, i’m pretty fast limited with the lenght of my url.

Is there a way to send that with POST option?

thanks.

Your form is getting sent via POST, the problem is here: createUrl(’…’,array(‘form’=>serialize())…), and this is what your webserver is telling you: 414 Request-URI Too Long.

Basically you add your serialized form data to request URL.

Use param ‘data’ of ajax $.post for this. See JQuery docs for more info about how to pass extra data to post.

ok, thx for you fast answer.

so I read JQuerry.data() docs and so I send my data like this :


<?php

     echo $form->dropDownList($eventForm, 'nbSession',

        array(

                1=>'1',

                2=>'2',

                3=>'3',

                4=>'4',

                5=>'5'),

        array('prompt' => 'Choisissez le nombre de session d\'ateliers',

                'id' => 'nbSession',

                'class'=>'eventForm',

                     'ajax' => array(

                     'type' => 'POST',

                     'url' => @Yii::app()->createUrl('admin/UpdateAjaxFormNbSession'),

		     'data' => array('form'=>$form,'listAteliers'=>$listAteliers)),

                'update' => '#session'));

?>

I’m not sure but, by reading sme logs, it seems to be send via POST option, so that’s great, but I don’t know how to recieve the data. (did I have to serialize here? )

Else In my controller I do :




$this->renderPartial('_ajaxContentFormSession',

    			array(

    					'form'=>$_POST['form'],

    					'listAteliers'=>$_POST['listAteliers']

    					//'eventForm'=>$_GET['eventForm'],

    					//'gestionnaire'=>$gestionnaire,

    			),

    			false,

    			true);



and in my _ajaxContentFormSession view :




$form = $_POST['form'];



???

Or _POST not needed? btw none of them works… :(

and I come back to my last week problem…

The simplest way is to use JS:


$.ajax({

    url: '...',

    data: $('your-form-selector').serialize(),

    ...

});

I think that my form is correctly send to my _ajaxView. But I can’t use it correctly…

what I have to write in the _ajaxView to get back my $form which is sending via POST option passing by my controller.

for now, I tried :

unserialize($form);

where I get an error :


<h1>PHP Error [8]</h1>

<p>unserialize(): Error at offset 344 of 4573 bytes ( ...\_ajaxContentFormSession.php:2)</p>

(I tried strislashes function without any success…)

I tried too without serialization and unserialization :

$_POST[‘form’] or $form

and I get some value but not the complete form

what I get :


array(11) { ["action"]=> string(0) "" ["method"]=> string(4) "post" ["stateful"]=> string(5) "false" ["errorMessageCssClass"]=> string(12) "errorMessage" ["htmlOptions"]=> array(1) { ["id"]=> string(14) "evenement-form" } ["clientOptions"]=> array(1) { ["validateOnSubmit"]=> string(4) "true" } ["enableAjaxValidation"]=> string(4) "true" ["enableClientValidation"]=> string(5) "false" ["focus"]=> string(0) "" ["actionPrefix"]=> string(0) "" ["skin"]=> string(7) "default" } 

what I want : (very longer that what I have.)


object(CActiveForm)#91 (19) { ["action"]=> string(0) "" ["method"]=> string(4) "post" ["stateful"]=> bool(false) ["errorMessageCssClass"]=> string(12) "errorMessage" ["htmlOptions"]=> array(1) { ["id"]=> string(14) "evenement-form" } ["clientOptions"]=> array(1) { ["validateOnSubmit"]=> bool(true) } ["enableAjaxValidation"]=> bool(true) ["enableClientValidation"]=> bool(false) ["focus"]=> NULL ["attributes":protected]=> array(<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/cool.gif' class='bbc_emoticon' alt='8)' /> { ["EventForm_Titre"]=> array(6) { ["id"]=> string(15) "EventForm_Titre" ["inputID"]=> string(15) "EventForm_Titre" ["errorID"]=> string(19) "EventForm_Titre_em_" ["model"]=> string(9) "EventForm" ["name"]=> string(5) "Titre" ["enableAjaxValidation"]=> bool(true) } ["EventForm_LibelleEvenement"]=> array(6) { ["id"]=> string(26) "EventForm_LibelleEvenement" ["inputID"]=> string(26) "EventForm_LibelleEvenement" ["errorID"]=> string(30) "EventForm_LibelleEvenement_em_" ["model"]=> string(9) "EventForm" ["name"]=> string(16) "LibelleEvenement" ["enableAjaxValidation"]=> bool(true) } ["EventForm_NumCompteIban"]=> array(6) { ["id"]=> string(23) "EventForm_NumCompteIban" ["inputID"]=> string(23) "EventForm_NumCompteIban" ["errorID"]=> string(27) "EventForm_NumCompteIban_em_" ["model"]=> string(9) "EventForm" ["name"]=> string(13) "NumCompteIban" ["enableAjaxValidation"]=> bool(true) } ["EventForm_DateUltimePaiement"]=> array(6) { ["id"]=> string(28) "EventForm_DateUltimePaiement" ["inputID"]=> string(28) "EventForm_DateUltimePaiement" ["errorID"]=> string(32) "EventForm_DateUltimePaiement_em_" ["model"]=> string(9) "EventForm" ["name"]=> string(18) "DateUltimePaiement" ["enableAjaxValidation"]=> bool(true) } ["EventForm_DateUltimeInscription"]=> array(6) { ["id"]=> string(31) "EventForm_DateUltimeInscription" ["inputID"]=> string(31) "EventForm_DateUltimeInscription" ["errorID"]=> string(35) "EventForm_DateUltimeInscription_em_" ["model"]=> string(9) "EventForm" ["name"]=> string(21) "DateUltimeInscription" ["enableAjaxValidation"]=> bool(true) } ["EventForm_NbMaxAccompagnant"]=> array(6) { ["id"]=> string(27) "EventForm_NbMaxAccompagnant" ["inputID"]=> string(27) "EventForm_NbMaxAccompagnant" ["errorID"]=> string(31) "EventForm_NbMaxAccompagnant_em_" ["model"]=> string(9) "EventForm" ["name"]=> string(17) "NbMaxAccompagnant" ["enableAjaxValidation"]=> bool(true) } ["EventForm_Dvd"]=> array(6) { ["id"]=> string(13) "EventForm_Dvd" ["inputID"]=> string(13) "EventForm_Dvd" ["errorID"]=> string(17) "EventForm_Dvd_em_" ["model"]=> string(9) "EventForm" ["name"]=> string(3) "Dvd" ["enableAjaxValidation"]=> bool(true) } ["EventForm_Acte"]=> array(6) { ["id"]=> string(14) "EventForm_Acte" ["inputID"]=> string(14) "EventForm_Acte" ["errorID"]=> string(18) "EventForm_Acte_em_" ["model"]=> string(9) "EventForm" ["name"]=> string(4) "Acte" ["enableAjaxValidation"]=> bool(true) } } ["summaryID":protected]=> string(18) "evenement-form_es_" ["_summaryAttributes":"CActiveForm":private]=> array(33) { [0]=> string(17) "EventForm_nbJours" [1]=> string(32) "EventForm_comboGestionnaireValue" [2]=> string(27) "EventForm_comboAtelierValue" [3]=> string(15) "EventForm_Titre" [4]=> string(26) "EventForm_LibelleEvenement" [5]=> string(31) "EventForm_DateUltimeInscription" [6]=> string(27) "EventForm_NbMaxAccompagnant" [7]=> string(25) "EventForm_Id_Gestionnaire" [8]=> string(23) "EventForm_NumCompteIban" [9]=> string(13) "EventForm_Dvd" [10]=> string(14) "EventForm_Acte" [11]=> string(28) "EventForm_DateUltimePaiement" [12]=> string(19) "EventForm_DateEvent" [13]=> string(20) "EventForm_HeureDebut" [14]=> string(22) "EventForm_HeureAcceuil" [15]=> string(23) "EventForm_Id_Evenements" [16]=> string(18) "EventForm_HeureFin" [17]=> string(13) "EventForm_Nom" [18]=> string(16) "EventForm_Prenom" [19]=> string(17) "EventForm_Adr_Rue" [20]=> string(20) "EventForm_Adr_Numero" [21]=> string(24) "EventForm_Adr_CodePostal" [22]=> string(22) "EventForm_Adr_Localite" [23]=> string(18) "EventForm_Adr_Pays" [24]=> string(24) "EventForm_LibelleSession" [25]=> string(14) "EventForm_Date" [26]=> string(14) "EventForm_Hdeb" [27]=> string(14) "EventForm_Hfin" [28]=> string(19) "EventForm_nbAtelier" [29]=> string(33) "EventForm_NbMaxAtelierSelectionne" [30]=> string(24) "EventForm_LibelleAtelier" [31]=> string(18) "EventForm_Id_Salle" [32]=> string(26) "EventForm_NbMaxParticipant" } ["actionPrefix"]=> NULL ["skin"]=> string(7) "default" ["_id":"CWidget":private]=> string(14) "evenement-form" ["_owner":"CWidget":private]=> object(AdminController)#15 (15) { ["layout"]=> string(17) "//layouts/column2" ["menu"]=> array(0) { } ["breadcrumbs"]=> array(0) { } ["defaultAction"]=> string(5) "index" ["_id":"CController":private]=> string(5) "admin" ["_action":"CController":private]=> object(CInlineAction)#16 (4) { ["_id":"CAction":private]=> string(14) "ajoutEvenement" ["_controller":"CAction":private]=> *RECURSION* ["_e":"CComponent":private]=> NULL ["_m":"CComponent":private]=> NULL } ["_pageTitle":"CController":private]=> NULL ["_cachingStack":"CController":private]=> NULL ["_clips":"CController":private]=> NULL ["_dynamicOutput":"CController":private]=> NULL ["_pageStates":"CController":private]=> NULL ["_module":"CController":private]=> NULL ["_widgetStack":"CBaseController":private]=> array(1) { [0]=> *RECURSION* } ["_e":"CComponent":private]=> NULL ["_m":"CComponent":private]=> NULL } ["_widgetStack":"CBaseController":private]=> array(0) { } ["_e":"CComponent":private]=> NULL ["_m":"CComponent":private]=> NULL } 

My last idea is that the problem comes from the unserialization and not by the fact of passing some data.

because I have more information passed when I serialize my form and then, print it without unserialization.

there is the result :


string(4573) "O:11:"CActiveForm":19:{s:6:"action";s:0:"";s:6:"method";s:4:"post";s:8:"stateful";b:0;s:20:"errorMessageCssClass";s:12:"errorMessage";s:11:"htmlOptions";a:1:{s:2:"id";s:14:"evenement-form";}s:13:"clientOptions";a:1:{s:16:"validateOnSubmit";b:1;}s:20:"enableAjaxValidation";b:1;s:22:"enableClientValidation";b:0;s:5:"focus";N;s:13:"�*�attributes";a:8:{s:15:"EventForm_Titre";a:6:{s:2:"id";s:15:"EventForm_Titre";s:7:"inputID";s:15:"EventForm_Titre";s:7:"errorID";s:19:"EventForm_Titre_em_";s:5:"model";s:9:"EventForm";s:4:"name";s:5:"Titre";s:20:"enableAjaxValidation";b:1;}s:26:"EventForm_LibelleEvenement";a:6:{s:2:"id";s:26:"EventForm_LibelleEvenement";s:7:"inputID";s:26:"EventForm_LibelleEvenement";s:7:"errorID";s:30:"EventForm_LibelleEvenement_em_";s:5:"model";s:9:"EventForm";s:4:"name";s:16:"LibelleEvenement";s:20:"enableAjaxValidation";b:1;}s:23:"EventForm_NumCompteIban";a:6:{s:2:"id";s:23:"EventForm_NumCompteIban";s:7:"inputID";s:23:"EventForm_NumCompteIban";s:7:"errorID";s:27:"EventForm_NumCompteIban_em_";s:5:"model";s:9:"EventForm";s:4:"name";s:13:"NumCompteIban";s:20:"enableAjaxValidation";b:1;}s:28:"EventForm_DateUltimePaiement";a:6:{s:2:"id";s:28:"EventForm_DateUltimePaiement";s:7:"inputID";s:28:"EventForm_DateUltimePaiement";s:7:"errorID";s:32:"EventForm_DateUltimePaiement_em_";s:5:"model";s:9:"EventForm";s:4:"name";s:18:"DateUltimePaiement";s:20:"enableAjaxValidation";b:1;}s:31:"EventForm_DateUltimeInscription";a:6:{s:2:"id";s:31:"EventForm_DateUltimeInscription";s:7:"inputID";s:31:"EventForm_DateUltimeInscription";s:7:"errorID";s:35:"EventForm_DateUltimeInscription_em_";s:5:"model";s:9:"EventForm";s:4:"name";s:21:"DateUltimeInscription";s:20:"enableAjaxValidation";b:1;}s:27:"EventForm_NbMaxAccompagnant";a:6:{s:2:"id";s:27:"EventForm_NbMaxAccompagnant";s:7:"inputID";s:27:"EventForm_NbMaxAccompagnant";s:7:"errorID";s:31:"EventForm_NbMaxAccompagnant_em_";s:5:"model";s:9:"EventForm";s:4:"name";s:17:"NbMaxAccompagnant";s:20:"enableAjaxValidation";b:1;}s:13:"EventForm_Dvd";a:6:{s:2:"id";s:13:"EventForm_Dvd";s:7:"inputID";s:13:"EventForm_Dvd";s:7:"errorID";s:17:"EventForm_Dvd_em_";s:5:"model";s:9:"EventForm";s:4:"name";s:3:"Dvd";s:20:"enableAjaxValidation";b:1;}s:14:"EventForm_Acte";a:6:{s:2:"id";s:14:"EventForm_Acte";s:7:"inputID";s:14:"EventForm_Acte";s:7:"errorID";s:18:"EventForm_Acte_em_";s:5:"model";s:9:"EventForm";s:4:"name";s:4:"Acte";s:20:"enableAjaxValidation";b:1;}}s:12:"�*�summaryID";s:18:"evenement-form_es_";s:31:"�CActiveForm�_summaryAttributes";a:33:{i:0;s:17:"EventForm_nbJours";i:1;s:32:"EventForm_comboGestionnaireValue";i:2;s:27:"EventForm_comboAtelierValue";i:3;s:15:"EventForm_Titre";i:4;s:26:"EventForm_LibelleEvenement";i:5;s:31:"EventForm_DateUltimeInscription";i:6;s:27:"EventForm_NbMaxAccompagnant";i:7;s:25:"EventForm_Id_Gestionnaire";i:8;s:23:"EventForm_NumCompteIban";i:9;s:13:"EventForm_Dvd";i:10;s:14:"EventForm_Acte";i:11;s:28:"EventForm_DateUltimePaiement";i:12;s:19:"EventForm_DateEvent";i:13;s:20:"EventForm_HeureDebut";i:14;s:22:"EventForm_HeureAcceuil";i:15;s:23:"EventForm_Id_Evenements";i:16;s:18:"EventForm_HeureFin";i:17;s:13:"EventForm_Nom";i:18;s:16:"EventForm_Prenom";i:19;s:17:"EventForm_Adr_Rue";i:20;s:20:"EventForm_Adr_Numero";i:21;s:24:"EventForm_Adr_CodePostal";i:22;s:22:"EventForm_Adr_Localite";i:23;s:18:"EventForm_Adr_Pays";i:24;s:24:"EventForm_LibelleSession";i:25;s:14:"EventForm_Date";i:26;s:14:"EventForm_Hdeb";i:27;s:14:"EventForm_Hfin";i:28;s:19:"EventForm_nbAtelier";i:29;s:33:"EventForm_NbMaxAtelierSelectionne";i:30;s:24:"EventForm_LibelleAtelier";i:31;s:18:"EventForm_Id_Salle";i:32;s:26:"EventForm_NbMaxParticipant";}s:12:"actionPrefix";N;s:4:"skin";s:7:"default";s:12:"�CWidget�_id";s:14:"evenement-form";s:15:"�CWidget�_owner";O:15:"AdminController":15:{s:6:"layout";s:17:"//layouts/column2";s:4:"menu";a:0:{}s:11:"breadcrumbs";a:0:{}s:13:"defaultAction";s:5:"index";s:16:"�CController�_id";s:5:"admin";s:20:"�CController�_action";O:13:"CInlineAction":4:{s:12:"�CAction�_id";s:14:"ajoutEvenement";s:20:"�CAction�_controller";r:108;s:14:"�CComponent�_e";N;s:14:"�CComponent�_m";N;}s:23:"�CController�_pageTitle";N;s:26:"�CController�_cachingStack";N;s:19:"�CController�_clips";N;s:27:"�CController�_dynamicOutput";N;s:24:"�CController�_pageStates";N;s:20:"�CController�_module";N;s:29:"�CBaseController�_widgetStack";a:1:{i:0;r:1;}s:14:"�CComponent�_e";N;s:14:"�CComponent�_m";N;}s:29:"�CBaseController�_widgetStack";a:0:{}s:14:"�CComponent�_e";N;s:14:"�CComponent�_m";N;}" 

so, is tehre a serialization problem or not?

and do you have an idea about how to solve it?

thanks in advance :)

Hi

One suggesion

Dont go directly to _ajaxview … inbetween goto controller and validate the data… then goto _ajaxview… its better…

inbetween page(_ajaxview) you will loose form data, because u will have different $form on ajaxview… just guess…

Why do you want to serialize the form object?

that’s planned, In my controller, my validation is in comment for now because i’m actually not able to pass the data correctly until now.

as I said in my previous post, if I don’t serialize, I don’t have all my form… only this. (look the different result gigen with the var_dump function above)


array(11) { ["action"]=> string(0) "" ["method"]=> string(4) "post" ["stateful"]=> string(5) "false" ["errorMessageCssClass"]=> string(12) "errorMessage" ["htmlOptions"]=> array(1) { ["id"]=> string(14) "evenement-form" } ["clientOptions"]=> array(1) { ["validateOnSubmit"]=> string(4) "true" } ["enableAjaxValidation"]=> string(4) "true" ["enableClientValidation"]=> string(5) "false" ["focus"]=> string(0) "" ["actionPrefix"]=> string(0) "" ["skin"]=> string(7) "default" } 

You don’t need the form, you need the data from it which can be sent via POST.

If you want to use ajax, you need to collect all the required data using JS (as I’ve said before, you can use serialize() method) and send it using ‘data’ param of the ajax request.

Im losing it…

I don’t really need the data already encode in the CActiveForm, but I need the form to add some option depending on the user choice.

so in this case, I have my dropDownList which is listing value between 1 and 5. If the user choose the value ‘3’, three part of the form about the session will be showed and will be add to the form. these 3 part of the form have to be add in my DB like 3 record of my table session when the user click on the submit button.

maybe I go totally in the wrong way by using onchange and ajax but it’s the only things that I know until now to do that. and I don’t really understand your ajax request, even with the docs.


$.ajax({

    url: '...',

    data: $('your-form-selector').serialize(),

    ...

});

If I do that… so :




<script>

$.ajax({

    type: 'POST'

    url: 'admin/UpdateAjaxFormNbSession', //my controller and action

    data: '$form.serialize(),

});

</script>

So, the $form will be available in my controller? and will be send to the _ajaxView by using :




$this->renderPartial('_ajaxContentFormSession',

    			array('form'=>$_POST['form']),false,true);



?

and when it will be sent? on each action of what? how can I link this .ajax to the dropdownlist?

So thx you very much to all of you for help and your patience. it finally works.

what I did (If it can be help someone in the future :P )

In my main view, I put an ajax event on the onchange option of an item :




<?php

echo $form->dropDownList($eventForm, 'nbSession',

			array(

				1=>'1',

				2=>'2',

				3=>'3',

				4=>'4',

				5=>'5'

				),

				array(

				'prompt' => 'Choisissez le nombre de session d\'ateliers',

				'id' => 'nbSession',

				'class'=>'eventForm',

				'ajax' => array(

				'type' => 'POST',

				'url' => @Yii::app()->createUrl('admin/UpdateAjaxFormNbSession'),

				'data' => array('form'=>serialize($form), 'listAteliers'=>$listAteliers, 'nbSession'=>'js:this.value'),

				'update' => '#session'

													),        

)

);

?>

where I send via POST my $form (CActiveForm) to add elements, a list of objects and the value selected of my dropDownList.

In my controller :

I made my renderPartial giving the element to my _ajaxView :


$this->renderPartial('_ajaxContentFormSession',

    			array(

    					'form'=>$_POST['form'],

    					'nbSession'=>$_POST['nbSession'],

    					'listAteliers'=>$_POST['listAteliers'],

    			),

    			false,

    			true);

and finnally I take back information in the _ajaxView like this :




$form = preg_replace('!s:(\d+):"(.*?)";!e', "'s:'.strlen('$2').':\"$2\";'", $form);

$form = unserialize($form);


$nbSession;


$listAtelier;



so it works for now :) even if I’m not sure about the preg_replace function…

but thx to all for your advice.