doutangtan6386 2015-03-02 12:08
浏览 43
已采纳

使用CActiveForm从URL中排除空表单参数

I am relatively new to both Yii and PHP and I have some problems while composing user-friendly URLs.

I want to create an URL from user-entered parameters in a form which is an extension of CFormModel. I intially chose the GET method instead of POST because the user should be able to bookmark the URL in order to return to the same search results later on.The user must specify (at least) one parameter for the search but since there are many possible parameters in the form, I want to shorten the URL by including only non-empty parameters and their values, e.g.,

http://localhost/search/results?name=John&country=Ireland

instead of

http://localhost/search/results?name=John&family=&country=Ireland&yt0=Search

(If someone knows how to exclude the button ID and label “yt0=Search”, that would quite nice, too.) I understand that passing all GET parameters to the URL is the standard behaviour of HTML forms and cannot be changed by using only PHP. Now I had the idea to add a JavaScript function that checks for all form parameters whether their values are empty after the form is submitted. If a parameter value is empty, the name of the parameter is set to an empty string (as suggested here) which effectively removes the empty parameters from the URL:

function myFunction()
{
    var myForm = document.getElementById('form-id');
    var allInputs = myForm.getElementsByTagName('input');
    var input, i;

    for(i = 0; input = allInputs[i]; i++) {
        if(input.getAttribute('name') && !input.value) {
            input.setAttribute('name', '');
        }
    }
}

However, I am not sure where to call this function (as opposed to “onsubmit” for a standard HTML form) and how to reference the form parameters since I am not yet familiar with CFormModel/CActiveForm. Any help would be greatly appreciated!

This is the (simplified) form model:

class SearchForm extends CFormModel {

    private $_parameters = array (
        'firstName' => array (
            'type' => 'text',
            'config'=>array('name'=>'name'),
        ),
        'familyName' => array (
            'type' => 'text',
            'config'=>array('name'=>'family'),
        ),
        'country' => array (
            'type' => 'text',
            'config'=>array('name'=>'country')
        ),
    );

    public $firstName;
    public $familyName;
    public $country;

    public function getParameters() {
        return $this->_parameters;
    }
}

This is the relevant part of the view:

$elements = $model->getParameters ();

$form = $this->beginWidget ( 'CActiveForm', array (
    'method'=>'get', 
    'enableAjaxValidation' => false 
    )
);

This is the action part of the controller:

public function actionResults() {
    $model = new SearchForm ();

    $filters = array ();
    if (isset ($_REQUEST['name'])){
        $filters['firstName'] = $_REQUEST['name'];
    }
    if (isset ($_REQUEST['family'])){
        $filters['familyName'] = $_REQUEST['family'];
    }
    if (isset ($_REQUEST['country'])){
        $filters['country'] = $_REQUEST['country'];
    }

    if ($filters) { 
        $model->attributes = $filters;

        if ($model->validate ()) {
            // search action
        }   
    }
}

(I asked a similar but less specific question two weeks ago here.)

  • 写回答

1条回答 默认 最新

  • dongquxiao8545 2015-03-09 17:23
    关注

    You can achieve this by creating two actions in your controller.

    class SearchController extends Controller {
    
    function actionFormhandler() {
    
        $formValues    = $_POST;
        $argName       = $_POST['name'];
        $argCountry    = $_POST['name'];
        // and other statements
    
        // Now redirect
        $this->redirect(array('/search/results',
            array('id'      => $argName,
                  'country' => $argCountry
        ));
    }
    
    
    function actionResults() {
       // do your thang here.
    }
    
    }
    
    • The first action receives a post, from your form, and contains all possible data in the $_POST variable.
    • The second action processes a GET, which contains the relevant URL components. This is your final destination, and is redirected to by the post handler action.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题