Php javascript循环中的Codeigniter方法
我有下面的javascript循环,它正确地提醒我需要在Codeigniter方法中使用的值。以下是js循环:Php javascript循环中的Codeigniter方法,php,javascript,codeigniter,Php,Javascript,Codeigniter,我有下面的javascript循环,它正确地提醒我需要在Codeigniter方法中使用的值。以下是js循环: function myInsert(){ $('input[name=r_maybe].r_box').each(function(){ if( $(this).prop('checked') ){ // need to replace this alert with codeigniter method below ale
function myInsert(){
$('input[name=r_maybe].r_box').each(function(){
if( $(this).prop('checked') ){
// need to replace this alert with codeigniter method below
alert ($(this).prop('value'));
}
});
}
我需要以某种方式执行此Codeigniter方法,而不是警告所需的值:
//this would never work because it mixes JS with PHP, but I need a workaround
$this->appeal_model->myMethod($(this).prop('value'), 888, 999);
有没有办法在javascript循环中运行此PHP代码?我知道PHP是服务器端的,JS是客户端的,但我确信我的问题一定有一个解决方案,我只是还不知道。谢谢。解决方案是对服务器进行ajax调用,您可以在控制器上有一个调用codeigniter方法的方法。这将划分php调用和客户端调用 如果要向数据库中插入内容,则应使用ajax post方法
解决方案是对服务器进行ajax调用,您可以在控制器上有一个方法来调用codeigniter方法。这将划分php调用和客户端调用 如果要向数据库中插入内容,则应使用ajax post方法
使用ajax将数据存储到服务器端: 代码应该是这样的:
function myInsert(){
$dataArray=[];
$('input[name=r_maybe].r_box').each(function(){
if( $(this).prop('checked') ){
// need to replace this alert with codeigniter method below
dataArray.push($(this).prop('value'))
}
});
if(dataArray.length>0)
{
$.ajax({
url:"your file name",//this file should contain your server side scripting
type:"POST",
data:{dataName : dataArray}
success:function(){
}
});
}
}
使用ajax将数据存储到服务器端: 代码应该是这样的:
function myInsert(){
$dataArray=[];
$('input[name=r_maybe].r_box').each(function(){
if( $(this).prop('checked') ){
// need to replace this alert with codeigniter method below
dataArray.push($(this).prop('value'))
}
});
if(dataArray.length>0)
{
$.ajax({
url:"your file name",//this file should contain your server side scripting
type:"POST",
data:{dataName : dataArray}
success:function(){
}
});
}
}
您可以使用fromjquery
function myInsert(){
$('input[name=r_maybe].r_box').each(function(){
if( $(this).prop('checked') ){
$.post('<?php echo site_url("controllerName/functionName")?>',
{"post1": $(this).prop('value'), "post2":888, "post3": 999 },
function(data.res == "something"){
//here you can process your returned data.
}, "json"); //**
}
});
}
您可以使用fromjquery
function myInsert(){
$('input[name=r_maybe].r_box').each(function(){
if( $(this).prop('checked') ){
$.post('<?php echo site_url("controllerName/functionName")?>',
{"post1": $(this).prop('value'), "post2":888, "post3": 999 },
function(data.res == "something"){
//here you can process your returned data.
}, "json"); //**
}
});
}
由于这将直接将数据转储到您的数据库中,请确保这也以某种方式得到保护,包括谁有权访问该控制器功能以及对传递的数据执行的清理/验证量。由于这将直接将数据转储到您的数据库中,请确保这也以某种方式得到保护,就谁有权访问该控制器功能以及对传递的数据执行的清理/验证量而言。这看起来很棒。我现在正在努力实现它。只是想知道用于URL的正确代码。您说过要引用“您的文件名”,但我不确定这是什么意思。它是您要调用的方法的url,因此CodeIgniter控制器的名称后跟您要调用的控制器中的方法的名称。这看起来很棒。我现在正在努力实现它。只是想知道用于URL的正确代码。您说过要引用“您的文件名”,但我不确定这是什么意思。它是您要调用的方法的url,因此CodeIgniter控制器的名称后跟您要调用的控制器中的方法的名称。感谢您的提示。是的,我正在尝试向数据库中插入一些内容,那么这是否意味着我不应该使用您的$.post,而应该使用$.ajax?$post是在$ajax上使用post的一个简短调用,您可以使用其中一个,但是如果您使用$ajax,请指定类型post谢谢提示。是的,我正在尝试向数据库中插入一些内容,这是否意味着我不应该使用$.post,而应该使用$.ajax?$post是对$ajax上使用post的简写调用,您可以使用其中一种,但如果使用$ajax,请指定类型post