Yii2: Pjax duplicates parameters and converts space to +

Created on 8 Jul 2016  路  24Comments  路  Source: yiisoft/yii2

I have two problems with pjax and maybe someone can help me. I have one pjax form which with get params reload grid view. And grid view have pjax filters which reload grid again. So my problems is:

  1. if I submit form it adds all params to url and convert space to "+" like this "?name=my+name". But now if I use grid filter it add new params to url and convert "+" to "?name=my%2Bname&search=test". So in result php will get "my+name" instead of "my name".
  2. second problem is duplicate url params. If I use my form and grid filters many times, I get duplicate params in url like this "?name=&name=&search=&search=".

Any ideas how to fix any of these issues?

Additional info

| Q | A |
| --- | --- |
| Yii version | 2.0.8 |

pjax bug

Most helpful comment

What about Encoding you use on your browser, UTF-8 or what?
What your regional settings, if you use Windows(tm) OS?
php version?
yii2-pjax version?
can you use latest yii2 version, yii2 v2.0.9?
kindly submit your output phpinfo() result page.
On your dev-tools (chorme, edge, mozilla), Request & Response Headers, especially for pjax / xhr request.
Please run requirements.php in your yii2 installations.

See image below, either by + or %20, the result are same on me.
edge

All 24 comments

Seems you do it wrong. I had this problem to but because my setup (both on widget and layout) is wrong. Please share your view code.

Here is my example:
search.zip

Tell me what I am doing wrong.

@DMGPage

Your code was:

<?= Html::beginForm('', 'get', ['class' => 'form-horizontal', 'id' => 'search-form', 'data-pjax' => true]); ?>

you need to specify target form-action in array [] if you want to target same as action. change to:

<?= Html::beginForm([''], 'get', ['class' => 'form-horizontal', 'id' => 'search-form', 'data-pjax' => true]); ?>

Quite weird for me observing this behavior. Becase on docs it says array|string (see here)

Why? Form will call Url:to() and from documentation: "an empty string: the currently requested URL will be returned".

And how does your suggestion fix any of my problems with pjax?

Have you try to use [''] ?

Yes, and from, what I see, it fix duplicates parameters in url. Only problem, that I can not find any explanation, why [''] have different behaviour from ''. Even returned urls are equal, but with '' pjax duplicates params and with [''] will replace.

But first pjax problem remains. It still replace empty space with +.

But first pjax problem remains. It still replace empty space with +.

Is all fields empty?

As far as I know, space will be converted to + based on URL Encoding.
http://stackoverflow.com/questions/1634271/url-encoding-the-space-character-or-20
http://www.blooberry.com/indexdot/html/topics/urlencoding.htm

On my browser, Chrome & Edge works fine.

Chrome
chrome

Edge
edge

It is fine that space is converted to +. Problem is next time when pjax is called, because it replace + with "%2B" and now $_GET param has value "+" instead of space.

  1. enter in form "first name" and submit form: "?first_name=first+name&last_name=&email=&address=";
  2. enter in grid filter phone and pjax change url to: "?phone=123&first_name=first%2Bname&last_name=&email=&address=";
  3. echo $_GET['first_name']; returns "first+name".

Works fine:
Works fine


Consider using Yii::$app->request->getQueryParams(); instead $_GET
I think your issue now is not related by Yii code, but rather than your system environment setup / settings.

Take my code, if you wondering https://gist.github.com/agungsijawir/c6017d7c0840e1200cc3d26c9858d3eb

Yes this change everything :)
public function getQueryParams() { if ($this->_queryParams === null) { return $_GET; } return $this->_queryParams; }

Btw in my example I use "Yii::$app->request->get('first_name')" and result is wrong:
pjax

And from your result I see that you did only 1. step and did not searched in grid. Problem only appear after second pjax call when it takes current url and add new params.

I did not only one or two pjax call, but more than that. The result are same, works perfect.
either on form on filter on grid. same result, works as expected.

check your environment setup / config.

edge

Please close this issue as this not related to yii code.

Wait what environment settings should I check? I did clean yii installation and I still have same problem.

What about Encoding you use on your browser, UTF-8 or what?
What your regional settings, if you use Windows(tm) OS?
php version?
yii2-pjax version?
can you use latest yii2 version, yii2 v2.0.9?
kindly submit your output phpinfo() result page.
On your dev-tools (chorme, edge, mozilla), Request & Response Headers, especially for pjax / xhr request.
Please run requirements.php in your yii2 installations.

See image below, either by + or %20, the result are same on me.
edge

_What about Encoding you use on your browser, UTF-8 or what?_
Yes, utf-8

_What your regional settings, if you use Windows(tm) OS?_
Windows 7 - language for non-unicode programs: Latvian (Latvia)

_php version?_
info.pdf

_yii2-pjax version?_
version what comes with yii 2.0.9

_On your dev-tools (chorme, edge, mozilla), Request & Response Headers, especially for pjax / xhr request._
request.pdf

_Please run requirements.php in your yii2 installations._
requirements.pdf

_See image below, either by + or %20, the result are same on me._
In my case + is replaced with %2B and it happens in firefox and chrome. Tested even on linux mint + firefox and same problem. So it is not browser or OS problem.

Can you change your Regional Settings to US?

I did test on Firefox too, but behavior was same.
edge

Edited:
Even on Xubuntu 14.04 using Firefox, works as expected.
edge

Please update your Regional Settings.


Ok, make it simple to debug.
Use this simple plain php, html and jquery, jquery-pjax
I got this, what about you?

pjax

Wait you have name in form and name in grid? Of Course you will have different result. Could you test with my view file? Or at least use different names and follow steps described above.

_Can you change your Regional Settings to US?_
linux mint 17 3

here, with no model. same code with yours except CGridView components.
https://github.com/yiisoft/yii2/issues/11921#issuecomment-231980458

Here #11921 you do not have grid filter result combined with form params. Your url contains only form fields and this request works for me too. But if then I enter something in grid filter, data is changed. Well everything you can see in request.pdf. There is 2 requests: 1. after form submit; 2. after grid filter.

_same code with yours except CGridView components._
Use GridView, it does not change anything. In clean yii I use GridView too, and result is same. But don't ignore this part, because my problem is only second pjax request, which comes from grid filter. Form by it self works good.

Ah, I see...

To produce this issue:

  • Use Html::beginForm() and Html::endForm(), put an input box (named it first_name) and wrap the form with pjax.
  • Write GridView with filter enabled and wrap it with pjax.
  • Perform submit form with space on textbox, i.e this is test. Url with get params will change to this+is+test.
  • Perform filter on GridView filter (i.e on Something field on MyModel model) with space on string field, i.e. find a thing, enter.
  • Url will changed to controller/action?MyModel[Something]=find+a+thing&first_name=this%20is%20test . When dump \Yii::$app->request->get('first_name'), output will be find+a+thing.

note:

  • If form model and GridView searchModel is different, issue is reproduced. Even if using Model that extends from \yii\base\Model for use in search form.
  • Both ActiveForm and Html::beginForm()
  • Yii2 v2.0.9
  • php5.5.37 & php5.6.181+deb.sury.org~trusty+1

Can you use same model as GridView on your search form?

Yes, and result not changed. Here is my test view, model and controller:
search.zip

ezgif-311761952

Failed to reproduce (open GIF in new tab)

Test is performed differently than I described and @agungsijawir reproduced. From your gif I can see that you @SilverFire did not entered any text in first form. Please read accurately @agungsijawir comment how to reproduce this issue.

In short:

  1. enter text with space in form input and sumbit form;
  2. enter text with space in grid filter and submit filter.

@DMGPage thank you for pointing out my mistake

Was this page helpful?
0 / 5 - 0 ratings