doulu1968 2015-01-01 16:22
浏览 139
已采纳

将php结果保存为div中的内容

EDIT!!! : LINK = http://i299291.iris.fhict.nl/PHP31/DV3/DV3.php

My problem:

I've made two dropdown boxes with several options. The php code is working and the query gets the right result from the database. But now i want to compare two options.

This is what i've got so far, the problem now is the entire page refreshes when i enter the second value from the other dropdown box.

    <html>
    <head>
        <link rel="stylesheet" href="style.css">
    </head>
    <body>
    <form id = "leftDropdown" action= "" method="post">
    <select name="objectLinks">
      <option value="school">School</option>
      <option value="klas">Klas</option>
      <option value="geslacht">Geslacht</option>
      <option value="lengte">Lengte (CM)</option>
      <option value="kg">Gewicht (KG)</option>
      <option value="opleiding">Opleiding Ouders</option>
      <option value="leeftijdJaar">Leeftijd</option>
      <option value="interventie">Deelname interventie?</option>
      <option value="pestenVoor">Pestincidenten voor interventie</option>
      <option value="pestenNa">Pestincidenten na interventie</option>
      <option value="bmi">BMI waarde</option>
      <option value="overgewicht">Overgewicht</option>
      <option value="allochtonenPerc">Percentage Allochtonen</option>
    </select>
    <input type="submit" name="sendLinks" value="Go!">
    </form> 

    <form id = "rightDropdown" action= "" method="post">
    <select name="objectRechts">
      <option value="school">School</option>
      <option value="klas">Klas</option>
      <option value="geslacht">Geslacht</option>
      <option value="lengte">Lengte (CM)</option>
      <option value="kg">Gewicht (KG)</option>
      <option value="opleiding">Opleiding Ouders</option>
      <option value="leeftijdJaar">Leeftijd</option>
      <option value="interventie">Deelname interventie?</option>
      <option value="pestenVoor">Pestincidenten voor interventie</option>
      <option value="pestenNa">Pestincidenten na interventie</option>
      <option value="bmi">BMI waarde</option>
      <option value="overgewicht">Overgewicht</option>
      <option value="allochtonenPerc">Percentage Allochtonen</option>
    </select>
    <input type="submit" name="sendRechts" value="Vergelijk!">
    </form> 

    <div id = "leftDiv">

        <?php 

        include_once 'dv3ToDB.php'; // connect to database *local or at school's server*

        session_start();

        if(isset($_POST['sendLinks'])){
            $selectedValLinks = $_POST['objectLinks'];
            // echo "Jij selecteerde: ".$selectedVal;
            echo "<script>console.log('$selectedValLinks');</script>";
            // $_SESSION["valLinks"] = $selectedValLinks;
            // echo $_SESSION["valLinks"];

        $query = "SELECT ($selectedValLinks) FROM pesten ORDER BY ($selectedValLinks) * 1";
        $result = $conn->query($query);

        if ($result->num_rows > 0) {
            while ($row = $result->fetch_assoc()) {
                echo "Gevonden data in ".$selectedValLinks.": " . $row["$selectedValLinks"] . "<br>" ;
            }
        }else{
            echo "0 results";
        }

        }



    ?>

    </div>

    <div id = "rightDiv">
    <?php



        if(isset($_POST['sendRechts'])){
            $selectedValRechts = $_POST['objectRechts'];
            // echo "Jij selecteerde: ".$selectedVal;
            echo "<script>console.log('$selectedValRechts');</script>";
            // $_SESSION["valRechts"] = $selectedValRechts;
            // echo $_SESSION["valRechts"];

        $conn = mysqli_connect($host,$username,$password,$database)
        or die("verbinding mislukt:".mysqli_connect_error());

        $query = "SELECT ($selectedValRechts) FROM pesten ORDER BY ($selectedValRechts) * 1";
        $result = $conn->query($query);

        if ($result->num_rows > 0) {
            while ($row = $result->fetch_assoc()) {
                echo "Gevonden data in ".$selectedValRechts.": " . $row["$selectedValRechts"] . "<br>" ;
            }
        }else{
            echo "0 results";
        }

        }


     ?>
    </div>
    </body>
    </html>

i'm running it local now, let me know if you need me to build a database online to help me out.

How can i code it so the PHP output stays in de left DIV and/or the right DIV?

Thank you so much guys and girls! :)

  • 写回答

3条回答 默认 最新

  • douweidao3882 2015-01-01 17:00
    关注

    If you need to use only PHP, without any JavaScript (which could allow you to display the result from the database after selecting each option without reloading the page), I'd suggest that, after submitting one form, you add it's result as a hidden input for the other form, so you could read this variable after submitting the 2nd form. Like this:

    <? // Your code for getting the result of the 1st form above returns a variable $res1
    // Now insert this into the 2nd form:?>
    <input type="hidden" name="stored2" value="<?echo $res1;?>"/>
    

    This way, you can read the variable $_POST["stored2"] after submitting the 2nd form to get what the 1st form returned before. Do the same for the 1st form to store the results of the 2nd form if it was filled first.

    This way, you can compare the results of submitting 2 forms while using only PHP.

    EDIT: You'll need to place all the database requests before the forms to use this method, and just echo the result in the div later.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错