我需要通过ajax将checked字段的ID和“ checked”字符串传递给数据库。我对ajax一点都不熟悉
<h2 id="h2">To Do List</h2>
<ul id="new">
<?php if(!empty($details)){ foreach($details as $key => $del) { ?>
<li id="try-0"><input class= "box" type="checkbox"><input class="set field" name="list[]" type="text" value="<?php echo $del['value'];?>"></li>
<?php } ?>
<li id="try-0"><input class= "box" type="checkbox"><input class="set field" name="list[]" style="margin: 5px 0 5px 10px;width: 297px;height: 35px;" type="text" value=""></li>
<?php } else{ ?>
<li id="try-0"><input class= "box" type="checkbox"><input class="set field" name="list[]" type="text" value=""></li>
<?php } ?>
</ul>
<button class="btn btn-info" id="btn1">add</button></div>
<div class="col-md-2">
<button class="btn btn-info" id="btn2" type="submit" value="submit" formaction="<?php echo base_url();?>homeController/upload">Save</button></div>
</div>
</div>
JavaScript
<script type="text/javascript">
$(document).ready(function() {
var x =1;
$('#btn1').hide();
$('body').on('keyup', '.field', function(){
var search_text = $('.field').last().val();
if(search_text==""){
$('#btn1').hide();
}else{
$('#btn1').show();
}
});
$("#btn1").click(function (event) {
event.preventDefault();
$('#new').append('<li" id="try-'+x+'"><input class= "box" type="checkbox"><input name="list[]" class="set field" type="text" value=""></li>');
$('#btn1').hide();
x++;
});
$('body').on('click', '.box', function(){
var sta = 'checked';
// i also need to pass the id of the clicked checkbox
$.ajax({
type: "POST",
url: "<?php echo base_url();?>homeController/statuspass",
data: {search_name:sta},
success: function(data){
// console.log(success);
}
});
});
});
</script>
当我单击复选框ID和一个字符串必须传递到控制器时,我需要将字符串保存在数据库中。
当我尝试时,出现此错误:
POST http://localhost/todolist/homeController/statuspass 500 (Internal Server Error)
send @ jquery.min.js:2
ajax @ jquery.min.js:2
(anonymous) @ (index):146
dispatch @ jquery.min.js:2
y.handle @ jquery.min.js:2
答案 0 :(得分:0)
服务器端出现错误。 尝试通过xDebug调试php或在控制器操作中查看die()消息,以查看发生了什么事。
答案 1 :(得分:0)
您的Js代码通常应如下所示:
$(document).on('click', '#myFormSubmit', function(event) {
event.preventDefault();
var id = $('#hiddenInputThatHasMyID').val();
var blah = 'blah';
var blahblah = 'blahblah',
// I expect you to already know how to get the above vars or you can search an learn how to retrieve them, its not the purpose of your question here.
// You can serialize your form like this
// var data = $('form#myForm').serialize();
// Or create it manually like this
var data = {id:id, blah:blah, blahblah:blahblah};
$.ajax({
url: "<?= site_url('controller/method') ?>",
method: 'POST',
data: data,
dataType:'JSON',
success: function(response) {
console.log(response);
},
error: function(response) {
console.log(response);
}
});
});
这会将您的数据发送到该controller/method
并从中获取响应,因此可以在控制器的方法中等待:
public function method()
{
$id = $this->input->post('id');
$blah = $this->input->post('blah');
$blahblah = $this->input->post('blahblah');
// Do your magic here and return vars
$data['ret_var1'] = 'val1';
$data['ret_var2'] = 'val2';
// echo your data
header('Content-Type: application/json');
echo json_encode($data);
}
现在,在ajax响应中,您可以像这样使用var:
var var1 = response.var1; // val1
var var2 = response.var2; // val2
我希望这很清楚。
注释:
始终使用url: "<?= site_url('controller/method') ?>"
这样的网址
答案 2 :(得分:0)
代码点火器URL较好,以后得分较低。
将您的控制器名称homeController更改为home_controller,然后检查控制器名称,以大写字母开头,然后注意剂量有任何大写字母,以后再接受您的第一个,并在扩展# Update SERVICE_ACCOUNT_JSON_FILE_PATH with the file path to the private key
# file downloaded from the developer console.
SERVICE_ACCOUNT_JSON_FILE_PATH = 'gsuite1-7b1dc64d67fd.json'
#SERVICE_ACCOUNT_JSON_FILE_PATH = 'file.txt'
# Update USER_EMAIL with the email address of the user within your domain that
# you would like to act on behalf of.
USER_EMAIL = 'jschull@e-nable.org'
# plus.me and plus.stream.write are the scopes required to perform the tasks in
# this quickstart. For a full list of available scopes and their uses, please
# see the documentation.
SCOPES = ['https://www.googleapis.com/auth/plus.me',
'https://www.googleapis.com/auth/plus.stream.write']
def authenticate():
"""Build and return a Plus service object authorized with the service accounts
that act on behalf of the given user.
Returns:
Plus service object.
"""
print( 'Authenticate the domain for %s' % USER_EMAIL)
# Make service account credentials
credentials = ServiceAccountCredentials.from_json_keyfile_name(SERVICE_ACCOUNT_JSON_FILE_PATH, scopes=SCOPES)
# Setting the sub field with USER_EMAIL allows you to make API calls using the
# special keyword 'me' in place of a user id for that user.
delegate_credentials = credentials.create_delegated(USER_EMAIL)
http = httplib2.Http()
http = delegate_credentials.authorize(http)
# Create and return the Plus service object
return build('plusDomains', 'v1', http=http)
的位置接受第二个,并再次检查您是否拥有CI_Controller
方法是否在控制器类homeController中。