gpt4 book ai didi

javascript - 从 JS 向 PHP 传递值

转载 作者:行者123 更新时间:2023-12-01 01:26:04 25 4
gpt4 key购买 nike

将表格单元格值从 javascript 变量传递到 php 变量。

<script>
$(document).on('ready',function(){
$('#business_list_table').on('click','.view_btn',function (){
$.ajax({
url: "test.php",
method: "POST",
data:{business_id : "6510-1"},
success: function (data){
$('#business_permit_table').html(data);
}
});
});
});

<?php
$business_id = $_GET["business_id"];
echo $business_id;

最佳答案

你不能像这样直接在 PHP 中使用 JS 变量。使用ajax代替:

JS

$("#business_list_table").on('click', '.view_btn', function post() {
// get the current row
var currentRow = $(this).closest("tr");
var Business_id_value= currentRow.find("td:eq(1)").text(); // get current row 2nd T;

$.post('', {Business_ID: Business_id_value}, function(result){
$('table tbody').html(result);
});

});

PHP

if (isset($_POST['Business_ID'])) {
$Business_ID = $_POST['Business_ID'];

$conn = mysqli_connect("localhost", "root", "", "bpsystem");
if ($conn->connect_error) {
die("Database connection failed:" . $conn->connect_error);
} else {
$sql = "SELECT * FROM business_tb WHERE Business_ID='$Business_ID';";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while ($row = $result->fetch_assoc()) {
echo "<tr >";
echo "<td>BUSINESS NAME</td>";
echo "<td>" . $row['Business_Name'] . "</td>";
echo "</tr>";
echo "<tr >";
echo "</tr>";
}
}
}
}

关于javascript - 从 JS 向 PHP 传递值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53773202/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com