Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6030869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:06:04+00:00 2026-05-23T05:06:04+00:00

I am having great difficulty passing a variable from a PHP file to .js

  • 0

I am having great difficulty passing a variable from a PHP file to .js files

The code in the PHP file I used is this:

<script>

jQuery(document).ready(function(){
    var uid = <?php echo (intval($uid)); ?>;
    //var uid = <?php echo(intval($_SESSION['uid'])); ?>.val();

  });
</script>

The variable value should be passed into the .js file to refresh just a certain div on the page (not the whole page) after a form submission is performed.

This is the .js file and the corresponding code starts at “// refresh the monitor list div”:

    $(function() {  

   $(".button").click(function() {  
     // validate and process form here  

     $('.error').hide();  
       var domain = $("input#domain").val();  
        if (domain == "") {  
       $("label#domain_error").show();  
       $("input#domain").focus();  
       return false;  
     }  
        var com_domain = $("input#com_domain").val();  
         if (com_domain == "") {  
       $("label#com_domain_error").show();  
       $("input#com_domain").focus();  
       return false;  
     }  
        var cemail = $("input#cemail").val();
        var port = $("select#port").val();
        var active = $("input#active").val();
        var uid = $("input#uid").val();
        var main = $("select#main").val();

     var dataString = 'cemail='+ cemail + '&domain=' + domain + '&com_domain=' + com_domain + '&active=' + active + '&main=' + main + '&port=' + port;  
     //alert (dataString);return false;  
     $.ajax({  
       type: "POST",  
       url: "user_add.php",  
       data: dataString,  
       success: function() {  
         $('#monitor_form').append("<div id='message'></div>"); 
         $('#monitor_form form')[0].reset();
         $('#message').html("<img id='checkmark' src='images/tick.png' /><b> Monitor sucessfully added!</b>")  
         .hide()  
         .fadeIn(500, function() {  
           $('#message').append("");  
         });
         setTimeout("$('#message').hide().remove();", 6000);

         var dataString2 = 'ajax=1&uid=' + uid;
         $.ajax({
             type: "GET",
             url: "monpanel.php",
             data: dataString2,
             success: function(html_data){
                $('#list_monitors').html(html_data);
            }
         });
         //document.onkeydown = showDown;
       }  
     });  
     return false; 
   });  
 }); 

function showDown(evt) {
    event = (evt)? evt : ((event)? event : null);
    if (evt) {
        if (event.keyCode == 8 && (event.srcElement.type!= "text" && event.srcElement.type!= "textarea" && event.srcElement.type!= "password")) {
            // When backspace is pressed but not in form element
            cancelKey(evt);
        }
        else if (event.keyCode == 116) {
            // When F5 is pressed
            cancelKey(evt);
        }
        else if (event.keyCode == 122) {
            // When F11 is pressed
            cancelKey(evt);
        }
        else if (event.ctrlKey && (event.keyCode == 78 || event.keyCode == 82)) {
            // When ctrl is pressed with R or N
            cancelKey(evt);
        }
        else if (event.altKey && event.keyCode==37 ) {
            // stop Alt left cursor
            return false;
        }
    }
} 

function cancelKey(evt) {
    if (evt.preventDefault) {
        evt.preventDefault();
        return false;
    }
    else {
        evt.keyCode = 0;
        evt.returnValue = false;
    }
}

/*function mycallbackfunc(v,m,f) {
    if (v == 'Cancel') {
        $.prompt('The action was ' + v + 'ed');
    }
    else {
        $.prompt('Monitor ' + v + 'd successfully');
    }
}*/

// ask for validation on monitor delete, pause, resume request
$(document).ready(function(){
   $(".error").hide();  
   alert("Stage 0! -> uid="+uid.toString());
   $("#mondelpau").validate({
        debug: false,
        rules: {
            act: "required",
            uid: "required",
            sid: "required"
        },
        /*messages: {
            name: "Please let us know who you are.",
            email: "A valid email will help us get in touch with you.",
        },*/
        submitHandler: function(form) {
            // do other stuff for a valid form
            //$.post('delpaures.php', $("#mondelpau").serialize(), 
            alert("Stage 1! -> uid="+uid.toString());
            $.ajax({
              async: false,
              type: "POST",
              url: "delpaures.php",
              data: $("#mondelpau").serialize(),
              success: function(data) {
                $('#monadiv').html(data);  
                 //$('#results').html(data);
                 //alert (data);return false;

                 // refresh the monitor list div

                //$('#list_monitors').load(dataString8);
                //var uid = $("input#uid").val();
                //var dataString8 = 'ajax=1&uid=' + $("input#uid").val();
                var dataString8 = 'ajax=1&uid=' + uid; // .val()
                //var dataString8 = 'ajax=1&uid=19';
                alert("Stage 2! -> uid="+uid.toString());
                $.ajax({
                  async: false,
                  type: "GET",
                  dataType: "html",
                  url: "monpanel.php",
                  data: dataString8,
                  success: function(html_data){
                    alert("Stage 3!");
                    $("#list_monitors").css("background-color","#FF0000");
                    $("#list_monitors").html(html_data);
                  }
                });
              }   
            });
        }
    });
});

Needless to say I have tried everything, even renaming .js file to .php and redirecting to them with .htaccess, but that doesn’t work either.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T05:06:05+00:00Added an answer on May 23, 2026 at 5:06 am

    The reason you cannot access the variable in your js file is that the variable ‘uid’ is defined in a different scope than the js file. Its the same thing as:

    if(true) {
        var a = 1;
    }
    
    if(true) {
        // b will be undefined since 'a' was defined in another scope
        var b = a;
    }
    
    // so
    jQuery(document).ready(function({
       // this is one scope
       var a = 1;
    });
    
    jQuery(document).ready(function({
       // this is another scope and b will be undefined
       var b = a;
    });
    

    You need to store the uid in a hidden field like:

    <intput type="hidden" id="hidUid" value="<?php echo (intval($uid)); ?>"/>
    

    And then inside the scope of your javascript ($(document).ready)

    var uid = $("#hidUid").val();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having great difficulty getting my Android application to play videos from the SD
I'm having difficulty interpreting this formula that was from a past exam paper that
I am very new to this javascipt, jquery world and I am having great
I am having great difficulty reading the API for named scopes. Each bid has
I'm having a great deal of difficulty trying to figure out the logic behind
I am having great problems solving this one: I have a mysql database encoding
I am having a great deal of difficulty with getting rid of the white
I'm working from some sample code, provided in PHP - we are using c#,
I have included the necessary files for the ExtJs calendar. However I'm having difficulty
I am having great difficulty making a connection to an Oracle Database OJDBC. Error:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.