0

Codeigniter メソッドで使用する必要がある値を正しく警告する次の JavaScript ループがあります。js ループは次のとおりです。

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

このPHPコードをjavascriptループ内で実行できる方法はありますか? PHP がサーバー側で JS がクライアント側であることは知っていますが、私がまだ気づいていない問題の解決策があるに違いないと確信しています。ありがとう。

4

4 に答える 4

3

これに対する解決策は、サーバーへの ajax 呼び出しを行うことです。コントローラに codeigniter メソッドを呼び出すメソッドを設定できます。これにより、php 呼び出しとクライアント側の呼び出しが分割されます。

データベースに何かを挿入する場合は、ajax post メソッドを使用する必要があります。

http://api.jquery.com/jQuery.post/

function myInsert() { 
  $('input[name=r_maybe].r_box').each(function(){ 
    if( $(this).prop('checked') ){ 
      var value = $(this).prop('value');
      $.post("controllername/functionname", { value: value }, function(data) { 
        alert(data); // Returned message from the server
      }); 
     } 
  }); 
}
于 2013-01-27T17:00:17.877 に答える
1

jquery$.postから使用できます

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"); //**             
       } 
    });
}

コントローラーでは、次のものを使用できます。

function functionName()
{
//getting your posted sec token.
   $post1 = $this->input->post('post1'); 
   $post2 = $this->input->post('post2'); 
   $post3 = $this->input->post('post3'); 
   $data['res'] = "something";// return anything you like.
// you should use json_encode here because your post's return specified as json. see **
   echo json_encode($data); //$data is checked in the callback function in jquery.
}
于 2013-01-27T17:16:14.087 に答える
1

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(){
    }

    });       
}
    }
于 2013-01-27T17:00:33.833 に答える
0

これはデータをデータベースに直接ダンプするため、そのコントローラー機能に誰がアクセスできるか、および渡されるデータに対して行われるスクラブ/検証の量に関して、これも何らかの方法で保護されていることを確認してください。

于 2013-01-28T00:34:40.490 に答える