【问题标题】:onchange event and auto refresh table after submitonchange 事件和提交后自动刷新表
【发布时间】:2016-09-10 04:49:53
【问题描述】:
  1. 当我从第一个下拉菜单中选择一个选项然后自动显示表格而不点击提交时,如何创建 onchange 事件?
  2. 如何在编辑数据上点击提交时刷新上表?

            <?php
    
        $conn = new mysqli('localhost', 'root', 'jared17', 'hbadb') 
        or die ('Cannot connect to db');
    
        $result = $conn->query("select * from english");
    
        echo "<html>";
        echo "<body>";
        echo "<form name='form' method = POST>";
        echo "<select name = 'Students'>";
        while ($row = $result->fetch_assoc()) {
            $LRN = $row['LRN'];
            $Last = $row['Last_Name']; 
            $First = $row['First_Name'];
            if ($LRN == $_POST['Students']) $selected = 'selected="selected"';
            echo '<option value="'.$LRN.'">'.$Last.', '.$First.'</option>';
    
        }
         echo "</select>";
        echo "<input type='submit' name='submit' value='Show'>";
    
    
        if (isset($_POST['Students'])) {
            $lrn = $_POST['Students'];
            $stmt = $conn->prepare("SELECT Last_Name, First_Name, Level, Q1, Q2, Q3, Q4, FINAL FROM english WHERE LRN = ?");
            $stmt->bind_param('i', $lrn);
            $stmt->execute();
            $stmt->bind_result($last, $first, $level, $q1, $q2, $q3, $q4, $final);
            $stmt->fetch();
            echo "<table><tr><th>LRN</th><th>Name</th><th>Level</th><th>Q1</th><th>Q2</th><th>Q3</th><th>Q4</th><th>Final</th></tr>";
            echo "<tr><td>$lrn</td><td>$last, $first</td><td>$level</td><td>$q1</td><td>$q2</td><td>$q3</td><td>$q4</td><td>$final</td></tr></table>";
        }
        echo "</form>";
    
        echo "<form name='form2' method = POST>";
        ///////////EDIT DATA
        echo "Edit Data: ";
        echo "<select name = 'Edit'>";
    
        echo '<option value=Q1>Q1</option>';
        echo '<option value=Q2>Q2</option>';
        echo '<option value=Q3>Q3</option>';
        echo '<option value=Q4>Q4</option>';
        echo '<option value=FINAL>FINAL</option>';
        echo '<input type="number" name="editdata">';
        echo "</select>";
        echo "<input type='submit' name='submit2' value='Edit Now'>";
    
    
        if (isset($_POST['Edit'])) {
            $conn2 = new mysqli('localhost', 'root', 'jared17', 'hbadb') 
            or die ('Cannot connect to db');
            $upd = $_POST['Edit'];
            $txt = $_POST['editdata'];
            $now = "UPDATE english SET $upd='$txt' WHERE LRN='$lrn'";
            $res = $conn2->query($now);
            if (!$conn2->error) {
                echo "Errormessage: $conn->error";
        }
            echo $now;
    
        }
    
    
    
        echo "</form>";
    
    
    
        echo "</body>";
        echo "</html>";
        ?>
    

【问题讨论】:

    标签: php sql html-table onchange


    【解决方案1】:

    你所有的问题都必须用 ajax 解决。

    $.ajax({
        url: "example.php", //file which has query select to db table
        data: {id:theid},   //describe your value of select option here
        dataType: 'json',   // type of data that will you get (JSON/HTML).
        type: 'POST',       //sending type (POST/GET)
        success: function(data) {
           showTable();
        }
    });
    

    【讨论】:

    • 你能给我举个例子吗?我是新手,对不起
    • 学习ajax,建议阅读这个api.jquery.com/jquery.ajax/
    猜你喜欢
    • 2023-03-10
    • 2019-06-10
    • 1970-01-01
    • 2023-03-28
    • 2021-07-07
    • 2012-10-29
    • 2012-08-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多