Hi,
You need to somehow pass the selected value to your database script then add a 'where .. = [**] ' clause to the query, where [**] will be the passed in value.
I have put some snippets together to give you a better idea. It uses the onBlur() handler, like in the last post, but this time an Ajax call is made.
This time a custom loadURL function is used as the onBlur() event handler, on the select box (could be a textbox), each time the select box loses focus a call is made to retrieve the postcode of the related name, via the db script.
The value is passed to the db script using GET.
I have attached 2 files:
A html file containing all client side code, and the db php script
HTML Code:
<html>
<title>Javascript test</title>
<head>
<script>
var req = null;
function processReqChange()
{
if (req.readyState == 4 && req.status == 200 && req.responseText )
{
var postcode = req.responseText;
document.form1.postcode.value = postcode;
}
}
function loadUrl( url )
{
if(window.XMLHttpRequest) {
try { req = new XMLHttpRequest();
} catch(e) { req = false; }
}
else if(window.ActiveXObject)
{
try { req = new ActiveXObject("Msxml2.XMLHTTP");
} catch(e) {
try { req = new ActiveXObject("Microsoft.XMLHTTP");
} catch(e) { req = false; }
}
}
if(req) {
req.onreadystatechange = processReqChange;
req.open("GET", url + "?aname=" + document.form1.selcombo.value, true);
req.send("");
}
}
</script>
<form id="form1" name="form1">
<select name="selcombo" id="selCombo"
onBlur="loadUrl( 'sources/js.php' )" />
<option value="Paul Smith"/>Paul Smith</option>
<option value="Daniel Smith"/>Daniel Smith</option>
</select>
<input type="text" name="postcode" readonly /><br/>
</form>
</body>
</html>
PHP Code:
<?php
require_once("DB.php");
$name = trim(htmlspecialchars($_GET['aname'], ENT_QUOTES));
/* Select queries return a resultset */
if ($result = $mysqli->query("SELECT `author`, `title` FROM `books` where `author` = '$name'")) {
/* fetch object array */
$row = $result->fetch_row();
$items = $row[1];
/* free result set */
$result->close();
}
/* close connection */
$mysqli->close();
?>
<?php echo $items; ?>
Bookmarks