2016-10-20 33 views
0

我想在JavaScript代碼中傳遞一個帶有URL的變量。變量來自數據庫。我在StackOverflow中看到很多答案。但我認爲,對於我的問題,我找不到任何正確的答案。代碼如下:如何在JavaScript代碼中將URL傳遞給變量?

此代碼位於我的索引頁面。

<script type="text/javascript"> 

      $(document).ready(function(){ 

      $("#btn-view").hide(); 

      $("#btn-add").click(function(){ 
       $(".content-loader").fadeOut('slow', function() 
       { 
        $(".content-loader").fadeIn('slow'); 
        **$(".content-loader").load('video_add.php?lang=$lang')**; 
        $("#btn-add").hide(); 
        $("#btn-view").show(); 
       }); 
      }); 

      $("#btn-view").click(function(){ 

       $("body").fadeOut('slow', function() 
       { 
        **$("body").load('video.php?lang=$lang');** 
        $("body").fadeIn('slow'); 
        **window.location.href="video.php?lang=$lang";** 
       }); 
      }); 

     }); 
    </script> 

此代碼是code.js文件。

// JavaScript Document 

$(document).ready(function(){ 

/* Data Insert Starts Here */ 
$(document).on('submit', '#emp-SaveForm', function() { 

    $.post("video_create.php?lang=$lang", $(this).serialize()) 
    .done(function(data){ 
     $("#dis").fadeOut(); 
     $("#dis").fadeIn('slow', function(){ 
      $("#dis").html('<div class="alert alert-info">'+data+'</div>'); 
      $("#emp-SaveForm")[0].reset(); 
     });  
    }); 
    return false; 
}); 
/* Data Insert Ends Here */ 


/* Data Delete Starts Here */ 
$(".delete-link").click(function() 
{ 
    var id = $(this).attr("id"); 
    var del_id = id; 
    var parent = $(this).parent("td").parent("tr"); 
    if(confirm('Sure to Delete ID no = ' +del_id)) 
    { 
     $.post('video_delete.php?lang=$lang', {'del_id':del_id}, function(data) 
     { 
      parent.fadeOut('slow'); 
     }); 
    } 
    return false; 
}); 
/* Data Delete Ends Here */ 

/* Get Edit ID */ 
$(".edit-link").click(function() 
{ 
    var id = $(this).attr("id"); 
    var edit_id = id; 
    if(confirm('Sure to Edit ID no = ' +edit_id)) 
    { 
     $(".content-loader").fadeOut('slow', function() 
     { 
      $(".content-loader").fadeIn('slow'); 
      $(".content-loader").load('video_edit.php?edit_id='+edit_id); 
      $("#btn-add").hide(); 
      $("#btn-view").show(); 
     }); 
    } 
    return false; 
}); 
/* Get Edit ID */ 

/* Update Record */ 
$(document).on('submit', '#emp-UpdateForm', function() { 

    $.post("video_update.php?lang=$lang", $(this).serialize()) 
    .done(function(data){ 
     $("#dis").fadeOut(); 
     $("#dis").fadeIn('slow', function(){ 
      $("#dis").html('<div class="alert alert-info">'+data+'</div>'); 
      $("#emp-UpdateForm")[0].reset(); 
      $("body").fadeOut('slow', function() 
      { 
       $("body").fadeOut('slow'); 
       window.location.href="video.php?lang=$lang"; 
      });     
     });  
    }); 
    return false; 
}); 
/* Update Record */ 
}); 

所有URL傳遞變量

LANG = $ LANG

。 $ lang是en或bn,但它來自數據庫表。我如何使它可用?

+0

在javascript代碼中使用php什麼是 – Kumar

+1

在js中回顯php需要使用<?php echo $ lang; ?> – Blinkydamo

回答

1

你可以在你的索引頁面上設置javascript數組。

注:將這個腳本之前,包括你的code.js文件

<script> 
var global_var = { 
lang: "your value", 
}; 
</script> 

現在,在您code.js文件

'video_delete.php?lang='+global_var.lang 
+0

這不起作用 –

0

如果你想傳遞數據到網址,你不使用內聯JS,那麼你可以聲明全局變量,然後在* .js文件中使用它。

例如: 此代碼位於我的索引頁中。

<script type="text/javascript"> 
// Decl Global variable 
var $lang = "<?php print $lang; ?>"; 

// HEre your normal js code 
</script> 

此代碼位於code.js文件中。

// js code 
$.post("video_update.php?lang="+$lang, $(this).serialize()) 
// Js code 
+0

這不起作用 –

相關問題