JavaScript Pass PHP variable to Javascript function whitin an AJAX request possible? pass variable javascript to php,pass variable javascript,pass variable

I try to pass a variable from Javascript (when choosing some option in a form) to a PHP file which calls a SQL query.

The SQL query (a string) should be handed over to a Javascript function and the function should be executed. Everything in one click. Is that somehow possible?

I tried that with a AJAX request but when I use this for my last step:

var javascriptstring;
      $.getJSON('getstring.php', function(data) {
        javascriptstring = data.value;
        });

I get the exception: Uncaught TypeError: Cannot set property 'innerHTML' of null

And it prints out "undefined"

.HTML

  <!DOCTYPE html>
<html>

<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type='text/javascript'>
     function showString(time) {
  if (time=="") {
    document.getElementById("txtHint").innerHTML="";
    return;
  }
  if (window.XMLHttpRequest) {
    // code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
  } else { // code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState==4 && xmlhttp.status==200) {
      document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
  xmlhttp.open("GET","getstring.php?q="+time,true);
  xmlhttp.send();

      var javascriptstring;
      $.getJSON('getstring.php', function(data) {
        javascriptstring = data.value;
        });
        document.write(javascriptstring);
}
</script>
</head>
<body>

    <form>
    <select name="somestring" onchange="showString(this.value)">
    <option value="">No string selected</option>
    <option value="1">String 1</option>
    </select>
    </form>
    <br>
    <div id="txtHint"><b>String will be listed here...</b></div>

</body>
</html>

PHP

<?php
$q = intval($_GET['q']);

$con = pg_connect("host=localhost port=5432 dbname=Twitter user=postgres password=****");
if (!$con) {
    die('Could not connect: ' . pg_errormessage($con));
}

$sql="SELECT * FROM tswholeworld WHERE createdat > (NOW() - INTERVAL '".$q."hour');";
$result = pg_query($con,$sql);

    $string = "";
while($row = pg_fetch_assoc($result)){
    $lat = $row['latitude'];
    $lon = $row['longitude'];
    $string .= "new google.maps.LatLng({$lat}, {$lon}), ";
}
echo '{"value": "'.$string.'"}';
pg_close($con);
?>
Answer:1

Keep the Javascript function definition in head or before the select tag. like below -

<!DOCTYPE html>
 <html>
   <head>
     <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
     <script type='text/javascript'>
       function showString(time) {
         if (time=="") {
         document.getElementById("txtHint").innerHTML="";
         return;
       } 
       if (window.XMLHttpRequest) {
        // code for IE7+, Firefox, Chrome, Opera, Safari
         xmlhttp=new XMLHttpRequest();
        } else { // code for IE6, IE5
          xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
         }
         xmlhttp.onreadystatechange=function() {
       if (xmlhttp.readyState==4 && xmlhttp.status==200) {
          document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
         }
       }
       xmlhttp.open("GET","getstring.php?q="+time,true);
       xmlhttp.send();

       var javascriptstring;
       $.getJSON('getstring.php', function(data) {
        javascriptstring = data.value;
        });
       document.write(javascriptstring);

     }

     </script>

   </head>
   <body>
     <form>
       <select name="somestring" onchange="showString(this.value)">
         <option value="">No string selected</option>
         <option value="1">String 1</option>
       </select>
     </form>
   </body>
 </html>
Answer:2

I want to use $timeout inside my custom AngularJS directive, but it's not working. My last implementation looks as following: var App = angular.module('App', []); App.controller('Controller', ...

I want to use $timeout inside my custom AngularJS directive, but it's not working. My last implementation looks as following: var App = angular.module('App', []); App.controller('Controller', ...

when I am searching any city or state or country in google map then I want the shading of that place as shown in figure. can any one tell me how to do it ?

when I am searching any city or state or country in google map then I want the shading of that place as shown in figure. can any one tell me how to do it ?

I set a span using: <span id=\"xxx\">&#9656;</span> Can someone tell me why the following test doesn't work: if ($("#xxx").text() == "&#9656;") alert("hello");

I set a span using: <span id=\"xxx\">&#9656;</span> Can someone tell me why the following test doesn't work: if ($("#xxx").text() == "&#9656;") alert("hello");

  1. html text special characters

I'm trying to show a hidden listbox in the TinyMCE window. I want to show the listbox after the user selects an option. Currently I have it to show another popup window in the onselect function. Here ...

I'm trying to show a hidden listbox in the TinyMCE window. I want to show the listbox after the user selects an option. Currently I have it to show another popup window in the onselect function. Here ...