Mai Munah Mai Munah - 6 months ago 10
MySQL Question

javascript return data after success function

I want to submit a form using ajax function to mysql, but my attempt gives me the NULL results in mysql table.

Here's my 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');
}

});
}





And the form :



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





Here's the controller :



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);
}





The model :



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')
);
}

}





I think I made mistake in writing code inside the success:function(data), any help?

Answer

Change peson function to

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');
            }

        });
    }

and you expecting a json response so you have to change your server side code as well and from there you have to return data with json not with a view. I hope this will work for you