0

I have the following javascript loop which correctly alerts the value I need to use in a Codeigniter method. Here is the js loop:

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

Instead of alerting the required value, I need to somehow execute this Codeigniter method:

//this would never work because it mixes JS with PHP, but I need a workaround
$this->appeal_model->myMethod($(this).prop('value'), 888, 999);

Is there someway that I can run this PHP code inside the javascript loop? I know about PHP being server-side and JS being client-side, but I'm sure there must be a solution to my problem that I'm just not aware of yet. Thanks.

4 Answers 4

3

The solution to this is to make an ajax call to the server, you can have a method on your controller which calls your codeigniter method. This divides your php call and your client side call.

If you are inserting something into the database, you should use the ajax post method.

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
      }); 
     } 
  }); 
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the tip. Yes, I am trying to insert something into the database, so does that mean I should not use your $.post but should instead be using $.ajax ?
$post is a shorthanded call for using post on $ajax , you can use either, but specify type post if you use $ajax
1

Use ajax to store data to the server side: The code should be something like this:

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

    });       
}
    }

2 Comments

This looks great. I'm trying to implement it now. Just wondering about the correct code to use for the URL. You've said to refer to "your file name", but I'm not sure what that means.
It's the url for the method you want to call, so the name of the CodeIgniter controller followed by the name of the method in that controller that you want to call.
1

you can use $.post from jquery

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

In your controller you can have:

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.
}

Comments

0

Since this will be dumping data directly into your db, make sure this is secured in some manner as well, in terms of who has access to that controller function and the amount of scrubbing/verification done on the data being passed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.