0

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?

2
  • using php in javascript code, what are you Commented Oct 20, 2016 at 10:10
  • 1
    to echo the php in the js you need to use <?php echo $lang; ?> Commented Oct 20, 2016 at 10:14

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

Comments

0

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 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.