0

I have a HTML page out side my code igniter setup. Say abc.com is my CI based CMS website, I have abc.com/trade as my lead capture page. This HTML page(index.html) under trade folder. i cant put it in CI, so this is a stand alone page and my Apache is displaying this index.html. So far so good. Now i have a Contact us form in this html page i am using Jquery ajax to post it to the controller. Data is not accessible in my controller and if i return the posted value a blank array is coming in success method(Jquery).

I tried with Json.stringify to post it still i am not getting it.

HTML CODE

<form method="post" action="">
<div class="form-group">
<input name="f_full_name" type="text" placeholder="Name" required>
</div>
<div class="form-inline">
<div class="form-group">
<input name="f_contact_number" type="text" placeholder="Number+" value="" required>
</div>
<div class="form-group">
<input name="f_email_address" type="email" placeholder="Email" required>
</div>
</div>
<div class="form-group">
<textarea name="f_description" placeholder="Description"></textarea>
</div>
<input type="hidden" name="form2">
<button id="footer_enquiry" name="footer_enquiry" value="Submit">Submit</button>
</form>

JQUERY

$("#footer_enquiry").click(function(e){
e.preventDefault();
var ajxUrl      = "https://www.example.com/contactus/enquiry";
var ajxDataObj  = {
'name'              : $('input[name=f_full_name]').val(),
'contact'           : $('input[name=f_contact_number]').val(),
'email'         : $('input[name=f_email_address]').val(),
'description'       : $('[name=f_description]').val()
};
var ajxData     = JSON.stringify(ajxDataObj);
console.log(ajxData);
$.ajax({
type:         "POST",
url:          ajxUrl,
data:         ajxData,
contentType:  "json",
success: function (result) {
//do somthing here
alert(result);
console.log(result);
return false;
},
error: function (jqXHR, exception) {
var msg = '';
if (jqXHR.status === 0) {
msg = 'Not connect.\n Verify Network.';
} else if (jqXHR.status == 404) {
msg = 'Requested page not found. [404]';
} else if (jqXHR.status == 500) {
msg = 'Internal Server Error [500].';
} else if (exception === 'parsererror') {
msg = 'Requested JSON parse failed.';
} else if (exception === 'timeout') {
msg = 'Time out error.';
} else if (exception === 'abort') {
msg = 'Ajax request aborted.';
} else {
msg = 'Uncaught Error.\n' + jqXHR.responseText;
}   
alert(msg);
}                                                                                   
}); 

PHP CODE IN CONTROLLER function enquiry() {

    $name               = $this->input->post('name');
    $contact            = $this->input->post('contact');
    $email              = $this->input->post('email');
    $description        = $this->input->post('description');

    $data=array(
        'name'          => $name,
        'contact'       => $contact,
        'email'         => $email,
        'description'   => $description
    );

    print_r($data);

}

CONSOLE OUT PUT WHAT I AM GETTING

In
{"name":"test","contact":"01234567890","email":"gireesh.t@floretmedia.org","description":"testestestesetsetsetset"}
Array
(
[name] => 
[contact] => 
[email] => 
[description] => 
)

I expect posted data to be alerted but blank array is coming

dee flor
  • 67
  • 8
  • Did you try not contentType, but "dataType:'json'"? Try to use "cache:false", maybe the posted data is a wrong one. – DiabloSteve Jan 30 '19 at 14:23

1 Answers1

1

Take a look at this QA Handling JSON data in Codeigniter Controller, your backend is probably receiving the data but the $this->input->post is looking for form data being POSTed, not JSON.

Rob Nice
  • 198
  • 5