Form with GET method

I’m trying to create a form using GET method, I have the following:

main.php:


'urlManager'=>array(

            'urlFormat'=>'get',

			'showScriptName'=>false,

		),

index.php:


<?php echo CHtml::beginForm($action='?r=search/results', $method='get'); ?>

But this does not seem to work. When I click submit it does not go to [localhost/mysite/?r=search/results], it just redisplays the page, but the form variables are appended to the original URL. If I enter the URL [localhost/mysite/?r=search/results) manually into the address bar it works fine. Any ideas]

It should read:


<?php echo CHtml::beginForm('search/results','get'); ?>

Or, if you want to keep the variable assignments in your statement, change it to this:


<?php echo CHtml::beginForm($action='search/results', $method='get'); ?>

It should still work just fine.

you should try to avoid hard coding urls. do it like this


<?php echo CHtml::beginForm(array('search/results'), 'get'); ?>

No, that does not work either. When I check the "view source" the form action is set to "/mysite/?r=search/results"

But when I submit the form it just displays the original page with the query parameters appended to the URL.

No, that does not work either. When I check the "view source" the form action is set to "/mysite/?r=search/results"

But when I submit the form it just displays the original page with the query parameters appended to the URL.

This works:


<?php echo CHtml::beginForm(array('search/results')); ?>

BUT it uses POST, I cannot get it to work with GET.

I figured out the problem - a GET form action cannot include query parameters, the form has to submit to an actual page. I did the following in my form:




<?php echo CHtml::beginForm('', 'get'); ?>

<input type="hidden" name="r" value="search/results" />



And it works OK now.

This should really have been documented somewhere, I’ve spent 2 days trying to figure it.

Were you checking for the $_GET super global in your controller?

No. As detailed in my last post the issue is to do with the form GET method not allowing query parameters in its action. That’s not related to Yii, it’s a general web limitation.

Do the URL generated by using GET method always like this?

host/path/controller/action?formmodelname[‘activetextfieldname1’]=value1&formmodelname[‘activetextfieldname2’]=value2

can i make it simpler to this?

host/path/controller/action?activetextfieldname1=value1&activetextfieldname2=value2

Thanks

rgrds,

boet




<?php echo CHtml::beginForm('','get'); ?>

  <?php echo CHtml::label('User','user01',array('class'=>'small'))?>

  <?php echo CHtml::textField('search','',array('id'=>'user01','class'=>'sText'))?>

  <?php echo CHtml::submitButton('Search',array('class'=>'butDef')); ?>

<?php echo CHtml::endForm(); ?>



Generated html is:


<form action="/admin.php?r=user/index" method="get">

  <label class="small" for="user01">User</label>

  <input id="user01" class="sText" type="text" value="" name="search">

  <input class="butDef" type="submit" name="yt0" value="Search">

</form>

In order to make it work with Yii, we need to add the hidden field:


<?php echo CHtml::hiddenField('r','user/index')?>

A GET form action cannot include query parameters. You need to use hidden variables to generate the query paramaters.

I have problem with form send with get method and URL manager. I set my urlFormat to path. here is rule:


'search-<search>'=>array('item/search', 'urlSuffix'=>'.html', 'caseSensitive'=>false),

my search form:




<?php echo CHtml::beginForm(array('item/search'), 'get'); ?>

                <?php echo CHtml::textField('search', $_GET['search'], array('size'=>60));?>

                <?php echo CHtml::submitButton('search');?>



i get this url




domain.com/item/search?search=test&yt0=search



I have changed my urlmanager rule to:


'<yt0>-<search>'=>array('item/search', 'urlSuffix'=>'.html', 'caseSensitive'=>false),

but it didnt help.

What should I do??

This URL is composed by your browser when you submit a GET form. Only way to change would be to use some custom javascript that does form submission for you. In that script you can compose your own URL.