Jithin Varghese Jithin Varghese - 1 year ago 32
PHP Question

form submission using JQuery Ajax not working in server but working in localhost codeigniter

form submission using Jquery and Ajax not working in server but it works perfectly in localhost. Whats the reason for this. I using Codeigniter framework.

View


//order.php

<form action="" method="post" id="form_order">
<p><input type="text" name="due_date" /></p>
<p><input type="text" name="name" /></p>
<p><button type="submit">SAVE</button></p>
</form>

<script>
$(document).ready(function(){

(function($) {
"use strict";

$(function() {
$('#form_order').validate({
rules: {
due_date: {
required: true
},
name: {
required: true
}
},
messages: {
due_date: {
required: "date required"
},
name: {
required: "name required"
}
},
$('#loading').show();

$(form).ajaxSubmit({
type:"POST",
data: $(form).serialize(),
url:"<?php echo base_url("ordercode"); ?>",
success: function(data) {
$('#loading').hide();
$('#success').show();
$('#success').html(data);
},
error: function() {
$('#loading').hide();
$('#success').show();
$('#success').html('Something went wrong. Tryagain later!');
}
});
}
});
});

})(jQuery)
});
</script>


controller


//Ordercode.php

if($this->input->post('new_order')) {
$due_date = $this->input->post('due_date');
$name = $this->input->post('name');

$add = $this->orders->addOrder($due_date, $name);

if($add === TRUE) {
echo 'Order Added.';
}
else if($add === FALSE) {
echo 'Something went wrong. Tryagain later!';
}
else {
echo 'Something went wrong. Tryagain later!';
}
}


Model


//Orders.php

public function addOrder($due_date_first, $name) {
$data = array(
'order_due_date' => $due_date_first,
'order_name' => $name
);
$add = $this->db->insert('tbl_order', $data);

if($add == 1) {
return TRUE;
}
else {
return FALSE;
}
}


The above code works perfectly in localhost but not in server. Server always returns error.

Output


Server :
Something went wrong. Tryagain later!

Localhost :
Order Added.

Is there any problem in my coding. Why it's not working in server. Is there any solution. Thankyou.

Answer Source
$(document).ready(function(){
    (function($) {
        "use strict";
        $(function() {
            $('#form_order').validate({
                rules: {
                    due_date: {
                        required: true
                    },
                    name: {
                        required: true
                    }
                },
                messages: {
                    due_date: {
                        required: "date required"
                    },
                    name: {
                        required: "name required"
                    }
                },
                submitHandler: function(form,e) {
                    e.preventDefault();
                    var formData = new FormData(form);
                    $('#loading').show();
                    $.ajax({
                        type:"POST",
                        data: formData,
                        url:"<?php echo base_url("ordercode"); ?>",
                        success: function(data) {
                            $('#loading').hide();
                            $('#success').show();
                            $('#success').html(data);
                        },
                        error: function() {
                            $('#loading').hide();
                            $('#success').show();
                            $('#success').html('Something went wrong. Tryagain later!');
                        }
                    });
                }
            });
        })(jQuery);
    });
});

And remove action="" attribute in form tag

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download