Php 单击链接后如何刷新div的内容

Php 单击链接后如何刷新div的内容,php,jquery,ajax,hyperlink,html-table,Php,Jquery,Ajax,Hyperlink,Html Table,我有一个表单,其中包含一个下拉框,当值更改时,表单使用jQuery和ajax生成一个表。该表包含基于下拉框值的数据以及链接 我的初始脚本(即下拉框所在的位置和添加表的位置)是: <?php require_once('/connections/db.php'); mysql_select_db($database_db, $db); $query_weeks = "SELECT WeekNo, WeekName FROM tbl_weeks ORDER BY WeekNo ASC"; $w

我有一个表单,其中包含一个下拉框,当值更改时,表单使用jQuery和ajax生成一个表。该表包含基于下拉框值的数据以及链接

我的初始脚本(即下拉框所在的位置和添加表的位置)是:

<?php require_once('/connections/db.php');
mysql_select_db($database_db, $db);
$query_weeks = "SELECT WeekNo, WeekName FROM tbl_weeks ORDER BY WeekNo ASC";
$weeks = mysql_query($query_weeks, $db) or die(mysql_error());
$row_weeks = mysql_fetch_assoc($weeks);
$totalRows_weeks = mysql_num_rows($weeks);
 ?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>


</head>

<body>


        <form name="form1" method="post" action="">
        <select id="weekselect" name="week">
          <?php
        do {  
        ?>
          <option value="<?php echo $row_weeks['WeekNo']?>"><?php echo $row_weeks['WeekName']?></option>
          <?php
        } while ($row_weeks = mysql_fetch_assoc($weeks));
          $rows = mysql_num_rows($weeks);
          if($rows > 0) {
              mysql_data_seek($weeks, 0);
              $row_weeks = mysql_fetch_assoc($weeks);
          }
        ?>
        </select>
        </form>


<div id="mydata"></div>


<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function (){



    // listen to change in monthselect
    $('#weekselect').change(function(){

        var week= $('#weekselect option:selected').val();

        //pass val to php
        $.post('test2.php',{week: week}, function(data) {
            //do something with the data
            $('div#mydata').html(data);
        });         
    });


});
</script>
</body>

</html>
该脚本从mysql数据库生成下拉列表,并为其创建一个侦听器,当更改select时,select的值将使用jquery传递给test2.php。返回的数据被添加到div mydata中

生成该表的Test2如下所示

<?php


if (!isset($_SESSION)) {session_start();}
include('session.php');
require_once('Connections/dbc.php');
$open_week=$_SESSION['MM_OpenWeek'];
$week_selected=$_POST['week'];
$season=$_SESSION['MM_Season'];
?>


<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>
<?php
echo '<p>Week selected: '.$week_selected .'<p>';
echo '<p>Open Week: '.$open_week .'<p>';
if ($week_selected>=$open_week) {
    echo '<p>Week Open<p>';
} else {
    echo '<p>Week Closed<p>';
}

//create recordset of fixtures for the week selected
$sql="SELECT * FROM q_games_stats WHERE WeekNo =".$week_selected . " and Season=". $season ." and at='Home' ORDER BY WeekNo ASC";
$rec_games=mysqli_query($dbc,$sql);
//create table using the recordset
?>

<div id="data_table">

        <table class="table" align="center">
        <th class="th" width="80px">Match Date</th>
        <th class="th" width="150px">Home Team</th>
        <th class="th" width="40px">Score</th>
        <th class="th" width="150px">Away Team</th>
        <th class="th" width="150px">Link</th>

<?php
    while ($row=mysqli_fetch_array($rec_games)) 
    {
?>
        <tr class="tr">
        <td class="td"><?php echo $row['MatchDate']?></td>
        <td class="td"><?php echo $row['TeamName']?></td>
        <td class="td"><?php echo $row['Score']?></td>
        <td class="td"><?php echo $row['OppName']?></td>
        <td class="td"><a href="#" target="_top">Link</a></td>
        </tr>
<?php
    }
?>



</table>
</div>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function (){



    // listen to change in monthselect
    $('#data_table a').click(function(){
            alert("link clicked");

    });


});
</script>
</body>
</html>
这将生成包含链接的表。表中有一个链接的事件侦听器,当我单击它时,它会生成一个警报

我想做的不是在单击链接时生成警报,而是让表重新生成,原因是当用户单击链接时,它将运行一个脚本,该脚本将更改表中的基础数据,因此需要刷新表


有没有更好的方法可以做到这一点,或者有没有更好的方法可以做到我想做的事情?

请您详细介绍一下您的代码真的很混乱,我不明白您到底在哪里遇到了问题,您可以强调一下吗。我遇到的问题是,我希望在第二个脚本上创建的表在单击其中的链接时能够重现。其他一切都很好。我在test2.php底部的表中的链接中添加了一个事件侦听器,这将捕获事件并生成警报。但是我不想要警报,我想要重新绘制表并传递回div test1.php