蓝极冰焰 2024-04-08 17:46 采纳率: 40.7%
浏览 8
已结题

关于PHP中勾选提交到下一个页面和跳转显示的问题

问题一:
在这个页面掉选出来的名单中,我想通过勾选几个名单,加载到下一个页面中,请问这里如何实现?目前出现的情况是:不管我勾选几个,总是会自动选择2人。
问题二:
假设有一个页面A,而这个代码是页面B的,提交以后到页面C,而页面B显示在页面A的

标签中,我希望C页面也能显示在标签中,请问如何修改?


<?php require_once('Connections/games.php'); ?>
<?php
if (!isset($_SESSION)) {
  session_start();
}
$MM_authorizedUsers = "";
$MM_donotCheckaccess = "true";

function isAuthorized($strUsers, $strGroups, $UserName, $UserGroup) { 
  $isValid = False; 

  if (!empty($UserName)) { 

    $arrUsers = Explode(",", $strUsers); 
    $arrGroups = Explode(",", $strGroups); 
    if (in_array($UserName, $arrUsers)) { 
      $isValid = true; 
    } 

    if (in_array($UserGroup, $arrGroups)) { 
      $isValid = true; 
    } 
    if (($strUsers == "") && true) { 
      $isValid = true; 
    } 
  } 
  return $isValid; 
}

$MM_restrictGoTo = "d_dl_false.php";
if (!((isset($_SESSION['MM_Username'])) && (isAuthorized("",$MM_authorizedUsers, $_SESSION['MM_Username'], $_SESSION['MM_UserGroup'])))) {   
  $MM_qsChar = "?";
  $MM_referrer = $_SERVER['PHP_SELF'];
  if (strpos($MM_restrictGoTo, "?")) $MM_qsChar = "&";
  if (isset($_SERVER['QUERY_STRING']) && strlen($_SERVER['QUERY_STRING']) > 0) 
  $MM_referrer .= "?" . $_SERVER['QUERY_STRING'];
  $MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" . urlencode($MM_referrer);
  header("Location: ". $MM_restrictGoTo); 
  exit;
}
?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

mysql_select_db($database_games, $games);
$query_athletes = "SELECT * FROM athletes";
$athletes = mysql_query($query_athletes, $games) or die(mysql_error());
$row_athletes = mysql_fetch_assoc($athletes);
$totalRows_athletes = mysql_num_rows($athletes);

$colname_delegations = "-1";
if (isset($_SESSION['MM_Username'])) {
  $colname_delegations = $_SESSION['MM_Username'];
}
mysql_select_db($database_games, $games);
$query_delegations = sprintf("SELECT * FROM delegations WHERE dname = %s", GetSQLValueString($colname_delegations, "text"));
$delegations = mysql_query($query_delegations, $games) or die(mysql_error());
$row_delegations = mysql_fetch_assoc($delegations);
$totalRows_delegations = mysql_num_rows($delegations);
?>
<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>【报名 】</title>
    <link href="css/bootstrap-4.4.1.css" rel="stylesheet" type="text/css">
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function() {
            $('#submitButton').click(function(event) {
                event.preventDefault(); // 阻止表单的默认提交行为

                // 创建一个空数组来存储被选中的运动员的ID
                var selectedAthletes = [];

                // 遍历所有的复选框
                $('input[type="checkbox"]').each(function() {
                    // 如果复选框被选中,则将其值(运动员ID)添加到数组中
                    if ($(this).is(':checked')) {
                        selectedAthletes.push($(this).val());
                    }
                });

                // 如果至少有一个运动员被选中,则发送AJAX请求
                if (selectedAthletes.length > 0) {
                    $.ajax({
                        url: 'd_center.php', // 替换为实际的服务器端点URL
                        type: 'POST',
                        data: { athletes: selectedAthletes }, // 发送被选中的运动员ID数组
                        success: function(response) {
                            // 如果请求成功,处理服务器的响应
                            // 假设服务器返回的是HTML内容,将其显示在<main>标签中
                            $('main').html(response);
                        },
                        error: function(jqXHR, textStatus, errorThrown) {
                            // 如果请求失败,显示错误消息
                            console.error('AJAX请求失败: ' + textStatus, errorThrown);
                            alert('提交失败,请检查网络或联系管理员。');
                        }
                    });
                } else {
                    // 如果没有选中任何运动员,显示一个提示消息
                    alert('请至少选择一个运动员。');
                }
            });
        });
    </script>
</head>

<body>
    <div class="container-md">
        <table class="table">
            <tr>【 队伍报名表信息确认 】</tr>

            <tr align="center">
                <td>序号</td>
                <td>选择</td>
                <td>姓名</td>
                <td>性别</td>
                <td>年龄</td>
                <td>教练</td>
                <td>出生日期</td>
                <td>信息修改</td>
            </tr>
  <?php        
             //从'delegations'记录集中调取'dname'
            $dname = $row_delegations['dname'];
            if($dname!=' '){
            $maxRowCount = mysql_num_rows($athletes);
            // 根据'dname',从athletes记录集中调取出对应信息
            $athletes = "select * from athletes where dname = '$dname' group by aname order by birthday desc" ;
            $athletes =mysql_query($athletes) or die("查询失败");
            }
            $serial_number = 1;
 ?>
            <?php while ($row = mysql_fetch_assoc($athletes)): ?>
                <tr align="center">
                    <td><?php echo $serial_number; ?></td>
                    <td>
                        <?php echo "<input type='checkbox' name='athletes[]' value='" . $row['arealname'] . "'>"; ?>
                    </td>
                    <td><?php echo $row['arealname']; ?></td>
                    <td><?php echo $row['gender']; ?></td>
                    <td><?php echo $row['age']; ?></td>
                    <td><?php echo $row['coach']; ?></td>
                    <td><?php echo $row['birthday']; ?></td>
                    <td>修改</td>
                  </tr>
                <?php $serial_number++; ?>
            <?php endwhile; ?>
        </table>
            <form action="reg_match_list.php" method="post" id="reg_match_Form">
            <input type="submit" value="添加名单">
        </form>
   </div>
<main class="container" id="content"></main>
</body>
</html>
<?php
mysql_free_result($athletes);
mysql_free_result($delegations);
?>


  • 写回答

16条回答 默认 最新

  • 专家-赤兔[在线] 优质创作者: 编程框架技术领域 2024-04-08 17:46
    关注

    引自免费微信小程序:皆我百晓生

    针对您提供的PHP代码和所描述的问题,我将为您提供详细的解答与解决方案。请按照以下步骤进行调整和修改:

    问题一:勾选名单后总是自动选择2人

    根据您提供的HTML部分,每个运动员对应的勾选框都有相同的name属性值,即name="athletes[]"。在表单提交时,所有被选中的复选框将以数组形式传递给下一个页面。若总是自动选择2人,可能存在以下原因:

    1. 前端JavaScript干扰:您提到存在JavaScript脚本,但未提供这部分代码。请检查是否存在其他脚本或事件处理器影响了复选框的选择状态,导致始终选择2个。

    2. 后端处理逻辑错误:请检查接收并处理提交数据的reg_match_list.php文件,确保其正确解析并处理$_POST['athletes']数组,而不是仅获取前两个元素。

    鉴于您未提供相关JavaScript代码及reg_match_list.php的处理逻辑,此处无法直接定位问题所在。如果您确认前端无额外脚本影响复选框状态,且reg_match_list.php中处理逻辑无误,那么问题可能出在其他未知环节。建议您检查相关代码,或提供更具体的上下文信息以便进一步分析。

    问题二:页面跳转与显示问题

    根据您的描述,您希望在用户点击“添加名单”按钮后,将勾选的运动员名单提交至页面C(假设为reg_match_list.php),同时保持页面A的显示。根据现有代码,表单提交会导致页面跳转至reg_match_list.php。为实现您的需求,您可以考虑以下两种方案:

    方案一:使用AJAX异步提交

    将现有的表单提交方式改为使用AJAX异步提交,这样可以避免页面跳转,同时在页面A上动态显示提交结果。请参考以下修改后的HTML部分:

    <!-- 在<head>标签内添加以下jQuery库引用(已存在可忽略) -->
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    
    <!-- 在</body>标签前添加以下JavaScript代码 -->
    <script>
    $(document).ready(function() {
        $('#submitButton').click(function(event) {
            event.preventDefault(); // 阻止表单的默认提交行为
    
            // 创建一个空数组来存储被选中的运动员的ID
            var selectedAthletes = [];
    
            // 遍历所有的复选框
            $('input[type="checkbox"]').each(function() {
                // 如果复选框被选中,则将其值(运动员ID)添加到数组中
                if ($(this).is(':checked')) {
                    selectedAthletes.push($(this).val());
                }
            });
    
            // 如果至少有一个运动员被选中,则发送AJAX请求
            if (selectedAthletes.length > 0) {
                $.ajax({
                    url: 'reg_match_list.php', // 替换为实际的服务器端点URL
                    type: 'POST',
                    data: { athletes: selectedAthletes }, // 发送被选中的运动员ID数组
                    success: function(response) {
                        // 如果请求成功,处理服务器的响应
                        // 假设服务器返回的是HTML内容,将其显示在<main>标签中
                        $('main').html(response);
                    },
                    error: function(jqXHR, textStatus, errorThrown) {
                        // 如果请求失败,显示错误消息
                        console.error('AJAX请求失败: ' + textStatus, errorThrown);
                        alert('提交失败,请检查网络或联系管理员。');
                    }
                });
            } else {
                // 如果没有选中任何运动员,显示一个提示消息
                alert('请至少选择一个运动员。');
            }
        });
    });
    </script>
    
    <!-- 修改表单按钮ID为'submitButton' -->
    <input type="button" value="添加名单" id="submitButton">
    

    同时,您需要修改reg_match_list.php以适应AJAX请求,返回处理后的运动员名单HTML,而非直接渲染整个页面。

    方案二:保留表单提交,使用iframe展示结果

    如果您希望保留原有的表单提交方式,可以在页面A中嵌入一个隐藏的iframe,用于承载提交后页面C(reg_match_list.php)的显示内容。请参考以下修改后的HTML部分:

    <!-- 在<body>标签内合适位置添加以下iframe -->
    <iframe id="resultFrame" style="display:none;"></iframe>
    
    <!-- 修改表单提交目标为iframe -->
    <form action="reg_match_list.php" method="post" target="resultFrame" id="reg_match_Form">
        <input type="submit" value="添加名单">
    </form>
    
    <!-- 添加一个按钮,用于显示iframe内容 -->
    <button onclick="document.getElementById('resultFrame').style.display = 'block';">显示结果</button>
    

    这样,当用户点击“添加名单”按钮时,表单数据会被提交至reg_match_list.php,并显示在隐藏的iframe中。用户点击“显示结果”按钮后,iframe内容会显示在页面A上。

    请您根据实际情况选择合适的方案进行调整。如果您对上述解答有任何疑问或需要进一步协助,请随时告知。

    评论 编辑记录

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 4月12日
  • 创建了问题 4月8日