【发布时间】:2021-11-13 07:38:52
【问题描述】:
我有一个名为 SQLFailover.php 的页面,其中显示了一个包含两行的表。每行都有一个切换按钮。第一行上的切换按钮将设置为主要,并且该行上的服务器名称将由代码中之前完成的 SQL 查询确定。第二行的切换将设置为辅助,并将从同一 SQL 查询中获取服务器名称。当任一按钮被切换时,我需要该按钮触发对 SQLAction.php 的发布。该页面将调用一个 powershell 脚本,该脚本将更新表中的服务器名称,然后使用 header() cmd 重定向回 SQLFailover.php。当重新加载 SQLFailover.php 时,带有注释“从 SQL 表中获取当前主服务器和辅助服务器”的部分将执行一个查询,检索要显示的主服务器和辅助服务器,此时它们将从稍后将添加到 SQLAction.php 页面的 powershell。我遇到的麻烦是,当我当前的脚本发布到操作页面时,键 => 值对是未定义的。请帮助我了解这是如何发生的并更正我的代码。谢谢。
SQLFailover.php
<link href="https://gitcdn.github.io/bootstrap-toggle/2.2.2/css/bootstrap-toggle.min.css" rel="stylesheet">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<div class="portlet-body">
<div class="table-scrollable">
<table class="table table-hover">
<thead>
<tr>
<th class="all"> Location </th>
<th class="all"> Status </th>
</tr>
</thead>
<tbody>
<?php $UserSession = $_SESSION['jigowatt']['username'];?>
<script> var UserSession = '<?=$UserSession?>';</script>
<?php
$env = "CERT";
if (isset($env)) {
if ($env === 'CERT') {
$stageEnvironmentType = "CERT";
$SQLFileName = "AGL_C_ 01\C01,10995";
$serverName = $SQLFileName;
}elseif($env === 'PROD') {
$stageEnvironmentType = "PROD";
$SQLFileName = "AGL_P_ 01\P01,11111";
$serverName = $SQLFileName;
}
//SQLServer Connection
$connectionInfo = array( "Database"=>"master");
$conn = sqlsrv_connect( $serverName, $connectionInfo );
if( $conn === false ) {
die( print_r( sqlsrv_errors(), true));
}else{
//get current primary server and secondary server from SQL Table
$sql = file_get_contents('SQLSelect.sql');
$stmt = sqlsrv_query( $conn, $sql);
if( $stmt === false ) {
die( print_r( sqlsrv_errors(), true));
}
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
$conn_current_PrimaryReplica = $row['conn_current_PrimaryReplica'];
$conn_new_PrimaryReplica = $row['conn_new_PrimaryReplica'];
?>
<tr>
<td> <?php echo $conn_current_PrimaryReplica;?> </td>
<td>
<input data-server-name="<?php echo "$conn_new_PrimaryReplica";?>" class="toggle-event" checked type="checkbox" data-toggle="toggle" data-on="Primary" data-onstyle="success" data-off="Secondary" data-offstyle="danger" data-size="small" />
</td>
</tr>
<tr>
<td> <?php echo $conn_new_PrimaryReplica; ?> </td>
<td>
<input data-server-name="<?php echo "$conn_current_PrimaryReplica";?>" class="toggle-event" type="checkbox" data-toggle="toggle" data-on="Primary" data-onstyle="success" data-off="Secondary" data-offstyle="danger" data-size="small" />
</td>
</tr>
<?php
}
sqlsrv_free_stmt($stmt);
}
}
?>
</tbody>
</table>
</div>
</div>
<script>
$(function() {
$('.toggle-event').change(function() {
var serverName = $(this).data('server-name');
var mode = $(this).prop('checked');
var username = '<?=$UserSession?>';
var payload = {
server: serverName,
username: username
};
var form = document.createElement('form');
form.style.visibility = 'hidden';
form.method = 'POST';
form.action = '/SQLAction.php';
for (key in Object.keys(payload)) {
var input = document.createElement('input');
input.name = key;
input.value = payload[key];
form.appendChild(input);
}
document.body.appendChild(form);
form.submit();
})
})
</script>
下面是 SQLAction.php 页面。
<?php
echo "<b>begin POST values</b><br><br>";
foreach ($_POST as $key => $value) {
echo "<tr>";
echo "<td>";
echo $key;
echo "</td>";
echo " : ";
echo "<td>";
echo $value;
echo "</td>";
echo "</tr>";
echo "<br>";
}
echo "<b>end of POST values</b><br><br>";
header('refresh:5; / SQLFailover.php ');
?>
【问题讨论】:
标签: javascript php jquery post