I have a dropdown with content )Course Title) from the database in the index.php file. I get dropdown selected data and send it to a PHP file. In PHP file using received (Course Title), I select related Course (Start Date) and (End Date) and send them to a PHP file using AJAX.
I have two input tags of type text, which I want its value to be related (Start Date) and (End Date) when users click a course title.
When I show the received (Start Date) and (End Date) in a div it is showing well, but when I want to set the values of input tags relatively start_date and end_date it is not showing anything.
I am new in ajax and jquery and don't know what is problem
following is my index.php
<!DOCTYPE html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>
<body>
<form method="post" action="insert.php">
<select name="training_title" id="dropdownList"">
<option value=""></option>
<?php
require('db.php');
$sql = "SELECT distinct(COURSE_TITALE) FROM [MicrosoftDynamicsAX].[dbo].[ETIS_Course_Final]";
$params = array();
$options = array("Scrollable" => SQLSRV_CURSOR_KEYSET);
$stmt = sqlsrv_query($conn, $sql, $params, $options);
$row_count = sqlsrv_num_rows($stmt);
if ($row_count === false)
echo "Error al obtener datos.";
else
while ($row = sqlsrv_fetch_array($stmt)) {
?>
<option name="training_title" value="<?php echo $row[0]; ?>"><?php echo $row[0]; ?></option>
<?php
}
sqlsrv_close($conn);
?>
</select>
<input type="text" name="start_data" id="start_data" value="">
<input type="text" name="end_date" id="end_date" VALUE="">
<button type="submit" name="submit" id="submit"> Submit</button>
</form>
<div id="result"></div>
<script>
$(document).ready(function () {
$('#dropdownList').on('change', function () {
var val1 = $("#dropdownList option:selected").val();
$.ajax({
type: 'POST',
url: 'test.php',
data: {text1: val1},
success: function (response) {
$('#result').html(response);
}
});
});
});
</script>
</body>
and following is test.php file
<?php
$text1 = $_POST['text1'];
require('db.php');
$sql = "SELECT distinct(STARTDATETIME) FROM [MicrosoftDynamicsAX].[dbo].[ETIS_Course_Final] where COURSE_TITALE='$text1'";
$params = array();
$options = array("Scrollable" => SQLSRV_CURSOR_KEYSET);
$stmt = sqlsrv_query($conn, $sql, $params, $options);
$row_count = sqlsrv_num_rows($stmt);
if ($row_count === false)
echo "Error.";
else
while ($row = sqlsrv_fetch_array($stmt)) {
echo $row['STARTDATETIME']->format('d/m/y');
?>
<?php
}
$sql1 = "SELECT distinct(ENDDATETIME) FROM [MicrosoftDynamicsAX].[dbo].[ETIS_Course_Final] where COURSE_TITALE='$text1'";
$params1 = array();
$options1 = array("Scrollable" => SQLSRV_CURSOR_KEYSET);
$stmt1 = sqlsrv_query($conn, $sql1, $params1, $options1);
$row_count = sqlsrv_num_rows($stmt1);
if ($row_count === false)
echo "Error.";
else
while ($row = sqlsrv_fetch_array($stmt1)) {
echo $row['ENDDATETIME']->format('d/m/y');
?>
<?php
}
sqlsrv_close($conn);