首页 > 解决方案 > 如何在js中按ID搜索

问题描述

我正在尝试从 dataTable 中选择行,但是 PHP 下的 js 代码出现问题。下面给出了代码。这里首先我从数据库中获取数据,然后将它们添加到 dataTable 中。代码工作完美,直到这里..但是当我尝试从表中选择任何行时,它会显示错误提示

解析错误:语法错误,第 43 行 C:\xampp\htdocs\Trying\fetch.php 中的意外 'getval' (T_STRING)

var table = document.getElementById('getval');几乎在代码的末尾。

`

    <?php
            $connect = mysqli_connect("localhost","root","","ajmal");
            $output = '';

            $sql = "SELECT                                              
            medicinName,pricerPerSheet,dealerID,availAbleAt,district,place 
            FROM medicinalinfo WHERE medicinName LIKE 
            '%".$_POST["search"]."%'";
            $result = mysqli_query($connect,$sql);


            if(mysqli_num_rows($result) > 0)
            {
                $output .= '<h4 align="center" class="h4_search">Search 
                Result</h4>';
                $output .= '<div class="row">';
                $output .= '<div class="col-md-8 col-md-offset-1 well">';
                $output .= '<div class="table-responsive">
                    <table id="tbl" class="table table-bordered table- 
                    striped table-hover">
                        <tr>
                            <th>Medicin Name</th>
                            <th>Price Per Sheet</th>
                            <th>Availble At</th>
                            <th>District</th>
                            <th>Area</th>
                        </tr>';
               $output .= '</div>';
               $output .= '</div>';
               $output .= '</div>';
               while ($row = mysqli_fetch_array($result)) {
               $output .= '
               <tbody id="getval">
                    <tr>
                        <td>'.$row['medicinName'].'</td>
                        <td>'.$row['pricerPerSheet'].'</td>
                        <td>'.$row['availAbleAt'].'</td>
                        <td>'.$row['district'].'</td>
                        <td>'.$row['place'].'</td>
                    </tr>
               </tbody>
             ';
             }
             $output.='</table>';

             $output.='<script>

             var table = document.getElementById('getval');

             for(var i=0; i<table.rows.length; i++){
                table.row[i].onclick = function(){
                    alert(this.cells[0].innerHTML);
                };
             }

             </script>
          ';
          echo $output;
          }
        else
        {
           echo '<h4 align="center" class="h4_search">Data Not Found</h4>';
        }


     ?>

`

标签: javascriptphp

解决方案


您正在为该字符串使用单引号,因此您必须在该 JS 位中转义它们,或更改为双引号。

因此,该行应为:

var table = document.getElementById(\'getval\');

或者

var table = document.getElementById("getval");

编辑: 好的,这是您的整个代码,已更正,您有几个错误:

<?php

$connect = mysqli_connect("localhost", "root", "", "ajmal");
$output = '';

$sql = "SELECT                                              
            medicinName,pricerPerSheet,dealerID,availAbleAt,district,place 
            FROM medicinalinfo WHERE medicinName LIKE 
            '%" . $_POST["search"] . "%'";
$result = mysqli_query($connect, $sql);


if (mysqli_num_rows($result) > 0) {
    $output .= '<h4 align="center" class="h4_search">Search
            Result</h4>';
    $output .= '<div class="row">';
    $output .= '<div class="col-md-8 col-md-offset-1 well">';
    $output .= '<div class="table-responsive">
                <table id="tbl" class="table table-bordered table-
                striped table-hover">
                    <tr>
                        <th>Medicin Name</th>
                        <th>Price Per Sheet</th>
                        <th>Availble At</th>
                        <th>District</th>
                        <th>Area</th>
                    </tr>
                    <tbody id="getval">';
    while ($row = mysqli_fetch_array($result)) {
        $output .= '
                    <tr>
                        <td>' . $row['medicinName'] . '</td>
                        <td>' . $row['pricerPerSheet'] . '</td>
                        <td>' . $row['availAbleAt'] . '</td>
                        <td>' . $row['district'] . '</td>
                        <td>' . $row['place'] . '</td>
                    </tr>';
    }
    $output .= '</tbody></table>';
    $output .= '</div>';
    $output .= '</div>';
    $output .= '</div>';
    $output .= '<script>
             var table = document.getElementById("getval");
             for(var i=0; i<table.rows.length; i++){
                table.rows[i].onclick = function(){
                    alert(this.cells[0].innerHTML);
                };
             }

             </script>
          ';
    echo $output;
} else {
    echo '<h4 align="center" class="h4_search">Data Not Found</h4>';
}
?>

首先,您在表格中关闭了 div,这是错误的,我在您的代码中更正了这一点。然后,您有多个具有相同 id 的 tbody 元素,这就是造成混乱的原因。将 tbody 段移到 mysqli_fetch_array 循环之外,因此我们只有一个 tbody 和多行。此外,在 javascript 部分,它应该是

table.rows[i].onclick

并不是

table.row[i].onclick

推荐阅读