Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder MaDmiX's Avatar
    Join Date
    Feb 2012
    Location
    Charlotte, NC
    Posts
    160
    Thanks
    40
    Thanked 0 Times in 0 Posts

    Is it possible to dynamically insert code into javascript?

    Hi All,

    I have a function to display a text field if the value of another field equals certain values:

    Code:
    if (theList1=="VTR"||theList1=="EVS"||theList1=="KiPro"||theList1=="Intercom"||theList1=="Camera") {
    	// use CSS style to show it
    	theEquipNum.style.display = "block";
    Now I would like to store those values in a table and use AJAX to retrieve them and format a string that I can store in a variable like so:

    PHP Code:
        $query mysql_query("SELECT `Locations` FROM `tbllocations` WHERE `numbered` = -1");

        
    $items = array();
            while (
    $listItems mysql_fetch_row($query)) {
                
    $items[] = $listItems[0];
            }

        
    $value=array_map(function($val) { return "theList1=='".$val;} , $items);

            if(
    $value){
                
    $result=implode("'||"$value)."'";
            }else{
                
    $result="";
            }

    echo 
    $result
    and the new javascript:

    Code:
      xmlhttp=new XMLHttpRequest();
      xmlhttp.onreadystatechange=function() {
    
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
          if (xmlhttp.responseText) {
            var numLoc = xmlhttp.responseText;
            //alert(numLoc);
          }
        }
      }
    
    xmlhttp.open("GET","query-numbered-locations.php",true);
    xmlhttp.send();
    
      	if (numLoc) {
    		  // use CSS style to show it
    		  theLocNum.style.display = "block";
    This does not seem to be working as the text box is not becoming visible when it should. Is this even possible, what I am attempting? If so, what am I doing wrong here? The string being echoed back looks correct, as it matches the code that it is replacing.

    Thanks in advance,

    Ken

  • #2
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    By the time that if statement is run, the callback most likely wasn't called (it is an asynchronous operation!). You need to delay execution of the logic until you receive the callback:

    Code:
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            handleResult(xmlhttp.responseText);
        }
    }
    
    function handleResult(result) {
        if(result) {
            theLocNum.style.display = "block";
        }
    }
    Note that mysql_query is deprecated and unsafe. http://php.net/manual/en/function.mysql-query.php
    Last edited by Airblader; 11-17-2013 at 08:04 PM.

  • #3
    Regular Coder MaDmiX's Avatar
    Join Date
    Feb 2012
    Location
    Charlotte, NC
    Posts
    160
    Thanks
    40
    Thanked 0 Times in 0 Posts
    Hi Airblader,

    Thanks for your reply. It seems like "result" is being treated like a boolean in that the if statement is now being triggered no matter what. In other words:

    Code:
    if("there is any result"){"do the block of code"}
    I am trying to inject the result as code within the if statement. But thanks, just the same.

    Ken

  • #4
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    I wouldn't recommend you to transfer code like this. Inside your PHP, just create an array of all the "allowed" values, then convert it to JSON and use that as the result for the ajax call. In your Javascript code parse that JSON into a Javascript object again and just check whether the value of theList is contained in it.

    Code:
    // you REALLY want to replace this deprecated and unsafe code!
    $query = mysql_query("SELECT `Locations` FROM `tbllocations` WHERE `numbered` = -1"); 
    
    $items = array(); 
    while ($listItems = mysql_fetch_row($query)) { 
        $items[] = $listItems[0]; 
    } 
    
    echo json_encode($items);
    Code:
    function handleResult(result) {
        var locationsToShowItFor = JSON.parse(result);
        if(locationsToShowItFor.indexOf(theList1) !== -1) {
            theEquipNum.style.display = "block";
        }
    }
    Last edited by Airblader; 11-17-2013 at 08:43 PM.

  • Users who have thanked Airblader for this post:

    MaDmiX (11-18-2013)

  • #5
    Regular Coder MaDmiX's Avatar
    Join Date
    Feb 2012
    Location
    Charlotte, NC
    Posts
    160
    Thanks
    40
    Thanked 0 Times in 0 Posts
    Hey Airblader,

    I got this figured out. You gave me a new way of looking at the problem. Rather than try to inject code into my javascript, I made the evaluation in my query and then returned either the text "Match" or "noMatch". This was all I needed for my if statement to do what it was supposed to do based on the returned text.

    Thanks!!!

    Ken

    PS: I will take your advice on the deprecated function too.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •