dongqi7631 2014-04-17 09:48
浏览 24
已采纳

如何将3行脚本组合成一个页面

Hi i currently use javascript to call small function that run a sql query on a secondary page i have about 4 to 5 of these small scripts and i was just wondering how to put them all together and be able to call the one i need out of this one page instead of having 5 different scripts that have 3 to 5 lines of code in. my code and process is below.

first the JavaScript that I am Currently using.

$("select#principle").attr("disabled","disabled");
            $("select#type").change(function(){
                var id = $("select#type option:selected").attr('value');
                $("select#principle").attr("disabled","disabled");
                $("select#principle").html("<option>wait...</option>");
                $.post("select_principle.php", {id:id}, function(data){
                    $("#Options").addClass("active");
                    $("select#principle").removeAttr("disabled");
                    $("select#principle").html(data);
                });
            });

This is a JavaScript function that I use it checks to see if a drop down box has been selected and then removes the disable attribute for the next one. but as you see there is a $.Post this calls a PHP page.

the page that is called is

<?php
    include "select.class.php";
    echo $opt->ShowPrinciple();
?> 

this is the php code that goes to the select.class.php page and runs a sql query. this is also the code that i have multiple of i want this to be in one script with others that call other functions the same way but they have to be run only when they are called.

the SQl query that is run is below

// this displays the principle when called

public function ShowAllPrinciple()
{
    $sql = "SELECT principle.principle_id,principle.description,principle.section_id,COUNT(media.principle_id) as media_count
            FROM principle 
            LEFT OUTER JOIN media ON principle.principle_id = media.principle_id 
            AND principle.section_id = media.section_id 
            WHERE principle.section_id={$_POST['id']}
            GROUP BY principle.principle_id,principle.description";
    $res = mysql_query($sql,$this->conn);
    $principle = '<option value="%">choose...</option>';
    while($row = mysql_fetch_array($res))
    {
        $principle .= '<option value="' . $row['principle_id'] . '">' . $row['description']. '...('.$row['media_count'].') </option>';
    }
    return $principle;
}

this is just a simple SQL query that connects to the database and then gets the data and displays it into the drop down box.

any ideas on how I can put these small scripts into one page and call the specific function using JavaScript only small scripts need to be within a PHP page and the SQL query is within a PHP page this cannot change.

 <?php
        include "select.class.php";
        echo $opt->ShowPrinciple();
    ?>

any help would be much appreciated

  • 写回答

1条回答 默认 最新

  • douruyun8153 2014-04-17 10:00
    关注

    I think based on your question you need something like this:

    <?php
    include "select.class.php";
    switch ($_POST['action']) {
        case "firstAction":
            echo $opt->ShowPrinciple();
            break;
        case "secondAction":
            echo $opt->ShowSomethingElse();
            break;
        case default:
            echo 'something went wrong';
            break;
    }
    ?>
    

    Where $action is a action you have to post or add in the url.

    Example of javascript: Javascript for first select box

    $("select#type").change(function() {
        var id = $("select#type option:selected").attr('value');
        $("select#principle").attr("disabled", "disabled");
        $("select#principle").html("<option>wait...</option>");
        $.post("select_principle.php", {action: 'firstAction', id: id}, function(data) {
            $("#Options").addClass("active");
            $("select#principle").removeAttr("disabled");
            $("select#principle").html(data);
        });
    });
    

    Javascript for second select box

     $("select#type").change(function() {
         var id = $("select#principle option:selected").attr('value');
         $.post("select_principle.php", {action: 'sencondAction', id: id}, function(data) {
             $("select#principle").html(data);
         });
     });
    

    Notice I didn't test this code. Once you made a change the script will post action as wel as the id to the php file. In the switch is based on $_POST['action'] in the php file.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀