简体   繁体   English

通过从 javascript 文件查询数据库,根据下拉菜单中的选择更新 div 内容

[英]Updating div content based on a selection from a drop down menu by querying a database from a javascript file

I had a question answered earlier here that was a massive help that involved changing content using ajax based on a dropdown selection from a mysql database.我之前在这里回答了一个问题,这是一个巨大的帮助,涉及根据 mysql 数据库中的下拉选择使用 ajax 更改内容。

I now need to fill the contents of the editted div with the values from the same row as the selected option from the drop down menu.我现在需要使用与从下拉菜单中选择的选项相同的行中的值填充编辑过的 div 的内容。

In order to do this it seems that the only way is to query from my js file, and the only ways to do this is through either nodejs or further ajax.为了做到这一点,似乎唯一的方法是从我的 js 文件中查询,而唯一的方法是通过 nodejs 或进一步的 ajax。 I have attempted both ways but nodejs is proving tough as I am using cpanel to host the site.我已经尝试了两种方法,但是当我使用 cpanel 来托管站点时,nodejs 变得很困难。 Ajax would be a great way to do this but I need the query to include WHERE cModel = '$cModel' in order to retrieve the correct values and obviously I can't get that variable when im getting the php script from a separate file. Ajax 将是一个很好的方法来做到这一点,但我需要查询包含WHERE cModel = '$cModel' 以检索正确的值,显然当我从单独的文件中获取 php 脚本时我无法获取该变量。

I apologise if i've left out some important details, this has been doing my head in massively and i wanted to see if there was any easier way to do this.如果我遗漏了一些重要的细节,我深表歉意,这一直在我脑海中挥之不去,我想看看是否有更简单的方法来做到这一点。 Thanks!谢谢!

HTML & PHP for one dropdown box:一个下拉框的HTML 和 PHP

<form id="parts">
        <fieldset>
            <legend>Choose your parts</legend>
            Any parts marked with * are required<br/><br/>
            <label for="CPU">CPU*</label><br/>
            <?php
                $cresult = $mysqli->query("SELECT * FROM pCpu ORDER BY cModel asc");
            ?>
                <select id="CPU" name="CPU" onchange="myUpdateFunc()">
                <option value="" disabled selected>Select your Part</option>
                <?php
                while ($rows = $cresult->fetch_assoc()) {
                    $cmodel = $rows['cModel'];
                    echo "<option value='$cmodel'>$cmodel</option>";

                }

                ?>
                </select>

                <br/>

                <div id="divResults">

                </div>
                <br/><br/>

JS : JS

function myUpdateFunc()
{
    var mySelected = $("#CPU").find("option:selected").val();

    $('#divResults').html ('CPU Model: ' + mySelected + '<br/> CPU Socket: ')
}

AJAX is definitely the way to go. AJAX 绝对是要走的路。 Call a PHP script and pass it cModel .调用一个 PHP 脚本并将其传递给cModel

function myUpdateFunc()
{
  var mySelected = $("#CPU").find("option:selected").val();
  $.ajax({
    url: "searchCPU.php?cmodel=" + mySelected, 
    success: function(result){
    $('#divResults').html ('CPU Model: ' + result + '<br/> CPU Socket: ')
     }
  });
}

searchCPU.php搜索CPU.php

<?php
    $cmodelParam = $_GET['cmodel'];
    $cresult = $mysqli->query("SELECT * FROM pCpu WHERE cModel = $cmodelParam");
    //assuming it returns a single row for simplicity
    $row = mysqli_fetch_assoc($cresult);
    echo $row['cModel'];
?>

Obviously you can do more error checking and this is open to SQL injection .显然你可以做更多的错误检查,这对SQL 注入是开放的。 You should always use prepared statements .您应该始终使用准备好的语句 I didn't alter it to simply answer your question and let you resolve those issues on your own.我没有改变它来简单地回答你的问题,让你自己解决这些问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM