我正在尝试从dataTable中选择行,但是在PHP下的js代码中出现了问题。下面给出了。下面我首先从数据库中获取数据,然后将它们添加到dataTable中。代码完美地工作到此处..但是当我试图从表格中选择任何一行时,它会显示错误说明
解析错误:语法错误,意外' getval' (T_STRING)在第43行的C:\ xampp \ htdocs \ Trying \ fetch.php
,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>';
}
?>
`
答案 0 :(得分:-2)
您正在为该字符串使用单引号,因此您必须在该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元素,这就是造成混乱的原因。在mysqli_fetch_array循环之外移动了tbody段,因此我们只有一个tbody和多个行。此外,在javascript部分,它应该是
table.rows[i].onclick
而不是
table.row[i].onclick