yii包括ajax的php文件.. php有yii形式

时间:2014-03-22 12:19:14

标签: php yii

我有一个带有ajax提交按钮的yii表单我想在我的主页上包含这个表单当我点击按钮时我写了我的代码:     

    $.ajax({
        type: 'POST',
        data: x,
        url:'<?php echo yii::app()->createAbsoluteUrl("site/info_form");?>',
        scriptCharset:"application/x-www-form-urlencoded; charset=UTF-8",
        success: function(get)
        {
        document.getElementById("form1").innerHTML=get;

        }
    });
});
</script>

info_form使用ajax提交按钮获取表单..当点击提交时,它无法执行ajax

我的控制员:

public function actionAdd_main_info()     {

    $model_info=new FreeMainInfo;
    $user=Yii::app()->session['user_id'] ;



     if(isset($_POST['FreeMainInfo']))   
{
    $model_info->attributes=$_POST['FreeMainInfo'];
    if($model_info->validate())
    {
      $model_info->full=1;
      $model_info->reg_id=$user;

      $model_info->save(false);
      echo CJSON::encode(array('status'=>'success' ));
      Yii::app()->end();

    }
     else
     {
      $error = CActiveForm::validate($model_info);
      if($error!='[]')
      echo $error;
      Yii::app()->end();
      }


}
}

我用ajax生成的表单:

                                <?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'main_info',
    'action'=>Yii::app()->createUrl('//site/add_main_info'),
    'enableAjaxValidation' => false,
    'clientOptions' => array(
        'validateOnSubmit' => false,
    ),
    'htmlOptions'=>array('class'=>'form-horizontal SearchForm'),
)); ?>                  

我的ajax提交按钮:

    echo CHtml::ajaxSubmitButton('Save Your Main Info',CHtml::normalizeUrl(array('site/add_main_info','render'=>true)),
                                 array(
                                         'type'=>'POST',
                                         'dataType'=>'json',
                                         'data'=>'js:$("#main_info").serialize()',
                                         'success'=>'function(data) {

                                             if(data.status=="success"){
                                             $("#form1").remove(); 
                                             $("#alert1").remove(); 
                                            }
                                             else{
                                            $.each(data, function(key, val) {
                                            $("#"+key+"_em_").text(val);                                                    
                                            $("#"+key+"_em_").hide();
                                            $("#"+key+"_em_").text(val);                                                    
                                            $("#"+key+"_em_").show();
                                            });
                                        }       
                                    }',  
                                     ),array('id'=>'submit','class'=>'btn btn btn-primary'));

2 个答案:

答案 0 :(得分:0)

(这不是你问题的实际答案,但希望这有助于你)

成功后把这个失败的功能弄清楚是什么错误?

error: function(data) { // 500 Status Header
        var data = $.parseJSON(data);
        $.each(data.errors, function(index, value) {
            alert(value);
        });
    },

答案 1 :(得分:0)

在Yii中,我们有AjaxButton(),它为您提供Ajax的所有提示器,我创建了示例来帮助您,在此示例中dataType是JSON

查看:

 <?php
            echo CHtml::ajaxSubmitButton($model->isNewRecord ? Yii::t('app','Create') : Yii::t('app','Save'),$model->isNewRecord ? array('questions/create') : array(Yii::app()->controller->id . '/update?id=' . $model->q_id),array(
            'dataType'=>'json',
            'success'=>'function(data) {
                     $("#AjaxLoader").hide();
                    if(data.status=="success"){

                   // do something here 

                    }
                    else{
// print error messages 
                        $.each(data, function(key, val) {
                            $("#questions-form #"+key+"_em_").text(val);
                            $("#questions-form #"+key+"_em_").show();

                        });
                    }
                }',
            'beforeSend'=>'function(){
    $("#AjaxLoader").show();
}'
            ),array('class'=>'btn btn-primary', 'id'=>'slink-btn'.uniqid()));
        ?>

控制器:

<?php
...
public function actionCreate()
    {
        $this->pageTitle = Yii::t('app','create');
        $model=new Questions;

        // Uncomment the following line if AJAX validation is needed
        $this->performAjaxValidation($model);

        if(isset($_POST['Questions']))
        {
            $model->attributes=$_POST['Questions'];
            $model->user_id=uid();
            if ($model->validate()) {
                $model->save(false);

                echo CJSON::encode(array('status' => 'success'));
                Yii::app()->end();
            } else {
                $error = CActiveForm::validate($model);
                if ($error != '[]')
                    echo $error;
                Yii::app()->end();
            }
        }

        if (Yii::app()->request->getIsAjaxRequest())
            echo $this->renderPartial('_form', array('model' => $model), true, true); //This will bring out the view along with its script.

        echo $this->renderPartial('_form', array('model' => $model), true, true); //This will bring out the view along with its script.
    }
...

&GT;

对于您的问题,您需要将表单编辑为:

<?php $form=$this->beginWidget('CActiveForm', array(
        'id'=>'events-serach-form',
        'action'=>Yii::app()->createUrl('//index.php/site/search'),
        'enableAjaxValidation' => true,
        'clientOptions' => array(
            'validateOnSubmit' => true,
        ),
        'htmlOptions'=>array('class'=>'form-horizontal SearchForm'),
    )); ?>

参考文献:

http://www.codexamples.com/90/chtml-ajaxsubmitbutton/

http://www.yiiframework.com/doc/api/1.1/CHtml