Codeigniter,在控制器

时间:2017-01-30 22:49:12

标签: php codeigniter codeigniter-2 codeigniter-3

我在各种网页上搜索,但没有人清楚或解决我的问题......我和专家一起来,因为我需要你的帮助...... :(

这是我的用户控制器方法

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class UserController extends CI_Controller {

function __construct()
{
 parent::__construct();
}

function new_user_register(){
$this->form_validation->set_rules('primernombre', 'First Name','trim|required|alpha|min_length[3]|max_length[100]|xss_clean');
$this->form_validation->set_rules('primerapellido', 'Last Name', 'trim|required|alpha|min_length[3]|max_length[100]|xss_clean');
$this->form_validation->set_rules('nombreusuario','Username','trim|required|xss_clean');
$this->form_validation->set_rules('email','Email','trim|required|valid_email|is_unique[cat_tbl_usuario.email]|xss_clean');
$this->form_validation->set_rules('clave','Password','trim|required|matches[claveConfirmacion]|md5');
$this->form_validation->set_rules('claveConfirmacion','Confirm Password','trim|required|md5|matches[clave]');
if ($this->form_validation->run() == FALSE) {
  echo "<script language=\"javascript\">
      swal('Please adjust the values in user' , 'Bad data format', 'error');
    </script>";
  }
}
?>

基本上我将我的视图加载到iFrame(FANCYBOX)中,然后单击OK按钮,调用Method new_user_register,如果验证为false,那么SWEETALERT应该出现错误,除了在控制台说swald没有定义。我搜索的例子对我来说不太清楚。

2 个答案:

答案 0 :(得分:2)

以举例来说,将数据发送到数据库

if($_POST)
        {
            $config=array(
                array(
                    'field' =>  'parent',
                    'label' =>  'Parent',
                    'rules' =>  'trim|required'
                ),
                array(
                    'field' =>  'name',
                    'label' =>  'Name',
                    'rules' =>  'trim|required'
                )
            );
            $this->form_validation->set_rules($config);
            if($this->form_validation->run()==false)
            {
                $data['errors']=validation_errors();
                $data['parents']=$this->admin_model->getMenuParents();
                $data['title']='Master Cook | Kitchen';
                $this->load->view('static/head',$data);
                $this->load->view('static/header');
                $this->load->view('static/sidebar');
                $this->load->view('admin/add_menu');
                $this->load->view('static/footer');
            }
            else
            {
                $this->admin_model->addMenuItem($_POST);
                $data['success']='Congratulations! Menu Item Added Successfully';
                $data['parents']=$this->admin_model->getMenuParents();
                $data['menu']=$this->admin_model->getMenuItems();
                $data['title']='Master Cook | Kitchen';
                $this->load->view('static/head',$data);
                $this->load->view('static/header');
                $this->load->view('static/sidebar');
                $this->load->view('admin/add_menu');
                $this->load->view('static/footer');
            }
        }
        else
        {
            $data['parents']=$this->admin_model->getMenuParents();
            //echo '<pre>';print_r($data);exit;
            $data['title']='Master Cook | Kitchen';
            $this->load->view('static/head',$data);
            $this->load->view('static/header');
            $this->load->view('static/sidebar');
            $this->load->view('admin/add_menu');
            $this->load->view('static/footer');
        }

在视图中使用Sweet Alerts。例如,我正在呈现管理项目的视图。像这样

Data Table

它的外观

Sweet Alert

以下是调用甜蜜警报的代码

<?php for($i=0;$i<count($menu_items);$i++){?>
                                <tr>
                                    <td data-order="Jessica Brown">
                                        <strong><?php echo $i+1;?></strong>
                                    </td>
                                    <td class="maw-320">
                                        <span class="truncate"><?php echo $menu_items[$i]['name']?></span>
                                    </td>
                                    <td><?php echo $menu_items[$i]['parent']?></td>
                                    <td><?php echo $menu_items[$i]['class']?></td>
                                    <td><?php echo $menu_items[$i]['url']?></td>
                                    <td>
                                        <a href="<?php echo base_url().'admin/edit_admin_menu/'.$menu_items[$i]['id'];?>" class="btn btn-default"><i class="icon icon-pencil"></i></a>
                                   // Call Sweet Alert     
                                   <button class="btn btn-danger" onclick="validate(this)" value="<?php echo $menu_items[$i]['id']?>"><i class="icon icon-times"></i></button>
                                    </td>
                                </tr>
                            <?php }?>

这是呈现甜蜜警报的功能

<script src="<?php echo base_url()?>assets/js/sweetalert.min.js"></script>
<script>
$(function(){ TablesDatatables.init(); });
function validate(a)
{
    var id= a.value;

    swal({
            title: "Are you sure?",
            text: "You want to delete this Menu Item!",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "Yes, Delete it!",
            closeOnConfirm: false }, function()
        {
            swal("Deleted!", "Menu Item has been Deleted.", "success");
            $(location).attr('href','<?php echo base_url()?>admin/del_admin_menu/'+id);
        }
    );
}
 </script>

答案 1 :(得分:1)

您编写的代码不是专业方法,请尝试使用ajax提交您的表单并在ajax成功中调用swal(sweetalert)。

第1步: 不要在控制器构造中加载库form_validation作为...

$this->load->library('form_validation');

$this->load->helper('form');

步骤:2 在编写ajax提交之前,在页眉或页脚中集成了SweetAlert,如

<script src="dist/sweetalert.min.js"></script>
<link rel="stylesheet" type="text/css" href="dist/sweetalert.css">

步骤:3 控制器代码示例为

function MethodName {
$this->form_validation->set_error_delimiters('', '');
$this->form_validation->set_rules('fname','First Name', 'required');
$this->form_validation->set_rules('lname','Last Name', 'required');
$this->form_validation->set_rules('email','Email Address','required|valid_email|is_unique[sec_users.email]');
if ($this->form_validation->run() == FALSE) {
    echo validation_errors();
} 
else {
  // To who are you wanting with input value such to insert as 
  $data['frist_name']=$this->input->post('fname');
  $data['last_name']=$this->input->post('lname');
  $data['user_name']=$this->input->post('email');
  // Then pass $data  to Modal to insert bla bla!!
   }
}

第4步: 最后,ajax中的SweetAlert提交。

<script type="text/javascript"> 
  $(document).ready(function(){
    var dataString = $("#FormId").serialize();
    var url="ControllerName/MethodName"
        $.ajax({
        type:"POST",
        url:"<?php echo base_url() ?>"+url,
        data:dataString,
        success:function (data) {
            swal(data);
        }
        });     
  })
</script>

这些指南将如何发挥作用。

谢谢!