开发者

How to extract xml data using jQuery and update a web page with the extracted data

I have a page that has parts that need to be updated upon some action carried out Here is a description

There will be three dropdowns When the first dropdown changes value the second dropdown should contain option specific to first dropdown selected item, second dropdown changing third dropdown options and so forth.

<html>
<head></head>
<body>
<select name=section1 id=section1>
    <option value=option1>Option1</option>
    <option value=option2>Option2</option>
</select>
<select name=section2 id=section2>
    <option value=option1>Option1</option>
    <option value=option2>Option2</option>
</select>
</body>
</html>

Here is the Javascript

 $('#section1').removeAttr('onchange'); 
 $('#section1').change(function () {
var level = $("#section1").val();
al开发者_开发技巧ert(level);

 $.ajax({
   type:'post',
   url: 'ajax/education.php',
   data: "level="+level,
   dataType:'xml',
   success: function(data) {
 // alert(jqXHR.response);

//classes=data.find('classes').text();
   $('#section2').html(classes);

   },
  error: function(jqXHR,textStatus,errorThrown) { alert(jqXHR.response); }
 });    
 });

Here is the php file that generate the xml;

 require_once('../library/config.php');
 require_once('../library/database.php');


$db = new Database();
$level_classes='';
$class_subjects='';

if(isset($_POST['level'])){
$level = $_POST['level'];
$db->query("SELECT l.class_name,c.id,c.class FROM level l JOIN class c ON    c.level_id=l.id WHERE l.id='$level'");
foreach($db->getData() as $data){
    extract($data);
    $level_classes.="<option value='$id'>$class_name $class</option>";

}
 }elseif(isset($_POST['classes'])){
$class=$_POST['classes'];
$db->query("SELECT s.id,s.name FROM class_subjects cs l JOIN subjects s ON s.id=cs.subject_id WHERE cs.class_id='$class' ORDER BY s.name");
foreach($db->getData() as $data){
    extract($data);
    $class_subjects .="<option value='$id'>$name</option>"; 
}   

 }elseif(isset($_POST['subjects'])){
$class=$_POST['classes'];
$db->query("SELECT s.id,s.name FROM class_subjects cs l JOIN subjects s ON s.id=cs.subject_id WHERE cs.class_id='$class' ORDER BY s.name");
foreach($db->getData() as $data){
    extract($data);
    $class_subjects .="<option value='$id'>$name</option>"; 
}   

 }
 echo '<?xml version= "1.0" encoding="utf-8" standalone="yes" ?>';
 echo "<classes>".htmlentities($level_classes)."</classes>";
 echo "<subjects>".htmlentities($class_subjects)."</subjects>";

Using the code above, I get the error part working than the success part of the jQuery ajax. Where am I wrong

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜