0

I'm trying to post data on my HTML code to CI with Ajax. But I got no response?

Here is my JS Code

$(document).ready(function(){
$("#simpan").click(function(){
    nama_pelanggan = $("#nama_pelanggan").val();
    telp = $("#telp").val();

    jQuery.ajax({
        type: "POST",
        url: "http://192.168.100.100/booking_dev/booking/addBookingViaWeb/",
        dataType: 'json',
        data : {
            "nama_pelanggan":nama_pelanggan,
            "telp":telp,
        },
        success: function(res) {
            if (res){
                alert(msg);
            }
        }
    });     
});
});

And here is my form

<form>
Nama Pelanggan <br> 
<input type="text" name="nama_pelanggan" id="nama_pelanggan"><br>
Telepon<br>
<input type="text" name="telp" id="telp"><br>
<input type="button" name="simpan" id="submit" value="Simpan">
</form>

and here is my contoller function code

public function addBookingViaWeb(){
    $data = array(
        'nama_pelanggan' => $this->input->post('nama_pelanggan'),
        'telp'=>$this->input->post('telp')
    );
    echo json_encode($data);
}

Here is my post param

Here my postparam

But I got no response

response param

any idea?

11
  • Try using crossDomain :true in ajax.... if you are performing a cross domain exchange.... And alert(msg) is not gonna give you anything instead of error... :) Commented Apr 27, 2016 at 4:52
  • Have tried to change this alert(msg); to alert(res); ? What about alert(res.responseText); ? Commented Apr 27, 2016 at 5:09
  • try echo json_encode($data);exit; Commented Apr 27, 2016 at 5:37
  • are you sure $data to be a valid array?If you can run this code in non ajax way try print_r($data) Commented Apr 27, 2016 at 5:51
  • still no response :( Commented Apr 27, 2016 at 5:57

4 Answers 4

0

add method in from if you use post then

<form method="post"  action ="" >
Sign up to request clarification or add additional context in comments.

1 Comment

hi, I need to post the data with ajax.
0

Try using JQuery form serialize() to declare which data you want to post. It automatically put your form input into ajax data. Example :

first set ID to your form tag

<form id="form">

then

$.ajax({
    type:'POST',
    url : 'http://192.168.100.100/booking_dev/booking/addBookingViaWeb/',
    data:$('#form').serialize(),
    dataType:'JSON',
    success:function(data){
         console.log(data);
    }
});

5 Comments

Dude take a look at his onclick method. The ajax method is never even fired.
@fruitoftheloins it does..you can see the status code 200 there.
sorry I copied the wrong code for submit id xD, but it still give no response when I add serialize() :(
@techie_28 any Idea? Cause the code is normal if I put the code inside CI
@newbie I have another solution , but it little bit native. You can try using $data = json_decode(file_get_contents('php://input'));. Then use $data->nama_pelanggan to get nama_pelanggan value. I hope it will works
0

First problem I see is in your ajax submission code. Change

$("#simpan").click(function(){

to

$("#submit").click(function(event){

Notice that I added the event parameter. You now need to prevent the default submission behavior. On the first line of your click method add

event.preventDefault();

Now I'm assuming that your url endpoint http://192.168.100.100/booking_dev/booking/addBookingViaWeb/ can handle POST requests. Usually this is done with something like PHP or Ruby on Rails. If I was doing this in PHP I would write something like the following:

<?php
    $arg1 = $_POST["nama_pelanggan"];
    $arg2 = $_POST["telp"];

    // do something with the arguments

    $response = array("a" => $a, "b" => $b);
    echo json_encode($response);
?>

I personally don't know anything about handling POST requests with js (as a backend) but what I've given you should get the data over there correctly.

1 Comment

I've declare it on my controller function
0

I got solution for my problem from my friend xD just add header("Access-Control-Allow-Origin: *"); on controller function

Thank you for helping answer my problem.

1 Comment

nobody knew here that you were doing a cross domain call.. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.