【问题标题】:how to insert from data using ajax in codeigniter?如何在codeigniter中使用ajax插入数据?
【发布时间】:2017-07-27 06:48:54
【问题描述】:

控制器:test.php

public function signin()
{
    if($this->input->post('insert'))
    {
        $name = trim($this->input->post('name'));
        $email = trim($this->input->post('email'));
        $phone = trim($this->input->post('phone'));
        $message = trim($this->input->post('message'));
        $s_data = date('Y-m-d');

        $data = array(
                    'name' => $name,
                    'email' => $email,
                    'phone' => $phone,
                    'message' => $message,
                    's_date' => $s_date
                    ); 

        $recaptchaResponse = trim($this->input->post('g-recaptcha-response'));
        $userIp=$this->input->ip_address();
        $secret='****************************';
        $url="https://www.google.com/recaptcha/api/siteverify?secret=".$secret."&response;=".$recaptchaResponse."&remoteip;=".$userIp;
        $response = $this->curl->simple_get($url);
        $status= json_decode($response, true);

        $this->db->insert('contact',$data);
        if($status['success'])
        {     
            $this->session->set_flashdata('flashSuccess', 'successfull');
        }
        else
        {
            $this->session->set_flashdata('flashSuccess', 'Sorry Google Recaptcha Unsuccessful!!');
        }
    }
}

Ajax 代码:

<script>
    $(document).ready(function(){
        $("#insert").click(function(){
            var name = $("#name").val();
            var email = $("#email").val();
            var phone = $("#phone").val();
            var message = $("#message").val();
            var dataString = 'name='+ name + '&email='+ email + '&phone='+ phone + '&message='+ message;
            if(name==''||email==''||phone==''||message=='')
            {
                alert("Please Fill All Fields");
            }
            else
            {
                // AJAX Code To Submit Form.
                $.ajax({
                    type: "POST",
                    url: "<?php echo base_url('index.php/'); ?>test/signin",
                    data: dataString,
                    cache: false,
                    success: function(result){
                        alert(result);
                    }
                });
            }
            return false;
        });
    });
</script>

引导模式查看代码:-

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                </button>
                 <h4 class="modal-title" id="myModalLabel">header</h4>
            </div>
            <div class="modal-body">
                <div class="error"><strong><?=$this->session->flashdata('flashSuccess')?></strong></div>
                <form method="post" enctype="multipart/form-data" id="form1">
                    <input type="text" class="form-control1" id="name" name="name" placeholder="Enter Your Name">
                
                    <input type="text" class="form-control1" id="email" name="email" placeholder="Enter Your Email Id">
            
                    <input type="text" class="form-control1" id="phone" name="phone" placeholder="Enter Your Phone">
            
                    <textarea class="form-control1" name="message" id="message"  placeholder="Enter Your Message"></textarea>

                    <div class="g-recaptcha" data-sitekey="****************************"></div>                   
                    </br>
                    <input type="submit" name="insert" id="insert" value="Submit">
                </form>
                
            </div>
            <div class="modal-footer" style="background: #2874f0;padding: 25px;">
                <p>footer</p>
            </div>
        </div>
    </div>
</div>

在这段代码中,我创建了一个bootstrap modal,它在页面加载时打开,并且在模态正文中我使用 Google recaptch 创建了一个简单的查询。现在,我想在将值插入数据库后使用 ajax 插入表单数据而不加载页面,它显示成功消息。

那么,我该怎么做呢?请帮帮我。

谢谢

【问题讨论】:

  • 它将如何工作?您在表单中有submit 按钮,使用ajax 方法POST 并发送GET 等数据
  • 那么,请告诉我该怎么做? @JigarShah
  • 你在$status['success'] 的谷歌recaptcha 响应中得到了什么
  • 是的,@KundanPrasad 在 $status['success'] 我得到谷歌 recaptcha 响应

标签: php ajax codeigniter bootstrap-modal recaptcha


【解决方案1】:

步骤 1) 在 test.php 中加载数据库

public function __construct()
{
    parent::__construct();
    $this->load->database();
}

步骤2)修改

$this->db->insert('contact',$data);
  if($status['success'])
  {
    // code
  }

if($this->db->insert('contact',$data))
{
  //rest of code
}

它应该可以工作

【讨论】:

  • 请解释什么你做了改变,以及为什么。请记住,其他人应该能够从您的回答中学习
【解决方案2】:

控制器更改

注意:请阅读注释行以获得更好的理解

public function signin()
{
    if($this->input->post('insert'))
    {
        $name = trim($this->input->post('name'));
        $email = trim($this->input->post('email'));
        $phone = trim($this->input->post('phone'));
        $message = trim($this->input->post('message'));
        $s_data = date('Y-m-d');

        $data = array(
                    'name' => $name,
                    'email' => $email,
                    'phone' => $phone,
                    'message' => $message,
                    's_date' => $s_date
                    ); 

        $recaptchaResponse = trim($this->input->post('g-recaptcha-response'));
        $userIp=$this->input->ip_address();
        $secret='****************************';
        $url="https://www.google.com/recaptcha/api/siteverify?secret=".$secret."&response;=".$recaptchaResponse."&remoteip;=".$userIp;
        $response = $this->curl->simple_get($url);
        $status= json_decode($response, true);


        if($status['success'])
        {  
          //insert data when get success in google recaptcha
          $this->db->insert('contact',$data);   
          $res['msg']="successfull";
          //no need to set flash session in CI for ajax
            //$this->session->set_flashdata('flashSuccess', 'successfull');
        }
        else
        {
            //$this->session->set_flashdata('flashSuccess', 'Sorry Google Recaptcha Unsuccessful!!');
            $res['msg']="Sorry Google Recaptcha Unsuccessful!!";
        }
        //set page header as json format
        $this->output
        ->set_content_type('application/json')
        ->set_output(json_encode($res));
    }
}

更改脚本

<script>
    $(document).ready(function(){
        $("#insert").click(function(){
            var name = $("#name").val();
            var email = $("#email").val();
            var phone = $("#phone").val();
            var message = $("#message").val();
            //pass object in post
            var dataString = {'name': name , 'email': email , 'phone': phone , 'message': message};
            if(name==''||email==''||phone==''||message=='')
            {
                alert("Please Fill All Fields");
            }
            else
            {
                // AJAX Code To Submit Form.
                $.ajax({
                    type: "POST",
                    url: "<?php echo base_url('index.php/'); ?>test/signin",
                    data: dataString,
                    dataType: 'json',
                    cache: false,
                    success: function(result){
                        alert(result.msg);
                    }
                });
            }
            return false;
        });
    });
</script>

谢谢

【讨论】:

  • 当我检查recaptcha时,它建议我选择街道标志或道路或汽车,当我选择它并想要验证它时请再试一次为什么?@Kundan Prasad
  • 和提交按钮无法正常工作,它的行为类似于禁用
  • 请在浏览器的控制台检查可能是谷歌recaptcha的错误
猜你喜欢
  • 2015-09-14
  • 1970-01-01
  • 1970-01-01
  • 2022-06-23
  • 2023-03-20
  • 2020-12-17
  • 2018-11-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多