【发布时间】:2018-10-18 17:44:40
【问题描述】:
我和一个朋友试图弄清楚如何创建一个函数,该函数会根据下拉菜单中设置的值从数据库中提取一行。经过大量搜索,我们已经走到了这一步,但点击时什么也没有发生。如果我只加载 php,我只会收到带有“旧方法”的错误消息。任何帮助,将不胜感激。如果这对答案很重要,则只需要对结果进行样式设置。
(添加原始 AJAX 代码)在中间。
谢谢!
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type ="text/javascript">
// <!-- Change fields to be sourced by field list in future -->
let FIELD_LIST = [
"Any Field",
"Animation",
"Audio",
"Culinary",
"Fashion",
"Gaming",
"Industrial Design",
"Interior Design",
"Photo",
"Video",
"Web Design"
];
let INTEREST_MAP = {
"Any Field": ["Any Interest"],
"Animation": ["Modeling", "Motion", "Lighting", "Backgrounds", "Learning Animation"],
"Audio": ["ADR", "Audio Post", "Composer", "Electrician", "Foley", "Mixer", "On-Set", "Sound Design", "Studio", "Learning Audio"],
"Culinary": ["Baker", "Catering", "Cuisine (specify)", "Management", "Learning Culinary"],
"Fashion": ["Costume", "Design", "Marketing", "Tailor", "Wardrobe", "Learning Fashion"],
"Gaming": ["3D Artist", "Animator", "Art Director", "Concept Artist", "Environment Artist", "Game Tester", "Modeler", "Motion Capture", "Project Manager", "Programmer", "UI Artist", "Learning Gaming"],
"Industrial Design": ["Product", "Learning Industrial Design"],
"Interior Design": ["Commercial", "Drafting", "Environment", "Hospitality", "Institution", "Residential", "Learning Interior"],
"Photo": ["Commercial", "Event", "Fashion", "Narrative", "Nature", "Real Estate", "Sports", "Wedding", "Learning Photo"],
"Video": ["Cinematographer", "Director", "Editor", "Producer", "Production Assistant", "Scriptwriter", "Learning Video"],
"Web Design": ["Developing", "Interface", "Typography", "User Experience", "Learning Web Design"]
};
function populate2(fieldId, interestId) {
let field = document.getElementById(fieldId).value;
let interestList = document.getElementById(interestId);
interestList.innerHTML = "";
for(let interest in INTEREST_MAP[field]){
let newOption = document.createElement("option");
newOption.value = INTEREST_MAP[field][interest];
newOption.innerHTML = INTEREST_MAP[field][interest];
interestList.options.add(newOption);
}
}
// $(document).ready(function(){
// $("#collaborate").click(function(){
// $.get("Profiles.php", function(data){
// alert("Data: " + data);
// });
// });
// });
$(document).ready(function(){
$('#collaborate').click(function() {
event.preventDefault();
$.get('userprofiles.php', {ajax: true},function(data, status){
alert(data);
});
});
});
</script>
<div id="container">
<div class="column">
<select class="field" name="field" id="field" onchange="populate2('field','interestId')" >
<option value="Any Field">Any Field</option>
<option value="Animation">Animation</option>
<option value="Audio">Audio</option>
<option value="Culinary">Culinary</option>
<option value="Fashion">Fashion</option>
<option value="Gaming">Gaming</option>
<option value="Industrial Design">Industrial Design</option>
<option value="Interior Design">Interior Design</option>
<option value="Photo">Photo</option>
<option value="Video">Video</option>
<option value="Web Design">Web Design</option>
</select>
<h1 class="info1">1</h1>
<h3 class="info1">Select an artist's field of choice you need</h3>
</div>
<div class="column">
<select class= "interestId" name="interestId" id="interestId">
<option value="Any Interest">Any Interest</option>
</select>
<h1 class="info2">2</h1>
<h3 class="info2">Select a specific interest you're looking for</h3>
</div>
<div class="column">
<button class="button" id="collaborate" name="collaborate">
Collaborate!
</button>
</div>
</div>
</div>
原始 AJAX 代码(根据我朋友的理解)
$(document).ready(function(){
$('#collaborate').click(function() {
$.ajax({
type: "GET",
url: "userprofiles.php",
data: { 'interest' : "asd" } // this line does nothing at the moment. But ideally it is used to pass data directly as variables to your php document.
}).done(function( result ) {
// alert( "Data Saved: " + result );
$('#data').text(data);
});
});
});
PHP
$Interest = $_GET['interestId'];
$sql = "SELECT * from User WHERE Interest1='$Interest' OR Interest2='$Interest' OR Interest3 ='$Interest'";
$result = mysqli_query($sql);
// $row = mysql_fetch_row($result);
// echo "<pre>";
// print_r($row);
// echo "</pre>";
// attempt select query execution
// OLD METHOD BELOW
if ($result) {
print_r($result);
mysqli_free_result($result);
} else {
echo "ERROR: Could not execute $sql. " . mysqli_error($link);
}
【问题讨论】:
-
“点击没有任何反应”。您首先需要找出失败的确切位置 - 点击处理程序是否触发? ajax 失败了吗?查询失败了吗?在您的点击处理程序中放置一个警报,以查看它是否被触发。打开浏览器控制台并查找错误。
-
这就是我们感到困惑的地方。没有错误。我对调试器一无所知,但我的朋友是。他逐步检查了我们将添加的原始代码,但没有发现任何问题。我决定改变它,看看能不能让它工作。
-
请连同代码一起提供详细信息。不仅仅是整个页面的代码。简要说明
-
@AnikethSaha 你能解释一下你需要什么更多的细节吗?在编程方面我是个新手(朋友要好得多),但这是我们俩都没有运气的事情。
-
所以基本上你想要的是:“我想在单击标有 Collaborate 的按钮时从 DB 中提取记录”?
标签: php jquery mysql arrays ajax