• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

RESOLVED Fetch information from a database with AJAX

Messages
7
Likes
0
Points
1
#1
My goal:
-> Through a <select> tag (triggers a javascript), I want to fetch information from a mysql database, using the XMLHttpRequest object.
-> With this AJAX method it is possible to dynamically display information (when changing choice of <select> tag) without reloading the page.


my html (with javascript snippet shortcode)
----------
HTML:
[wbcr_js_snippet id="14541"]content[/wbcr_js_snippet]
<form>
<select name="users" onchange="showUser(this.value)">
<option value="">Select option</option>
<option value="andre">andre</option>
<option value="sofie">sofie</option>
</select>
</form>
<br>
<div id="txtHint"><b>Please choose in list above your option</b></div>
my javascript (written in snippet id 14541)
----------------
JavaScript:
function showUser (str) {
  if (str == "") {
    document.getElementById("txtHint").innerHTML = "";
    return;
  } else {
    if (window.XMLHttpRequest) {
    // code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp = new XLMHttpRequest();
  }
xmlhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      document.getElementById("txtHint").innerHTML = this.responseText;
    }
  };
  xmlhttp.open("GET","getuser.php?q="+str,true);
  xmlhttp.send();
}
// this code can be found at www.w3schools.com/php/php_ajax_database.asp
my php file getuser.php
---------------------------
PHP:
$q = $_GET['q'];
global $wpdb;
$sql1 = "SELECT * FROM my_table WHERE person = '" . $q . "'";
$result = $wpdb->get_results($sql1);
foreach($result as $row)
{ echo $row->person}
-----------------------------------------------------------------------------------------------------
This does not work with Woody snippets in wordpress.
I tried this code combination outside of wordpress, and it worked.
My problem here is that I don't know how to call getuser.php.
Should I write it in a php snippet? How do I call itfrom the javascript snippet? Or are therer other solutions?

Thanks so much for your help.
Dominique
 
Last edited by a moderator:

Temyk

Developer & Support
Messages
1,129
Likes
42
Points
48
#2
Hello.

Specify the full URL of the getuser.php file.
 
Messages
7
Likes
0
Points
1
#3
Thank you.
I rewrote the getuser.php in the normal php code (see here below):
-------

$q = $_GET['q'];
$sql1 = "SELECT * FROM my_table WHERE person = '" . $q . "'";
$result = mysql_query($sql1);
while ($row = mysql_fetch_array($result))
{ echo $row['person']}

--------
and I put the full URL -> https://.../getuser.php within the javascript, see above. (I did not write out the dots for security reasons)

But this full URL didn't work either.

Outside Wordpress and Woody Snippet, the small code https://.../getuserphp?q= .. works on its own, and it works also in combination with the HTML and Javascript (outside Wordpress). But within the Wordpress environment with Woody snippet it doesn't.

Is it because AJAX applications do not work within Woody Snippet?
 

Temyk

Developer & Support
Messages
1,129
Likes
42
Points
48
#4
Woody does not restrict code in any way, including ajax requests.

The fact is that JavaScript is trying to find the getuser.php in the same folder, where js file is located.
Try specifying the path from the site root, for example:
JavaScript:
xmlhttp.open("GET","/wp-content/getuser.php?q="+str,true);
Or move this file to the site root