简体   繁体   English

将JavaScript变量存储在PHP变量中,以将其用于源页面中的动态元描述

[英]Storing the JavaScript variable in a PHP variable to use it for dynamic meta description in source page

I have made an AJAx request in CI. 我已经在CI中提出了AJAx请求。 This request is fetching data from database and displaying this on the same page. 该请求正在从数据库中获取数据,并将其显示在同一页面上。 I need to create dynamic meta description. 我需要创建动态元描述。 Where i need to use one field value from database in my meta description so once I view page source it must appear in the view page source. 我需要在元描述中使用数据库中的一个字段值的地方,因此一旦查看页面源,它​​就必须出现在视图页面源中。

I wanted to store the javascript variable in the php variable so once I open the source page I can easily display that in my description. 我想将javascript变量存储在php变量中,因此一旦打开源页面,就可以轻松地在我的描述中显示它。 Is there any way to use javascript variable for meta description in view page source or using php. 有什么方法可以在视图页面源代码中或使用php使用javascript变量进行元描述。

View: 视图:

$('#city').on('change',function(){
                 var branchName=  $('#city option:selected').html();
                 $('#branchSel').html('<span><b>Branch Name: </b></span>'+branchName).addClass("appear");
                 var stateName=  $('#state option:selected').html().toLowerCase();
                 var countryName=  $('#country option:selected').html().toLowerCase();
                 countryName = countryName.replace(/\s+/g, '-');
                 stateName = stateName.replace(/\s+/g, '-');
                 branchName = branchName.replace(/\s+/g, '-').toLowerCase();                                       
                    var cityID = $(this).val();
                        if(cityID){
                        window.history.pushState('', '', '<?php echo base_url($bank_rename.'/ifsc-code'); ?>/'+countryName+'/'+stateName+'/'+branchName);
                        $.ajax({
                            type:'POST',
                            url:'<?php echo base_url('bank/getBranchDetail'); ?>',
                            data:'city_id='+cityID,
                             success:function(data){
                                $('#dataDisplay').remove();
                                $('#ifsc').html("").removeClass("appear");
                                $('#micr').html("").removeClass("appear");
                                $('#contact').html("").removeClass("appear");
                                $('#address').html("").removeClass("appear");
                                var dataObj = jQuery.parseJSON(data);
                                    $(dataObj).each(function(){
                                        var ifsc = $('#ifsc').html("<span><b>IFSC Code: </b></span>");
                                        var micr = $('#micr').html("<span><b>MICR Code: </b></span>");
                                        var contact = $('#contact').html("<span><b>Contact No: </b></span>");
                                        var address = $('#address').html("<span><b>Address: </b></span>");
                                        ifsc.append(this.ifsc_code).addClass("appear");
                                        var ab= this.ifsc_code;
                                        micr.append(this.micr_code).addClass("appear");
                                        contact.append(this.contact_no).addClass("appear");
                                        address.append(this.address).addClass("appear");
                                        $('#branch_code').html('<span><b>Branch Code: </b></span>Last six characters of IFSC Code represent Branch code.');

                                    });
                            }
                        }); 
                        }else {
                           window.history.pushState('', '', '<?php echo base_url($bank_rename.'/ifsc-code'); ?>/'+countryName+'/'+stateName);
                           $('#dataDisplay').html('Select State, District, Branch Name to Display Details Here'); 
                           $('#branch_code').html("").removeClass("appear");
                           $('#ifsc').html("").removeClass("appear");
                           $('#micr').html("").removeClass("appear");
                           $('#contact').html("").removeClass("appear");
                           $('#address').html("").removeClass("appear");
                        }
                });
        });

HTML Code: HTML代码:

<td><p id="ifsc"></p></td>

Controller: 控制器:

public function getBranchDetail(){
    $branch = array();
    $city_id = $this->input->post('city_id');
    if($city_id){
        $con['conditions'] = array('id'=>$city_id);
        $branchData = $this->Bank_model->getBranchData($con);
    }
    echo json_encode($branchData);
}

Model: 模型:

function getBranchData($params = array()){
    $this->db->select('c.micr_code, c.ifsc_code, c.contact_no, c.address');
    $this->db->from($this->branchTbl.' as c');

    //fetch data by conditions
    if(array_key_exists("conditions",$params)){
        foreach ($params['conditions'] as $key => $value) {
            if(strpos($key,'.') !== false){
                $this->db->where($key,$value);
            }else{
                $this->db->where('c.'.$key,$value);
            }
        }
    }

    $query = $this->db->get();
    $result = ($query->num_rows() > 0)?$query->result_array():FALSE;

    //return fetched data
    return $result;
}

If somebody can tell me how to access only a single field of database ifsc_code in the view page , then it can also be helpful. 如果有人可以告诉我如何仅在视图页面中访问数据库ifsc_code的单个字段,那么它也可能会有所帮助。 I am stuck here... 我被困在这里...

If I understood correctly you would like to dynamically update metadata when you do above request. 如果我正确理解,则在执行上述请求时,您希望动态更新元数据。 You have an ajax call in your script which is only returning one object, you can instead send metadata info in the request as follow. 您的脚本中有一个ajax调用,该调用仅返回一个对象,您可以按如下所示在请求中发送元数据信息。

in your controller file 在您的控制器文件中

public function getBranchDetail(){
    $branchData = array();
    $branchData['meta_title'] = 'Your meta title here';
    $branchData['meta_description'] = 'Your meta description here';
    $branchData['members'] = array();// good practice to setup default
    $city_id = $this->input->post('city_id');
    if($city_id){
        $con['conditions'] = array('id'=>$city_id);
        $branchData['members'] = $this->Bank_model->getBranchData($con);
    }
    echo json_encode($branchData);
}            

and in your ajax call 在你的ajax电话中

$.ajax({
    type:'POST',
    url:'<?php echo base_url('bank/getBranchDetail'); ?>',
    data:'city_id='+cityID,
    success:function(data){
        var dataObj = jQuery.parseJSON(data);
        document.title = dataObj.meta_title; // set document title
        $('meta[name=description]').attr('content',dataObj.meta_description;); // set meta description
        $(dataObj.members).each(function(){
        //... your other code
        });
    }
});

I have created an associative array which values are being assigned in your ajax call. 我创建了一个关联数组,在您的ajax调用中分配了值。

Edit: 编辑:

your html should contain these values 您的html应该包含这些值

<html>
    <head>
        <title>Your Title</title>
        <meta name="description" content="meta desription">
        .....
    </head>
    <body>
        .....
    </body>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM