0

i have a doubt about sending php array to another php file using ajax-jquery, if we send the php array via jquery post using some input type hidden value , it's convert array as string. so the another php file not receiving as array value it's receiving as a string. i know php explode implode function for that. but in codeiginter the given array is differ see the following array i need to send

Array ( [0] => stdClass Object ( [pro_id] => 1731 [pro_code] => 2564 [pro_name] => Lakudi [pro_des] =>  IS USED IN ALL FOOD SWEETNER INSTEAD OF SUGAR [pro_det_des] => ALSO AVAILABLE VARIES COLORS AND SHAPES CUBES BIG AND SMALL BALLS AND YELLOW, BROWN AND BLACK COLORS [pro_pack_des] => BULK AS WELL AS CONSUMER PACKING CAN BE OFFERED 30 KGS BULK ALSO AVAILABLE [pack_length] => 65 [pack_breadth] => 85 [pack_height] => 110 [cbm] => 0.608 [case_netweight] => 30 [case_grossweight] => 31 [price_INR] => 1100.00 [price_USD] => 0.00 [slab1] => [slab2] => [slab3] => [slab4] => [s1price] => [s2price] => [s3price] => [s4price] => [prolabel] => [exp_on] => 2013-12-31 [und_grp] => 16 [image_name] => 19442_94.jpg [image_name1] => 0 [image_name2] => 0 [image_name3] => 0 [crt_date] => 2013-06-11 [up_date] => 2013-06-11 [status] => active [admin_status] => deactive [list] => 0 [supplier_id] => 100 [bin] => 0 ) )  

i was send this array using input type hidden sample code

<input type="hidden" class="datapro" value="<?php print_r($data)?>">

my jquery-ajax code

$('.clickfu').click(function(){
        var view = $(this).attr('id');      
        var datapro = $('.datapro').val();
        $.post('<?php echo base_url()?>productajax',{typ:'proview',view:view,datapro:datapro},function(data){                                               
             $(".prolist").replaceWith(data);
            // alert(data);
        });
        return false;
    })

in this array. how can i use explode implode function to convert the string as array. so please kindly give me guidance for solve this issue. thanks advance

5
  • Using <?php print_r($data)?> to set value of input is NOT a good idea, never, ever! Commented Jun 27, 2013 at 13:50
  • 1
    I think you might want to use json_encode, which takes a php object/array and turns it into a json object string. I'm not entirely sure what you are trying to though. Commented Jun 27, 2013 at 13:52
  • i want to just post as a array and get as a array. how can i do this in jason_encode. give me some example Commented Jun 27, 2013 at 13:56
  • 1
    @tamil_arvu take a look at this and this and also the PHP Doc Commented Jun 27, 2013 at 14:00
  • thanks hashem quolami Commented Jun 27, 2013 at 14:04

1 Answer 1

0

You can try below code structure :

in your script :

$('.clickfu').click(function(){

        var value1 = $('#$value1').val();

        var value2 = $('#$value2').val();

        var value3 = $('#$value3').val();

        var value4 = $('#$value4').val();

            $.ajax({
                        type:'post',
                        url:'<?php echo base_url();?>/controller_name/method_name',
                        data:{
                                value1 : value1,
                                value2 : value2,
                                value3 : value3,
                                value4 : value4,

                             },
                        success : function(res){
                            if( res == 1 )
                            {
                                alert(' Success..!!');
                            }
                            else
                            {
                                alert('Oops..!! Something went wrong.Please try again.');
                            }
                        }
                    });
        });

in your Controller :

    public function method_name()
    {
          if(isset($_POST))
            {

                $insert_array = array(

                                   'value1'       => $_POST['value1'],
                                   'value2'       => $_POST['value2'],
                                   'value3'       => $_POST['value3'],
                                   'value4'       => $_POST['value4'],

                                );
                $res = $this->model_name_db->insertValues($insert_array);
                if( $res > 0 )
                {
                   echo"1";exit;
                }  
                else
                {
                   echo"0";exit;
                }  
            }
            else
            {
                // your action
            }

    }
Sign up to request clarification or add additional context in comments.

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.