从另一个Ajax函数获取JSON编码数据

时间:2019-11-26 16:45:51

标签: javascript php ajax smarty prestashop-1.6

我使用的是prestashop 1.6,我有一个ajax函数,在其中编码一些数据,这些数据是我在控制台上使用echo成功检索到的。现在,我必须在另一个ajax函数中对该json数据进行解码,以获取特定变量的值。抱歉,我是prestashop 1.6的新手。

我的第一个ajax函数:

public function ajaxProcessAddQrVideo(){

    $target_dir = _PS_IMG_DIR_.DS.'video_qr';

    $id_product = Tools::getValue('id_product');

    $stamp = strtotime('now');
    $filename = 'video_qr_'.$id_product.'_'.$stamp.'.jpg';
    $target_file = $target_dir.DS.$filename;

    $upload = $_FILES['video_qr_attachment_file'];

    $err = array();
    $uploaded = false;

    if($upload['type'] !='image/jpeg'){
        array_push($err,"Veuillez entrer un fichier JPG");
    }

    if(empty($err)){
        $uploaded = @move_uploaded_file($upload['tmp_name'], $target_file);
        $this->context->smarty->assign('uploaded', $uploaded);
        $this->context->smarty->assign('filename', $filename);




    }
    $this->json =array(
        'uploaded'=>$uploaded,
        'err'=>$err,
        'id_product'=>$id_product,
        'stamp'=>$stamp,
        'file_name'=>$filename,
    );

     echo json_encode($this->json);


    exit;


}

我想在我的第二个ajax函数中获取'file_name'的值:

 public function ajaxProcessAddVideo(){

    $img_path = json_decode($this->json,true);
    $filename = $img_path['file_name'];
    $id_lang = Context::getContext()->language->id;

    $script = Tools::getValue('script');
    $id_product = Tools::getValue('id_product');
    $id_group_vid = Tools::getValue('cust_group');
    //add qr_code video
    $qr_code =$filename;

    $err = true;
    $insert = false;
    $videos = array();

    $vid = new MpsVideo();
    $vid->id_product = $id_product;
    $vid->url  = $script;
    $vid->cust_group = $id_group_vid;
    $vid->date_add = date('Y-m-d H:i:s');
    $vid->active = 1;
    $vid->qrcode = $qr_code;

    $is_existing = Db::getInstance()->getValue("SELECT COUNT(id_mps_video) FROM `ps_mps_video` WHERE id_product=$id_product AND url = '$script'");


    if($is_existing==0){
        $insert = $vid->save();

        $id_group = explode(',',$id_group_vid);
        $group_name = array();

        foreach($id_group as $k){
            $group = new Group($k);
            $group_name[] = $group->name[$id_lang]; 
        }

        $vid->group_names = implode('<br/>',$group_name);
    }

    echo json_encode(array(
        'err'=>$err,
        'video_exists'=>$is_existing,
        'insert'=>$insert,
        'vid'=>$vid,
    ));

    exit;

}

我不知道如何实现这一目标,但我知道它的可能。如果有人可以帮助您理解这一点,我将不胜感激。

1 个答案:

答案 0 :(得分:0)

第一个函数的结果是一个json,该结果必须从另一个文件中读取,然后传递给第二个函数。否则,相同的函数应读取该json:

$data = file_get_contents("file/file_json_result.php");
$img_path = json_decode($data, true);