我试图让我的ajax根据我选择的下拉列表响应多个值。
这是我的JS更新
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script>
$('#student_infor').change(function()
{
var first = $('#student_infor').val();
var req = $.get('frm_get_stud_record.php', {dd: first,action: 'ajax'});
req.done(function(row)
{
var whatever = JSON.parse(row);
$('#studen_title').val(whatever.stu_title);
$('#student_first_name').val(whatever.stu_first_name);
});
});
</script>
这是我的ajax php
<?php
$ajax = false;
$dbValue = 1; //or the default value of your choice - matched to the default selection value of the dropdown
if(isset($_GET['action']) && $_GET['action'] == 'ajax' && isset($_GET['dd']))
{
$dbValue = intval( $_GET['dd'] );
$ajax = true;
}
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "college_school_system";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT distinct a.stu_unique_id,
a.stu_title,
a.stu_first_name,
a.stu_middle_name,
a.stu_last_name,
a.stu_gender,
a.stu_email_id,
a.stu_mobile_no,
a.stu_dob,
b.stu_course_id,
b.stu_batch_id,
b.stu_section_id,
c.course_name,
d.batch_name,
e.section_name
from stu_info a INNER JOIN stu_academics b
ON
a.stu_unique_id = b.stu_acad_stu_info_id
inner join courses c
ON
b.stu_course_id = c.course_id
INNER JOIN batches d
ON
b.stu_batch_id = d.batch_id
INNER JOIN section e
ON
b.stu_section_id = e.section_id
where a.stu_unique_id = $dbValue
ORDER BY d.batch_name DESC";//die($sql);
$res = mysqli_query($conn,$sql);
$title = '';
$f_name = '';
while ($row = mysqli_fetch_array($res)){
$f_name = $row['stu_first_name'];
$m_name = $row['stu_middle_name'];
$l_name = $row['stu_last_name'];
$title = $row['stu_title'];
$gender = $row['stu_gender'];
$email = $row['stu_email_id'];
$mobile = $row['stu_mobile_no'];
$birth = $row['stu_dob'];
//$title = "$title";
if($ajax) echo $title;
if($ajax) echo $f_name;
}
//die($dataTable);
?>
我在这里尝试实现的是让Ajax返回多个值,这样我就可以在各自的输入类型文本框中插入这些返回的值。