javascript在成功函数后返回数据

时间:2016-05-31 06:28:02

标签: javascript jquery mysql ajax codeigniter

我想使用ajax函数向mysql提交一个表单,但是我的尝试在mysql表中给了我NULL结果。

这是我的javascript:



function pesan()
    { 
        email = $("#email").val(); 
        from_nama = $("#from_nama").val(); 
        from_phone = $("#from_phone").val(); 

        $.ajax
        ({
            url : "<?php echo site_url('kirim/undangan')?>/",
            type: "POST",
            dataType: "JSON",
            success: function(data)
            {       
               $('#alert').show();
               $('#email'+data).html(data.email);
               $('#from_nama'+data).html(data.from_nama);
               $('#from_phone'+data).html(data.from_phone);
            },
            error: function (jqXHR, textStatus, errorThrown)
            {
                alert('Error upload data');
            }

        });
    }
&#13;
&#13;
&#13;

形式:

&#13;
&#13;
        <h4 id="form">Data Personal</h4>

                            <div class="col-sm-4">
                                <input type="email" class="form-control input-lg" id="email"  name="email"  placeholder="Email" required>
                            </div>

                            <div class="col-sm-4">
                                <input type="text" class="form-control input-lg"  id="from_nama" name="from_nama" placeholder="Nama" required>
                            </div>

                            <div class="col-sm-4">
                                <input type="number" class="form-control input-lg"  id="from_phone"  name="from_phone" placeholder="Phone" required>
                            </div>
                            <div>
                               <input type="hidden" name="id" id="id" />
                            </div>
                            <br>
                            <div class="row" align="center">
                                
                                    <button id="pesan" type="button" class="btn btn-download btn-md" onclick=pesan()>
                                    <span class="glyphicon glyphicon-send" aria-hidden="true" ></span>
                                    Pesan
                                    </button>
&#13;
&#13;
&#13;

这是控制器:

&#13;
&#13;
function undangan()
	{	
			$email          =   $this->input->post('email');
			$from_nama		=   $this->input->post('from_nama');
			$from_phone		=   $this->input->post('from_phone');

			$data_user = array(

				'email'			 				=> $email,
				'name'      	 				=> $from_nama,
				'phone' 			 			=> $from_phone,
				'status'           	 			=> '0',
				'unique_id' 	        		=> uniqid()

			    );

            $this->load->model('excel');
            
			$this->excel->tambahuser($data_user);
       
		 	
		$this->load->view('kirimundangan.php',$data);
	} 
&#13;
&#13;
&#13;

模特:

&#13;
&#13;
function tambahuser($data_user)
        {
            $this->db->insert('request', $data_user);
            $this->db->insert_id();
            
            foreach ($data_user as $key)     
            {  
                $data = array(
                    
                    'from_name'          =>   $this->input->post('from_nama'),
                    'from_phone'         =>   $this->input->post('from_phone')
                );
            }
             
        }
&#13;
&#13;
&#13;

我认为我在成功编写代码时犯了错误:函数(数据),有什么帮助吗?

1 个答案:

答案 0 :(得分:1)

将peson功能更改为

function pesan()
    { 
        var email = $("#email").val(),
          from_nama = $("#from_nama").val(),
          from_phone = $("#from_phone").val(); 

        $.ajax
        ({
            url : "<?php echo site_url('kirim/undangan')?>/",
            type: "POST",
            dataType: "JSON",
            data:{from_nama: from_nama, email: email, from_phone: from_phone},
            success: function(data)
            {       
               $('#alert').show();
               $('#email'+data).html(data.email);
               $('#from_nama'+data).html(data.from_nama);
               $('#from_phone'+data).html(data.from_phone);
            },
            error: function (jqXHR, textStatus, errorThrown)
            {
                alert('Error upload data');
            }

        });
    }

并且您期望json响应,因此您必须更改服务器端代码,并且从那里您必须使用json而不是视图返回数据。 我希望这对你有用