I'm an amateur programmer so please be nice:-)
I've been trying (for hours and hours!) to develop a web form which on submission of one form item (eg employee number) it retrieves data from a remote SQL web server and populates the other fields in the web page (eg name, address, telephone number etc). All pretty usual stuff and I've been attempting to adopt and adapt numerous examples from StackOverflow.
The approach is HTML Form to collect EmployeeID, javascript getJSON to request the data via a remote server php script, which connects to the SQL database, queries the database creates the return array, and json_encodes it back to the web form.
The consistent problem seems to be that data is not getting passed back to the Web Form, and therefore doesn't populate the other fields.
So I searched and found a tutorial which had a downloadable "working" example code (acknowledging https://www.electrictoolbox.com/json-data-jquery-php-mysql/). This example populates the Variety select options when a fruit is selected and is very simple in its coding approach. I am using this to get the basic functionality working, but not even this simple code wants to play nicely:-) I'm sure it's something simple but I just can't seem to find the issue.
I did change the connection to the SQL database from PDO to Mysqli as I couldn't get PDO to work at all with the SQL Server.
What I'm expecting is that the form will display the items under the Variety dropdown box extracted from the database to match the type of fruit.
Here's the fruit.html file with the Javascript:
<!doctype html>
<html>
<head>
<title>Untitled 1</title>
</head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script type="text/javascript">
function populateFruitVariety() {
$.getJSON('https://mysite.com.au/Secure/fruit-varieties.php', {fruitName:$('#fruitName').val()}, function(data) {
var select = $('#fruitVariety');
var options = select.prop('options');
$('option', select).remove();
$.each(data, function(index, array) {
options[options.length] = new Option(array['variety']);
});
});
}
$(document).ready(function() {
populateFruitVariety();
$('#fruitName').change(function() {
populateFruitVariety();
});
});
</script>
<body>
<form name="testfruit" onsubmit="populateFruitVariety();">
Fruit: <select id="fruitName" name="name">
<option>Apple</option>
<option>Banana</option>
<option>Orange</option>
<option>Pear</option>
</select> Variety: <select id="fruitVariety" name="variety"></select>
<input type="submit" name="submit" value="Search">
</form>
</body>
</html>
and here's the fruit-varieties.php which runs on the Remote SQL Server:
<?php
// Connect using SQL Server Authentication.
$con = mysqli_connect($serverName, $username, $password);
if (!$con) {
echo 'Unable to connect to SQL server: ' . mysqli_error();
die('Unable to connect to SQL server: ' . mysqli_error());
}
// Select database
mysqli_select_db($con,$databaseName);
if (!$con) {
echo 'Unable to open database: ' . mysqli_error();
die('Unable to open database: ' . mysqli_error());
}
$fruitName = $_GET['fruitName'];
echo $fruitName.'|';
$rows = array();
if(isset($_GET['fruitName'])) {
$sql= "SELECT variety FROM fruit WHERE name = '$fruitName' ORDER BY variety";
$rows = mysqli_fetch_assoc(mysqli_query($con, $sql));
if (!$con) {
echo 'Query failed: ' . mysqli_error();
die('Query failed: ' . mysqli_error());
}
}
echo json_encode($rows);
?>
and finally, here is the SQL database construct and sample data:
SQL to create the table and populate with example data
CREATE TABLE IF NOT EXISTS `fruit` (
`fruit_id` int(10) unsigned NOT NULL auto_increment,
`name` varchar(50) NOT NULL,
`variety` varchar(50) NOT NULL,
PRIMARY KEY (`fruit_id`)
);
INSERT INTO `fruit` (`fruit_id`, `name`, `variety`) VALUES
(1, 'Apple', 'Red Delicious'),
(2, 'Pear', 'Comice'),
(3, 'Orange', 'Navel'),
(4, 'Pear', 'Bartlett'),
(5, 'Orange', 'Blood'),
(6, 'Apple', 'Cox''s Orange Pippin'),
(7, 'Apple', 'Granny Smith'),
(8, 'Pear', 'Anjou'),
(9, 'Orange', 'Valencia'),
(10, 'Banana', 'Plantain'),
(11, 'Banana', 'Burro'),
(12, 'Banana', 'Cavendish');
Thank you everyone - really appreciate any help you can give me!
Cheers ...Steve