0

我对使用 ajax-jquery 将 php 数组发送到另一个 php 文件有疑问,如果我们使用某些输入类型隐藏值通过 jquery post 发送 php 数组,它将数组转换为字符串。所以另一个php文件没有作为数组值接收它作为字符串接收。我知道 php explode implode 功能。但是在codeiginter中,给定的数组不同,请参阅我需要发送的以下数组

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

我使用输入类型隐藏示例代码发送了这个数组

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

我的 jquery-ajax 代码

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

在这个数组中。我如何使用explode implode函数将字符串转换为数组。所以请给我指导来解决这个问题。提前感谢

4

1 回答 1

0

您可以尝试以下代码结构:

在你的脚本中:

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

在您的控制器中:

    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
            }

    }
于 2017-09-04T13:55:39.637 回答