- Messages
- 7
- Likes
- 0
- Points
- 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)
----------
my javascript (written in snippet id 14541)
----------------
my php file getuser.php
---------------------------
-----------------------------------------------------------------------------------------------------
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
-> 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>
----------------
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
---------------------------
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: