用PHP打开一个新函数

时间:2016-10-14 09:35:52

标签: javascript php jquery html

我有一个PHP页面,我会点击一下打开一个函数。

函数显示了一个查询结果,但是当我编写这段代码时,它没有工作

<div class="btn-group">
        <button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown">
        Frequenza <span class="caret"></span>
        </button>
        <ul class="dropdown-menu" role="menu">
            <?php
                $query_frequenza="SELECT DISTINCT FREQUENZA FROM Dettagli_macchina WHERE  macchine_id='$macchine' and Email='$_SESSION[login_user]'";
                $result=mysqli_query($conne,$query_frequenza);
                while($row=mysqli_fetch_array($result)){
                    $frequenza=$row['FREQUENZA'];
                    echo"<li><a href='#?frequenza=$frequenza' onclick='showfiltro2()'>$frequenza</a></li>";
                }
                ?>
        </ul>
        </div>
<script type = "text/javascript">
            function showfiltro2() {
                document.getElementById("filtro2").style.display = "block";
                document.getElementById("filtro1").style.display = "none";
            }
        </script>
        <div id = "filtro2" style="display:none">
        <?php
            $filtro2=$_GET['frequenza'];
            $query="SELECT DISTINCT * FROM Dettagli_macchina WHERE macchine_id='$macchine' and Email='$_SESSION[login_user]' and FREQUENZA='$filtro2' ";
            $result=mysqli_query($conne,$query);
            echo 'Found '. mysqli_num_rows($result) .'results';
            echo "<table><tr>";
            while ($row = mysqli_fetch_array($result)) {
                echo "<tr><td>";
                echo $row['COMPONENTE'];
                echo "</td>";
                echo "<td>";
                echo $row['DETTAGLIO ATTIVITA'];
                echo "</td>";
                echo "<td>";
                echo $row['FREQUENZA'];
                echo "</td>";
                echo "<td>";
                echo $row['DATA-PREVISTA'];
                echo "</td>";
                echo "</tr>";
                }
            echo"</tr></table>";
            ?>
        </div>

1 个答案:

答案 0 :(得分:3)

您的问题源于对PHP和HTML如何工作的误解,以及数据如何在两者之间流动。

首先要记住,PHP和HTML是两个完全独立的部分,它们不会在“request-&gt; reply”链之外互相交互。
这意味着在客户端获得此处理的输出之前,所有PHP代码都在服务器上执行。服务器(PHP)不关心它是什么类型的输出,也不了解如何解析它;对于所有PHP知道,它都是简单的文本 在完全解析PHP代码之后,客户端会收到生成的文本。然后它注意到它可以将此文本理解为HTML,并将其解析为网页。此时,PHP代码根本不存在于代码中,并且Web浏览器(客户端)对此一无所知。

令人遗憾的是,如上所述,许多教程不断混合PHP和HTML代码,因为这进一步混淆了两者并使它们看起来像是交互式的。我建议将所有PHP代码移到任何HTML代码之上,并在向浏览器发送任何内容之前进行所有处理。
这不仅可以更容易地实际跟踪和理解正在发生的事情和原因;但它也允许您为代码添加更多功能,而不会破坏物理定律。 (例如:确定您不希望在生成所述表单的过程中向用户显示表单。)

所有这些意味着您不需要通过点击“打开功能”。您通过所述单击向服务器发送请求,然后PHP代码检查输入数据是否存在某些预定条件(GET参数等),然后调用所述条件的函数完成。
这样的东西,换句话说:     

// First off we should use PDO, as mysql_*() is deprecated and removed in PHP7.
$db = new PDO ($dsn);

// Using prepared statements here, to prevent SQL injections.
$stmt = $db->prepare ("SELECT DISTINCT FREQUENZA FROM Dettagli_macchina WHERE  macchine_id=:machineID and Email=:email");
$data = array (':machineID' => $macchine, ':email' => $_SESSION['login_user']);
if (!$stmt->exec ($data)) {
    // Something went wrong, handle it.
}

// Initialize a variable to hold the generated menu, and a template to use when creating it.
$menuOut = $searchOut = '';
$menuTemplate = "<li><a href='#?frequenza=%s' onclick='showfiltro2()'>%s</a></li>";

// Using prepared statements we can iterate through all of the results with foreach().
foreach ($stmt->fetchAll () as $row) {
    // Using htmlspecialchars() and rawurlescape() to prevent against XSS, and other HTML-injection attacks/mistakes.
    // Notice where and in what order I've used the different functions, as one protects the URL as well.
    $menuOut .= sprintf ($menuTemplate, htmlspecialchars (rawurlencode ($row['FREQUENZA'])), htmlspecialchars ($row['FREQUENZA']));
}

// Since this is probably the "function" you want to execute with said click, this is where we check if it
// has been sent by the client.
if (!empty ($_GET['frequenza'])) {
    // Here you want to check to see if the parameter is actually something you'd expect, and not some random(?) garbage.
    $filtro2 = $_GET['frequenza'];

    // Again, prepared statements as your code was open to SQL injections!
    $query = "SELECT DISTINCT * FROM Dettagli_macchina WHERE macchine_id=:machineID and Email=:email and FREQUENZA=:frequency";;
    $stmt = $db->prepare ($query);
    $data = array (
            ':machineID' => $macchine,
            ':email' => $_SESSION['login_user'],
            ':frequency' => $filtro2);
    if (!$res = $stmt->exec ($data)) {
        // Somethign went wrong with the query, handle it.
    }

    // Initialize a variable to hold the output, and the template to use for generating it.
    $searchOut = '<table>';
    $searchTemplate = '<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>';
    $count = 0;

    foreach ($stmt->fetchAll () as $row) {
        // Again, protection against XSS and other HTML-breaking mistakes.
        $searchOut .= sprintf ($searchTemplate,
                htmlspecialchars ($row['COMPONENTE']),
                htmlspecialchars ($row['DETTAGLIO ATTIVITA']),
                htmlspecialchars ($row['FREQUENZA']),
                htmlspecialchars ($row['DATA-PREVISTA']));
    }

    $searchOut = "<p>Found {$count} results</p>{$searchOut}</table>";
}

?>

<div class="btn-group">
    <button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown">
        Frequenza <span class="caret"></span>
        </button>
    <ul class="dropdown-menu" role="menu">
<?php echo $menuOut; ?>
    </ul>
</div>
<script type="text/javascript">
            function showfiltro2() {
                document.getElementById("filtro2").style.display = "block";
                document.getElementById("filtro1").style.display = "none";
            }
        </script>
<div id="filtro2" style="display: none">
    <?php echo $searchOut; ?>
</div>

我添加了一些注释来解释我做了什么以及为什么做了什么,以及从旧的(!),已弃用和过时的mysql_*()函数转换为PDO。
您可以阅读有关how to use PDO in the PHP manual

的更多信息