簡體   English   中英

HTML頁面中的Jquery Ajax到Codeigniter發布數據處理

[英]Jquery Ajax in HTML page to Codeigniter post data handling

我的代碼點火器安裝程序外有一個HTML頁面。 假設abc.com是我基於CI的CMS網站,我將abc.com/trade作為潛在客戶捕獲頁面。 此HTML頁面(index.html)在交易文件夾下。 我不能將其放在CI中,所以這是一個獨立頁面,而我的Apache正在顯示此index.html。 到現在為止還挺好。 現在,我在此html頁面中有一個Contact us表單,我正在使用Jquery ajax將其發布到控制器。 無法在控制器中訪問數據,並且如果我返回發布的值,則成功方法(Jquery)中將出現一個空白數組。

我嘗試與Json.stringify發布它仍然我不明白。

HTML代碼

<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函數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);

}

解決我得到的東西

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

我希望發布的數據會收到警報,但空白數組即將到來

看一下Codeigniter Controller中的此QA 處理JSON數據 ,您的后端可能正在接收數據,但是$this->input->post正在尋找要發布的表單數據,而不是JSON。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM