【问题标题】:yii include php file by ajax .. php have yii formyii 包含 ajax 的 php 文件 .. php 有 yii 形式
【发布时间】:2014-03-22 12:19:13
【问题描述】:

我有带有 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

我的控制器:

公共函数 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'));

【问题讨论】:

    标签: php yii


    【解决方案1】:

    (这不是您问题的实际答案,但希望对您有所帮助)

    成功后把这个失败函数放上去看看有什么问题?

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

    【讨论】:

    • 感谢您的回复,但在 m 代码中我可以包含 (info_form . php) 但是当我获取表单并单击提交时,转到 (site/add_main_info) 并回显 json 事件成功或失败不制作 ajax 代码
    【解决方案2】:

    在 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.
        }
    ...
    

    ?>

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

    <?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

    【讨论】:

    • 我使用 ajax 提交按钮创建了我的 ajax 表单,它在同一页面上运行良好.. 当我想通过 jquery 生成它时.. 我将它隔离在另一个页面中并由 ajax 调用它.. 当我点击我的按钮我得到了这个表格,但不像在同一页面上那样工作..我希望你理解先生
    • @Ali Adel 尝试将控制器路径添加到您的表单:'action'=>Yii::app()->createUrl('//index.php/site/search'),完整阅读我的回答。
    • 我在白页中的输出 {"FreeMainInfo_first_name":["First Name cannot be blank."],"FreeMainInfo_last_name":["Last Name cannot be blank."],"FreeMainInfo_address":["地址不能为空。"],"FreeMainInfo_city":["城市不能为空。"],"FreeMainInfo_state":["州不能为空。"],"FreeMainInfo_post_code":["邮政编码不能为空。"] }
    • @Ali Adel 很好,所以你的第一个问题已经解决了,你需要停止验证或使用场景。
    • 我的控制器 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->保存(); echo CJSON::encode(array('status'=>'success')); Yii::app()->end(); } else { $error = CActiveForm::validate($model_info); if($error!='[]') 回显 $error; Yii::app()->end(); } } }
    猜你喜欢
    • 2011-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-23
    相关资源
    最近更新 更多