I have a jQuery function that takes the selected option of a form select element and sends it to a php file, which is supposed to return the html to populate a second select element. Unfortunately, the jQuery is firing, but the return is empty. Any ideas?
Form elements:
if ($result) {
echo '<label>*Team: <select name="team" class="team" style=\'width: 150; font-size: 16px;\' autocomplete="off" tabindex="1">';
echo '<option value="">Select</option>';
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo '<option value="'.$row['TeamID'].'">'.$row['CaptainLast']." ".date('m-y',strtotime($row['ArrDate'])).'</option>';
}
echo '</select></label>';
} else {
echo "0 results";
}
?>
<label>*Team Member: <select name="member" class="member" style='width: 150; font-size: 16px;' autocomplete="off" tabindex="2">
<option value="">Select</option>
</select></label>
The jQuery:
$(document).ready(function(){
$(document).on('change','.team', function(){
var id=$(".team option:selected").val();
var dataString = 'tmid='+ id;
console.log(dataString);
$.ajax({
type: "POST",
url: "scripts/memfltpop.php",
data: dataString,
dataType: 'html',
success: function(html){
$(".member").html(html);
}
});
});
});
and the server file:
<?php
$team = $_POST['tmid'];
echo $team;
$con = mysqli_connect('**********', '****', '******', '*******');
if ($con) {
$sql = "SELECT MemberAdmin.IndID, MemberAdmin.First, MemberAdmin.Last ". "FROM MemberAdmin ". "LEFT JOIN Members ON MemberAdmin.IndID = Members.IndID ". "WHERE Members.TeamID='" .$team. "'";
$result = mysqli_query($con,$sql) or die(mysqli_error());
if ($result) {
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$id=$row['IndID'];
$data=$row['First']." ".$row['Last'];
echo '<option value="">Select</option>';
echo '<option value="'.$id.'">'.$data.'</option>';
}
} else {
echo "0 results";
}
}
else {
echo'Not connected to database';
}
For getting response in html, you need to use dataType html inside ajax request like this :
$.ajax({
type: "POST",
url: "scripts/memfltpop.php",
data: dataString,
dataType : 'html',
success: function(html){
$(".member").html(html);
}
});
Wow, found it. I never thought this would cause the SQL to freak out, but I changed from
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
to
while($row = mysqli_fetch_array($result, MYSQL_ASSOC)) {
As I was updating my SQL language, I missed this line. Thanks guys!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.