我知道这个问题已经被问过并且回答了很多次但是对于我的生活我无法得到解决方案来处理我的代码。我相信如果我重新编写我的所有代码,我最终会让它正常工作,回想起我在这个问题上花费的时间,我应该重做所有代码...但我现在很好超出我的截止日期,只需要一个工作原型...
有人可以建议如何在不重新加载页面的情况下更新单个页面上的多个表单吗?我很擅长PHP,并且在BEST中使用JS / JQuery / AJAX完成noob。我的html / php在一个文件中,我相信这是导致我最多的问题/是让我的AJAX表单处理工作的最大障碍。
基本上,我希望用户:
从表单组1(联赛冠军)中的多个下拉列表中选择团队,并在更新时使用新选择的团队更新我的数据库,而无需重新加载页面。
编辑每场比赛的球队得分和得分提交/更新,用新得分更新数据库。
对于每个要求,有多种形式(每个匹配1个,每个匹配1个)所以我在每个表单上设置了一个类以及一个id。
所有内容都可以通过PHP工作,但是每次提交内容时表单提交都会重新加载页面,这并不理想。
我已经尝试过来自http://scotch.io/tutorials/javascript/submitting-ajax-forms-with-jquery的AJAX代码以及来自SO的众多解决方案,但没有运气。我不知道如何编辑代码以适应我的情况。
我不确定哪些代码可以帮助有人回答此问题,因为我的所有代码都在一个页面中,并且在此帖子中复制/粘贴可能太长。我将提供任何有需要的信息/数据来帮助我。我在交付后7周(这对我和朋友来说是一个个人项目,所以没有太大的交易......但是真的想要完成这个)。
好的,这是我的代码。它非常糟糕,我正在重构它以利用功能和更好的查询等。但这就是我目前所希望的,并且希望能够使用#34; work"在我重新设计之前......(我希望根据我的问题更新的部分在评论中注释了STACKOVERFLOW)。希望这有助于有人帮助我,因为我仍处于僵局。
<?php
// includes and session data here
// Connect to DB
// get current date and time
$cur_time = date('Y-m-d H:i:s');
require_once('current_week.php'); // this file contains $cur_week and $cur_date vars
// Grab the match id for each match to use for userpicks check/populate
$query_matchids = "SELECT id AS match_id FROM matches ORDER BY match_time";
$data_matchids = mysqli_query($dbc, $query_matchids)
or die('Error querying matches table for the match ids.');
$match_ids = array();
while ($row_matchids = mysqli_fetch_array($data_matchids)) {
array_push($match_ids, $row_matchids['match_id']);
}
// Check if USERPICKS exist
$query_userpicks = "SELECT user_id AS userpicks_userid, match_id AS userpicks_matchid, team_id_winner AS userpicks_winnerid, " .
"home_score AS userpicks_hscore, away_score AS userpicks_ascore " .
"FROM user_picks " .
"WHERE user_id = $user_id";
$data_userpicks = mysqli_query($dbc, $query_userpicks)
or die('Error querying user_picks table for user picks data.');
if (mysqli_num_rows($data_userpicks) == 0) {
// If no userpicks exist for user, populate users picks' list with null data for each match
foreach ($match_ids AS $match_id) {
$query_userpicks_initial = "INSERT INTO user_picks (user_id, match_id) " .
"VALUES ($user_id, $match_id)";
mysqli_query($dbc, $query_userpicks_initial)
or die('Error querying user_picks table to insert empty userpicks data.');
}
}
// if userpicks do exist for user, put data into an array to be used later
$userpicks = array();
while ($row_userpicks = mysqli_fetch_assoc($data_userpicks)) {
array_push($userpicks, $row_userpicks);
}
// Grab Week data from SEASON_WEEKS table to display matches and week views for pagination
$query_weeks = "SELECT week_number AS week, start_date AS week_start, end_date AS week_end FROM season_weeks ORDER BY start_date";
$data_weeks = mysqli_query($dbc, $query_weeks)
or die('Error querying season_weeks table for week data.');
$weeks = array();
while ($row_weeks = mysqli_fetch_assoc($data_weeks)) {
array_push($weeks, $row_weeks);
}
// total number of weeks in season...this SHOULD be total number of rows in season column...edit this to something more accurate if not
$num_weeks = mysqli_num_rows($data_weeks);
// Grab League Data
$query_leagues = "SELECT * FROM leagues";
$data_leagues = mysqli_query($dbc, $query_leagues)
or die('Error querying leagues table.');
$leagues = array();
while ($row_leagues = mysqli_fetch_assoc($data_leagues)) {
array_push($leagues, $row_leagues);
}
// Grab MATCH and RESULTS data to generate the Matches and Results
$query_matches = "SELECT m.id, m.match_time, m.home_team_id, m.away_team_id, m.league_id, " .
"l.name AS league_name, l.league_abbr, l.start_date AS league_start, l.end_date AS league_end, l.img_lrg AS league_crestlg, l.img_icon AS league_crestsm, " .
"h.name AS home_team, h.team_abbr AS home_abbr, h.stadium, h.img_lrg AS home_crestlg, h.img_icon AS home_crestsm, h.website AS home_url, " .
"a.name AS away_team, a.team_abbr AS away_abbr, a.img_lrg AS away_crestlg, a.img_icon AS away_crestsm, a.website AS away_url, " .
"mr.result_home, mr.home_score, mr.result_away, mr.away_score, mr.pks, mr.pks_home, mr.pks_away, " .
"up.match_id AS user_matchid, up.team_id_winner AS user_winnerid, up.home_score AS user_hscore, up.away_score AS user_ascore " .
"FROM matches AS m " .
"JOIN leagues AS l " .
"ON m.league_id = l.id " .
"JOIN teams AS h " .
"ON m.home_team_id = h.id " .
"JOIN teams AS a " .
"ON m.away_team_id = a.id " .
"LEFT JOIN match_results AS mr " .
"ON m.id = mr.match_id " .
"LEFT JOIN user_picks AS up " .
"ON m.id = up.match_id AND up.user_id = '$user_id' " .
"ORDER BY league_id, match_time";
$data_matches = mysqli_query($dbc, $query_matches)
or die('Error querying matches, teams, and match_results tables for match data.');
$matches = array();
while ($row_matches = mysqli_fetch_assoc($data_matches)) {
array_push($matches, $row_matches);
}
// Grab points data from ace_points
?>
<section class="container">
<div class="row">
<h1 class="text-center col-xs-12"><?php echo $page_main_header; ?></h1>
</div>
<div class="row">
<div class="col-xs-12">
<div class="container champions_container">
<div class="row">
<button type="button" id="league_champs_button" class="btn btn-primary btn-lg col-md-6 col-md-offset-3 col-sm-8 col-sm-offset-2 col-xs-12" data-toggle="collapse" data-target="#champs_selections_container">Select League Champions</button>
</div>
<div class="row">
<div id="champs_selections_container" class="collapse">
<?php
// Grab Champions Pick Data for each League per user
foreach ($leagues AS $league) {
// grab teams for this league
$query_teams = "SELECT id, name FROM teams WHERE league_id = '" . $league['id'] . "'";
$data_teams = mysqli_query($dbc, $query_teams)
or die('Error querying teams table for team data.');
$teams = array();
while ($row_teams = mysqli_fetch_assoc($data_teams)) {
array_push($teams, $row_teams);
}
// Check Champion Picks to see if a champion has been selected
$query_champions = "SELECT league_id, team_id, pts_value FROM champ_picks WHERE user_id = $user_id AND league_id = '" . $league['id'] . "'";
$data_champions = mysqli_query($dbc, $query_champions)
or die('Error querying champ_picks table for Chamions data.');
$champions = array();
while ($row_champions = mysqli_fetch_assoc($data_champions)) {
array_push($champions, $row_champions);
}
$champ_points = ($champ_orig_points - ($cur_week * 2));
// if a champion exists in user's champ_picks, set variables accordingly
if (mysqli_num_rows($data_champions) == 1) {
$user_champ = $champions[0]['team_id'];
if ($champions[0]['pts_value'] === NULL) {
$user_champ_pts = 0;
}
else {
$user_champ_pts = $champions[0]['pts_value'];
}
}
else {
// No Champion selected for this league yet
$user_champ = '';
$user_champ_pts = 0;
}
// TODO: if CHAMP PICKS change, UPDATE edited selections only - via AJAX
/* STACKOVERFLOW - CHAMPIONS */
// if user submits form UPDATE champ_picks table with new champion selection(s)
if (isset($_POST['champs_submit_league' . $league['id']])) {
$league_id = $_POST['leagueid'][0];
$updated_champs = $_POST['champions'][0];
$updated_champs_pts = $champ_points;
$query_champs_update = "UPDATE champ_picks SET team_id = '$updated_champs', pts_value = '$updated_champs_pts' WHERE league_id = '$league_id'";
mysqli_query($dbc, $query_champs_update)
or die('Error querying champs_picks to update champions for ' . $league['name'] . '.');
}
else {
//echo 'Post not set for Champions ' . $league['id'] . '<br />';
}
?>
<form method="POST" action="<?php $_SERVER['PHP_SELF']; ?>" id="champ_picks_league<?php echo $league['id']; ?>" class="col-xs-12">
<div class="form-group col-md-6 col-md-offset-3 col-sm-8 col-sm-offset-2 col-xs-12">
<label class="sr-only" for="mousetrap_champs<?php echo $league['id']; ?>">Leave Blank <span class="required">(required to prove you are human)</span></label>
<input class="sr-only form-control" type="text" id="mouestrap_champs<?php echo $league['id']; ?>" name="mousetrap_champs<?php echo $league['id']; ?>" value="<?php if(!empty($spam_protect)) echo $spam_protect; ?>" placeholder="Leave blank" />
</div>
<div class="form-group col-md-6 col-md-offset-3 col-sm-8 col-sm-offset-2 col-xs-12">
<input type="hidden" id="leagueid<?php echo $league['id']; ?>" name="leagueid[]" value="<?php echo $league['id']; ?>" />
<label class="sr-only" for="champions"><?php echo $league['name']; ?> Champs</label>
<div class="input-group" style="position: relative;">
<select class="form-control" id="champions<?php echo $league['id']?>" name="champions[]">
<?php echo 'Champs: ' . $user_champ; ?>
<option value="" <?php if (empty($user_champ)) echo 'selected="selected"'; ?>><?php echo $league['name']; ?> Champs</option>
<?php
foreach ($teams as $team) {
// FIXIT: if POST is set per league pick, show newly updated champion team selection -current code isnt working to do this
// if (isset($_POST) && $updated_champs == $team['id']) {
// $select_option = 'selected="selected"';
// echo '<option value="' . $team['id'] . '" ' . $select_option . '>' . $team['name'] . ' (current pick)</option>';
// }
if (!empty($user_champ) && $user_champ == $team['id']) {
$select_option = 'selected="selected"';
echo '<option class="current_userpick" value="' . $team['id'] . '" ' . $select_option . '>' . $team['name'] . ' (current pick)</option>';
}
else {
$select_option = '';
echo '<option value="' . $team['id'] . '" ' . $select_option . '>' . $team['name'] . '</option>';
}
}
?>
</select>
<span class="input-group-addon champ_points_display"><?php echo $champ_points; ?> (<?php echo $user_champ_pts; ?>)</span>
<?php
// only show UPDATE button if user has agreed to RULES already
if ($_SESSION['user_rules'] == 'Y') {
?>
<input type="submit" id="champs_submit_league<?php echo $league['id']; ?>" class="btn btn-primary btn-lrg btn-block form-control input-group-addon" name="champs_submit_league<?php echo $league['id']; ?>" value="Update" />
<?php
}
?>
<!-- TODO: move this to a seperate file, disable UPDATE button if user re-selects original USERPICK champ -->
<script type="text/javascript">
$(document).ready(function() {
$("#league_champs_button").click(function() {
$("input#champs_submit_league<?php echo $league['id']; ?>").css("display", "none");
});
$("#champions<?php echo $league['id']?>").change(function() {
$("input#champs_submit_league<?php echo $league['id']; ?>").fadeIn(1000);
});
});
</script>
</div>
</div>
</form>
<?php
}
?>
</div>
</div>
</div>
<!-- Week Pagination - only show if have more than 1 week of matches-->
<?php
if ($num_weeks > 1) {
?>
<div class="container">
<div class="row">
<div class="col-xs-12 text-center">
<ul class="pagination">
<!-- pagination goes here -->
</ul>
</div>
</div>
</div>
<?php
}
// loop through each week of season(s)
foreach ($weeks as $week) {
if (isset($_GET['week']) && $_GET['week'] == $week['week']) {
?>
<div class="container week_container">
<div class="col-xs-12">
<div class="row week_heading">
<h3 class="col-xs-12">WEEK <?php echo $week['week']; ?></h3>
<p class="col-xs-12"><?php echo date("F j", strtotime($week['week_start'])) . ' - ' . date("F j, Y", strtotime($week['week_end'])); ?></p>
</div>
<?php
// loop through each League
foreach ($leagues AS $league) {
$league_id = $league['id'];
$league_name = $league['name'];
$league_abbr = $league['league_abbr'];
$league_start = $league['start_date'];
$league_end = $league['end_date'];
$league_crestlg = $league['img_lrg'];
$league_crestsm = $league['img_icon'];
$league_img_dir = ACE_IMAGE_UPLOADPATH . strtolower(str_replace(' ', '_', $league['name'])) . '/';
// Make sure matches exist for given league and given week...only display leagues with matches that week
$query_league_matches = "SELECT id FROM matches WHERE league_id = '$league_id' AND DATE(match_time) >= '" . $week['week_start'] . "' AND DATE(match_time) <= '" . $week['week_end'] . "'";
$result_league_matches = mysqli_query($dbc, $query_league_matches)
or die('Error querying matches for number of league matches in given week.');
if (mysqli_num_rows($result_league_matches) > 0) {
?>
<div class="row league_container">
<div class="row">
<div class="panel-group" id="accordion_league<?php echo $league_id; ?>">
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title"><a data-toggle="collapse" data-parent="#accordion_league<?php echo $league_id; ?>" href="#collapse_league<?php echo $league_id; ?>"><?php echo $league_name; ?> Matches</a></h4>
</div>
<div id="collapse_league<?php echo $league_id; ?>" class="panel-collapse collapse">
<div class="panel-body">
<?php
// Create Match variables
foreach ($matches as $match) {
$match_id = $match['id'];
$match_time = $match['match_time'];
$match_date = date('Y-m-d', strtotime($match_time)); // to use for grabbing matches for a given week
$match_time_convert = strtotime($match_time); // convert user input time to a UNIX timestamp so i can convert to a diff format to display
$match_time_readable = date('g:ia', $match_time_convert);
$match_date_readable = date('l F j, Y', $match_time_convert);
$match_leagueid = $match['league_id'];
$league_name = $match['league_name'];
$home_team = $match['home_team'];
$home_abbr = $match['home_abbr'];
$home_id = $match['home_team_id'];
$home_crestlg = $match['home_crestlg'];
$home_crestsm = $match['home_crestsm'];
$home_url = $match['home_url'];
$stadium = $match['stadium'];
$away_team = $match['away_team'];
$away_abbr = $match['away_abbr'];
$away_id = $match['away_team_id'];
$away_crestlg = $match['away_crestlg'];
$away_crestsm = $match['away_crestsm'];
$away_url = $match['away_url'];
$result_home = $match['result_home'];
$result_away = $match['result_away'];
$score_home = $match['home_score'];
$score_away = $match['away_score'];
$pks = $match['pks'];
$pks_home = $match['pks_home'];
$pks_away = $match['pks_away'];
$user_match_id = $match['user_matchid'];
$user_winner_id = $match['user_winnerid'];
$user_score_home = $match['user_hscore'];
$user_score_away = $match['user_ascore'];
// set winner variable
if ($result_home == 'W') { // HOME team win
$winner = $home_team;
$winner_id = $home_id;
}
elseif ($result_away == 'W') { // AWAY team win
$winner = $away_team;
$winner_id = $away_id;
}
else { // Draw
$winner = '';
$winner_id = '';
}
// TODO: if MATCH SCORES change, UPDATE edited selections only - via AJAX
/* STACKOVERFLOW - MATCH SCORE UPDATES */
// if user edits match scores UPDATE userpicks table with new scores and winner picks
if (isset($_POST['picks_submit' . $match_id])) {
$picks_matchid = $_POST['picks_matchids'][$match_id];
$picks_hscore = $_POST['home_scores'][$match_id];
$picks_homeid = $_POST['home_ids'][$match_id];
$picks_ascore = $_POST['away_scores'][$match_id];
$picks_awayid = $_POST['away_ids'][$match_id];
if ((!empty($picks_hscore)) && (!empty($picks_ascore))) {
// set winner variable
if ($picks_hscore > $picks_ascore) { // HOME team win
$picks_winnerid = $picks_homeid;
}
elseif ($picks_ascore > $picks_hscore) { // AWAY team win
$picks_winnerid = $picks_awayid;
}
else { // Draw
$picks_winner = 'DRAW';
$picks_winnerid = '-1';
}
$query_score_update = "UPDATE user_picks " .
"SET team_id_winner = '$picks_winnerid', home_score = '$picks_hscore', away_score = '$picks_ascore' " .
"WHERE user_id = '$user_id' AND match_id = '$picks_matchid'";
mysqli_query($dbc, $query_score_update)
or die('Error querying user_picks to update scores for Match #' . $match_id . '.');
}
elseif ((empty($picks_hscore)) && (!empty($picks_ascore))) { // if home score is empty
// TODO: echo errors for missing score data
}
else { // if away score is empty
// TODO: echo errors for missing score data
}
}
//else {
// DO I NEED ANYTHING IF POST IS NOT SET?
//}
// Display this week's matches
if (($match_leagueid == $league_id) && ($match_date >= $week['week_start']) && ($match_date <= $week['week_end'])) {
if (($cur_time > $match_time)) {
echo '<div class="row match_container past_match">';
}
else {
echo '<div class="row match_container">';
}
?>
<ul class="match_info col-md-2 col-xs-12">
<li class="match_date"><span class="label">Date</span><?php echo date("D. M. j", strtotime($match_time)); ?></li>
<li class="match_time"><span class="label">Kickoff</span><?php echo date("g:ia", strtotime($match_time)); ?></li>
<li class="stadium"><span class="label">Stadium</span><?php echo $stadium; ?></li>
<li class="league_logo"><span class="label">League</span>
<?php
if (!empty($league_crestsm)) {
echo '<img src="' . $league_img_dir . 'crests/' . $league_crestsm . '" alt="' . $league_name . ' logo" title="This is a ' . $league_name . ' match." /></li>';
}
else {
echo $league_name;
}
?>
</li>
</ul>
<form method="POST" action="<?php $_SERVER['PHP_SELF']; ?>" id="form_match<?php echo $match_id; ?>" class="teams_container col-md-8 col-xs-12">
<input type="hidden" min="0" class="form-control" id="picks_matchid_<?php echo $match_id; ?>" name="<?php echo 'picks_matchids[' . $match_id . ']'; ?>" value="<?php echo $match_id; ?>" />
<?php // if results exist for this match, display them
// if no results exist for this match
else {
?>
<div>
<div class="home_team col-xs-5">
<div class="row">
<h4 class="col-xs-12 team_name"><?php echo $home_team; ?></h4>
<h4 class="col-xs-12 team_name team_abbreviation"><a href="<?php echo $home_url; ?>" rel="" title="Visit <?php echo $home_team; ?>'s website."><?php echo $home_abbr; ?></a></h4>
<p class="col-xs-12 team_url"><a href="<?php echo $home_url; ?>" rel="" title="Visit <?php echo $home_team; ?>'s website."><?php echo $home_url; ?></a></p>
</div>
<div class="row">
<img class="team_crest col-xs-6" src="<?php echo $league_img_dir; ?>crests/<?php echo $home_crestsm; ?>" alt="<?php echo $home_team; ?>'s crest" />
<div class="form-group home_score score col-xs-6">
<input type="hidden" class="form-control" id="homeid_<?php echo $home_id; ?>" name="<?php echo 'home_ids[' . $match_id . ']'; ?>" value="<?php echo $home_id; ?>" />
<label class="sr-only" for="home_score">Home Score</label>
<?php
// if current time is BEFORE match time, allow user to pick match results
if ($cur_time < $match_time) {
if ((!empty($user_score_home) || ($user_score_home === 0)) && ($_SESSION['user_rules'] == 'Y')) {
?>
<!-- STACKOVERFLOW - MATCH SCORE UPDATES -->
<input type="number" min="0" class="form-control" id="home_score_<?php echo $match_id; ?>" name="<?php echo 'home_scores[' . $match_id . ']'; ?>" value="<?php echo $user_score_home; ?>" />
<?php
}
// if user has NOT yet agreed to RULES, do not allow user to pick match results
elseif ((!empty($user_score_home) || ($user_score_home === 0)) && ($_SESSION['user_rules'] == 'N')) {
?>
<input type="number" min="0" disabled="disabled" class="form-control" id="home_score_<?php echo $match_id; ?>" name="<?php echo 'home_scores[' . $match_id . ']'; ?>" value="<?php echo $_POST['home_scores'][$match_id]; ?>" />
<?php
}
else {
?>
<input type="number" min="0" class="form-control" id="home_score_<?php echo $match_id; ?>" name="<?php echo 'home_scores[' . $match_id . ']'; ?>" value="<?php echo $_POST['home_scores'][$match_id]; ?>" />
<?php
}
}
// if current time is AFTER match time and match OVER, DO NOT allow user to pick match results
elseif ($cur_time >= date('Y-m-d H:i', (strtotime('+95 minutes', strtotime($match_time))))) {
?>
<input type="number" min="0" disabled="disabled" class="form-control" id="home_score_<?php echo $match_id; ?>" name="<?php echo 'home_scores[' . $match_id . ']'; ?>" value="<?php echo $user_score_home; ?>" />
<p class="alert-danger post_match_warning">Results pending.</p>
<?php
}
// if current time is AFTER match time but match still playing, DO NOT allow user to pick match results
else {
?>
<input type="number" min="0" disabled="disabled" class="form-control" id="home_score_<?php echo $match_id; ?>" name="<?php echo 'home_scores[' . $match_id . ']'; ?>" value="<?php echo $user_score_home; ?>" />
<p class="alert-danger post_match_warning">Too Late.</p>
<?php
}
?>
</div>
</div>
</div>
<p class="versus_text col-xs-2">vs</p>
<div class="away_team col-xs-5">
<div class="row">
<h4 class="col-xs-12 team_name"><?php echo $away_team; ?></h4>
<h4 class="col-xs-12 team_name team_abbreviation"><a href="<?php echo $away_url; ?>" rel="" title="Visit <?php echo $away_team; ?>'s website."><?php echo $away_abbr; ?></a></h4>
<p class="col-xs-12 team_url"><a href="<?php echo $away_url; ?>" rel="" title="Visit <?php echo $away_team; ?>'s website."><?php echo $away_url; ?></a></p>
</div>
<div class="row">
<img class="team_crest col-xs-6" src="<?php echo $league_img_dir; ?>crests/<?php echo $away_crestsm; ?>" alt="<?php echo $away_team; ?>'s crest" />
<div class="form-group away_score score col-xs-6">
<input type="hidden" class="form-control" id="awayid_<?php echo $away_id; ?>" name="<?php echo 'away_ids[' . $match_id . ']'; ?>" value="<?php echo $away_id; ?>" />
<label class="sr-only" for="away_score">Away Score</label>
<?php
// if current time is BEFORE match time, allow user to pick match results
if ($cur_time < $match_time) {
if ((!empty($user_score_away) || ($user_score_away === 0)) && ($_SESSION['user_rules'] == 'Y')) {
?>
<!-- STACKOVERFLOW - MATCH SCORE UPDATES -->
<input type="number" min="0" class="form-control" id="away_score_<?php echo $match_id; ?>" name="<?php echo 'away_scores[' . $match_id . ']'; ?>" value="<?php echo $user_score_away; ?>" />
<?php
}
// if user has NOT yet agreed to RULES, do not allow user to pick match results
elseif ((!empty($user_score_away) || ($user_score_away === 0)) && ($_SESSION['user_rules'] == 'N')) {
?>
<input type="number" min="0" disabled="disabled" class="form-control" id="away_score_<?php echo $match_id; ?>" name="<?php echo 'away_scores[' . $match_id . ']'; ?>" value="<?php echo $_POST['away_scores'][$match_id]; ?>" />
<?php
}
else {
?>
<input type="number" min="0" class="form-control" id="away_score_<?php echo $match_id; ?>" name="<?php echo 'away_scores[' . $match_id . ']'; ?>" value="<?php echo $_POST['away_scores'][$match_id]; ?>" />
<?php
}
}
// if current time is AFTER match time and match OVER, DO NOT allow user to pick match results
elseif ($cur_time >= date('Y-m-d H:i', (strtotime('+95 minutes', strtotime($match_time))))) {
?>
<input type="number" min="0" disabled="disabled" class="form-control" id="away_score_<?php echo $match_id; ?>" name="<?php echo 'away_scores[' . $match_id . ']'; ?>" value="<?php echo $user_score_away; ?>" />
<p class="alert-danger post_match_warning">Results pending.</p>
<?php
}
// if current time is AFTER match time but match still playing, DO NOT allow user to pick match results
else {
?>
<input type="number" min="0" disabled="disabled" class="form-control" id="away_score_<?php echo $match_id; ?>" name="<?php echo 'away_scores[' . $match_id . ']'; ?>" value="<?php echo $user_score_away; ?>" />
<p class="alert-danger post_match_warning">Too Late.</p>
<?php
}
?>
</div>
</div>
</div>
</div>
<?php
}
if (($cur_time < $match_time) && ($_SESSION['user_rules'] == 'Y')) {
?>
<!-- STACKOVERFLOW - MATCH SCORE UPDATES -->
<div class="row">
<div class="form-group col-sm-4 col-sm-offset-4 col-xs-12">
<input type="submit" id="picks_submit<?php echo $match_id; ?>" class="btn btn-primary btn-lrg btn-block form-control submit" name="picks_submit<?php echo $match_id; ?>" value="Update Match" />
</div>
</div>
<!-- further code here -->
</form><!-- end .TEAMS_CONTAINER/score input form -->
</div><!-- ends .PAST_MATCH div -->
<!-- remaining code is below here -->
答案 0 :(得分:0)
我会尝试在一个例子中向你解释,我希望你能理解它。 很抱歉,但是你的代码很长,并不是最好的代码,我现在没有时间理解它。
假设您要更新数据库表格中的名称,并且您不想拥有多个文件。
<?php
$connect = mysqli_connect('hostname', 'username', 'pass', 'dbname');
if(mysqli_connect_errno()){
echo 'connection error: ' . mysqli_connect_error();
}
function update($name, $id){
mysqli_query($connect,
"UPDATE `my_user_table` SET `name`='$name' WHERE id=$id")
or die(mysqli_error($connection));
$success = '<p>Success</p>';
return $success;
}
if(isset($_POST['name']) && isset($_POST['id'])){
$myupdate = update($_POST['name'], $_POST['id']);
echo $myupdate;
exit;
}
?>
<div id="formcont">
<form id="updateform" name="update" method="post">
<input type="text" name="name" id="name" />
<input type="hidden" name="id" value="1" />
<input type="submit" id="submit" name="submit" value="submit" />
</form>
</div>
<script>
jQuery(document).ready(function(){
$('#submit').click(function(){ //or you can use on() instead of click()
var formdata = $('#updateform').serialize();
$.ajax({
type: "POST",
cache: false, //or if you want cache delete this line
data: formdata,
success: function(data){
//do whatever you want here
//for example remove form and print a success text
$('#updateform').remove();
$('#formcont').append(data);
}
});
return false;
});
});
</script>
这只是一个例子,你如何在同一个文件中使用你的php函数并使用ajax。