Php 使用Ajax在单击时获取行号

Php 使用Ajax在单击时获取行号,php,ajax,Php,Ajax,我已经使用了下面的代码和它的工作原理。但我希望在单击行时使用行号希望Ajax可以,并将其传递给同一页面中的php代码。我试过javascript,但效果并不理想。如果是在Ajax,那就更好了。任何帮助都将不胜感激: if(isset($_POST['search'])){ $search=mysqli_query($con,"SELECT * FROM bus_fares WHERE route_no='$_POST[route_no]'"); $num_rows = mysqli_num_ro

我已经使用了下面的代码和它的工作原理。但我希望在单击行时使用行号希望Ajax可以,并将其传递给同一页面中的php代码。我试过javascript,但效果并不理想。如果是在Ajax,那就更好了。任何帮助都将不胜感激:

if(isset($_POST['search'])){
$search=mysqli_query($con,"SELECT * FROM bus_fares WHERE route_no='$_POST[route_no]'");
$num_rows = mysqli_num_rows($search);
$search1=mysqli_query($con,"SELECT fare FROM fare  limit $num_rows ");
$x = 0;
echo" <table  id='my_table'><tr><th>Fare Stage</th>
            <th>Location</th>
            <th>Fare</th>
        </tr>";

while($row=mysqli_fetch_assoc($search) and $row1=mysqli_fetch_assoc($search1)){
echo"<tr>";
echo"<td>".$x."</td>";
echo"<td>".$row['location']."</td>";
echo"<td>".$row1['fare']."</td>";
echo"</tr>";
$x++;
}
echo"</table>"; 
}
而不是回声;做:


您真正想要的不仅是在表中存储可视数据,还存储某种元数据。有几种方法可以实现这一点

方法1:滥用id或类属性:

生成的HTML如下所示:

<!-- example with the id-attribute: -->
<tr id="mysql_row_1"> ... </tr>
<tr id="mysql_row_2"> ... </tr>

<!-- example with the class-attribute: -->
<tr class="mysql_row_1"> ... </tr>
<tr class="mysql_row_2"> ... </tr>
<tr data-mysql_row_number="1" onclick="getRowNumber()"> .... </tr>
<tr data-mysql_row_number="2" onclick="getRowNumber()"> .... </tr>
javascript部分:

function getRowNumber() {
    var rowNumber = this.className.replace("mysql_row_",""); // fetches the classname and removes the extra-strings
    alert(rowNumber);  // alerts "1", "2", ...
} 
方法2:使用数据-*属性:

此解决方案适用于HTML5。请添加有关兼容性的其他信息(如果有)

您的HTML代码如下所示:

<!-- example with the id-attribute: -->
<tr id="mysql_row_1"> ... </tr>
<tr id="mysql_row_2"> ... </tr>

<!-- example with the class-attribute: -->
<tr class="mysql_row_1"> ... </tr>
<tr class="mysql_row_2"> ... </tr>
<tr data-mysql_row_number="1" onclick="getRowNumber()"> .... </tr>
<tr data-mysql_row_number="2" onclick="getRowNumber()"> .... </tr>
这还可以生成完全有效的HTML5代码,并允许您存储无限量的信息,因为您可以指定任意数量的data-*属性

方法3:使用不可见字段:

生成的HTML代码:

<tr onclick="getRowNumber()">
    <td>
        Normal content of this field
        <input type="hidden" name="mysql_row_number" value="1"/>
    </td>
</tr>
这也会产生有效的HTML,但在我看来,在语义上并不真正正确,因为字段中的数据有些松散,并且没有直接连接到行。此外,您还可以使用相同的名称创建多个字段

我建议使用方法2 data-*,因为这是最灵活的解决方案,并且使用了一个设计用于存储元数据的属性。 方法1在所有旧浏览器中都是最可靠的,因为它们都支持通过JS访问id或class属性,只要您保持id标记的唯一性。 方法3对于较旧的浏览器也非常可靠。
那么您的实际问题是?…您仍然需要PHP来检索MySQL值。你可以使用jQuery在点击时显示它们谢谢你们的回复…我在这里有点迷路了。如果你能给我这个函数的确切代码。。只是想在php codenp中获得一个变量的onclick行号,数据属性是一个不错的主意,但跨浏览器的兼容性较差;不客气。你可能想投票支持我的答案,给我一点口碑提升:
function getRowNumber() {
    alert(this.getAttribute("data-mysql_row_number")); // alerts "1", "2", ...
} 
<tr onclick="getRowNumber()">
    <td>
        Normal content of this field
        <input type="hidden" name="mysql_row_number" value="1"/>
    </td>
</tr>
function getRowNumber() {
    var rowNumber = this.getElementsByName('mysql_row_number')[0].value;
    alert(rowNumber); // alerts "1", "2", ...
}