duanmianhong4893 2011-12-15 18:17
浏览 80

将params通过ajax传递给php

I know this is a very basic question, but i couldnt figure how to fix the code even after crawling the web for past 1 hour. I have an unordered list containing the information about the categories in the database, with cat_id as primary key. and a subject table with cat_id as its foreign key, so i want to access the subjects table through ajax request for given category ID. below is the code i used to generate categories. Where i am stuck is, i dont know which DOM element to fetch in order to send the unique id in the url parameter .. thanks ..

    <ul id="search_form">
       <?php
       $cat = Category::find_all();
       foreach($cat as $category) {
           echo '<li id="';
           echo $category->cat_id;
           echo '"><a href="subject.php?id=';
           echo $category->cat_id;
           echo'">';
           echo $category->category;
           echo '</a></li>';

       }

       ?>
    </ul>
    <div id="results">
        <!-- ajax contents goes here -->
    </div>

the ajax file is

    window.onload = init;
    function init() {
      if (ajax) {
        if (document.getElementById('results')) {
          document.getElementById('search_form').onclick = function() {

            ajax.open('get', 'subject.php?id='+id ); // subject.php?id=
            // how will i pass the variable
            ajax.onreadystatechange = function() {
              handleResponse(ajax);
            }
            ajax.send(null);
            return false; 
          } 
        }
      } 
    } 
    function handleResponse(ajax) {
      if (ajax.readyState == 4) {
        if ((ajax.status == 200) || (ajax.status == 304) ) {
          var results = document.getElementById('results');
          results.innerHTML = ajax.responseText;
          results.style.display = 'block';

        } 
      } 
    } 

and the subject.php

    <?php 
    //include("tpl/header.php");
    include("includes/initialize.php");
    ?>
       <h2></h2>
        <?php
            if (isset($_GET['id'])) {
                $id= mysql_real_escape_string($_GET['id']);
                $subject = Subject::find_subject_for_category($id);
                foreach($subject as $subj) {
                    echo $subj->subject_title;
                }
            } else {
                echo "No ID Provided";
            }
        ?>
  • 写回答

1条回答 默认 最新

  • dongpao1083 2011-12-15 18:35
    关注

    I have used Jquery to do these kind of things and it works fine for me.

    $.ajax({
      type: GET,
      url: "subject.php",
      data: {id: $('#search_form :selected').val()},
      success: function(result){
        // callback function
      }
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏