我对使用AJAX非常陌生,在用户单击svg图像的一部分后,使用AJAX将数据从php处理程序文件加载到html div元素时遇到了问题。
HTML代码-一旦php发送数据库信息,将由ajax更新
<div class="col-md-5 col-sm-5 exercises">
<h2 style="text-align: center;">Exercises</h2>
<div class="exercise-list">
<?php //$exercises->getExercises($result); ?>
<div class='media'><h3 id="exercise-list">Pick a muscle</h3>
</div>
</div>
</div>
ExerciseHandler.php-无法弄清楚$ _GET中的内容还是应该使用其他方法?
<?php
require_once "Exercises.class.php";
include 'dbconnect.php';
$connect = new mysqli($servername, $username, $password, $dbname);
$sql = "SELECT Muscle, Exercise, ImgResource, Descrip FROM exercises";
$result = $connect->query($sql);
if(isset( $_GET[''] )) {
$exercises = new Exercises();
$exercises->getExercises($result);
}
?>
Exercise.class.php
class Exercises {
public function getExercises($result) {
if ($result->num_rows > 0) {
// output data of each row
while ($row = $result->fetch_assoc()) {
echo "<div class='media'>";
echo "<div class='media-object pull-left'>";
echo "<a href='#'><img src='".$row["ImgResource"]."' class='img-responsive' alt='curl'></a>";
echo "</div>";
echo "<div class='media-body'>";
echo "<h4 class='media-heading'><a href='#'>".$row["Exercise"]."</a></h4>";
echo "</div>";
echo "</div>";
}
}
}
}
?>
loadExercises.js
function getExercises(clicked_muscle){
var muscle = clicked_muscle;
alert("You selected" + muscle);
if (window.XMLHttpRequest) {
//modern browsers
ajaxRequest = new XMLHttpRequest();
} else {
// code for old IE browsers
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
ajaxRequest.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("exercise-list").innerHTML=this.responseText;
};
}
ajaxRequest.open("GET", "ExerciseHandler.php", true);
ajaxRequest.send();
}
Getexercises函数。肌肉的事实之一
public function getExercises($result) {
if ($result->num_rows > 0) {
// output data of each row
while ($row = $result->fetch_assoc()) {
echo "<div class='media'>";
echo "<div class='media-object pull-left'>";
echo "<a href='#'><img src='".$row["ImgResource"]."' class='img-responsive' alt='curl'></a>";
echo "</div>";
echo "<div class='media-body'>";
echo "<h4 class='media-heading'><a href='#'>".$row["Exercise"]."</a></h4>";
echo "</div>";
echo "</div>";
}
}
}
答案 0 :(得分:1)
请检查以下代码。
HTML代码,我对该文件中的select选项进行了一些更改,并为所调用的ajax调用了jquery函数。
<div class="col-md-5 col-sm-5 exercises">
<h2 style="text-align: center;">Exercises</h2>
<div class="exercise-list">
<select onchange="getExercises(this)">
<option value="1">Test</option>
<option value="2">Test2</option>
</select>
<?php //$exercises->getExercises($result); ?>
<div class='media'><h3 id="exercise-list">Pick a muscle</h3>
</div>
</div>
</div>
loadExercises.js 我在GET方法中传递了数据。
function getExercises(clicked_muscle){
var muscle = set.value;;
alert("You selected" + muscle);
if (window.XMLHttpRequest) {
//modern browsers
ajaxRequest = new XMLHttpRequest();
} else {
// code for old IE browsers
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
ajaxRequest.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("exercise-list").innerHTML=this.responseText;
};
}
ajaxRequest.open("GET", "ExerciseHandler.php?value="+muscle, true);
ajaxRequest.send();
}
ExerciseHandler.php
<?php
require_once "Exercises.class.php";
include 'dbconnect.php';
$connect = new mysqli($servername, $username, $password, $dbname);
$sql = "SELECT Muscle, Exercise, ImgResource, Descrip FROM exercises";
$result = $connect->query($sql);
if(isset($_GET) && !empty($_GET)) {
$exercises = new Exercises();
$exercises->getExercises($result);
}
?>
答案 1 :(得分:0)
这是有效的代码
$("#Biceps").click(function(){
$.get("ExerciseHandler.php", function(data, status){
$("#exercise-list").html(data);
});
});