我在CI中提出了一个AJAx请求。此请求正在从数据库中获取数据并将其显示在同一页面上。我需要创建动态元描述。我需要在我的元描述中使用数据库中的一个字段值,因此一旦我查看页面源代码,它必须出现在查看页面源代码中。
我想将javascript变量存储在php变量中,这样一旦我打开源页面,我可以轻松地在我的描述中显示它。有没有办法在视图页面源代码中使用 javascript 变量进行元描述或使用 php。
视图:
$('#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");
}
});
});
网页代码:
<td><p id="ifsc"></p></td>
控制器:
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);
}
型:
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;
}
如果有人可以告诉我如何在视图页面中仅访问数据库ifsc_code的单个字段,那么它也会有所帮助。我被困在这里...
如果我理解正确,您希望在执行上述请求时动态更新元数据。 脚本中有一个只返回一个对象的 ajax 调用,您可以在请求中发送元数据信息,如下所示。
在控制器文件中
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);
}
并在您的 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
});
}
});
我创建了一个关联数组,在您的 ajax 调用中分配了这些值。
编辑:
您的 HTML 应包含这些值
<html>
<head>
<title>Your Title</title>
<meta name="description" content="meta desription">
.....
</head>
<body>
.....
</body>
</html>