Fahim Foysal Kamal
Fahim Foysal Kamal

Reputation: 339

How can i pass a variable with url in javascript code?

I want to pass a variable with URL in JavaScript code. Variable comes from database. I see many answer in StackOverflow. But I think, I don't find any right answer for my problem. Code are given bellow:

This code is in my index page.

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

This code is in code.js file.

// 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  */
});

All URL pass a variable

lang=$lang

. $lang is en or bn but it comes from database table. How can i make it usable?

Upvotes: 0

Views: 187

Answers (2)

Veer
Veer

Reputation: 6936

If you are want to pass data to url and you are not using inline JS then you can declare global variable and then use it in *.js file.

Ex: This code is in my index page.

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

// HEre your normal js code
</script>

This code is in code.js file.

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

Upvotes: 0

Ankur
Ankur

Reputation: 102

you can set javascript array on your index page like that.

Note : Put this script before include your code.js file

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

Now in your code.js file

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

Upvotes: 1

Related Questions