所以我在页面上有3个表单,并且根据提交的表单 - 将某些内容输入到数据库中。现在,如果我将它全部包含在同一页面上,这段代码可以完美运行,但当然,它需要刷新,所以我希望实现AJAX。现在,我已经将插入php代码移动到新页面上,并且正在寻找一些帮助来构建一个AJAX调用以将数据传递到php页面,这将继续插入正确的数据。
本质:
我将使用PDO重写SQL,所以请在当前代码中忽略它。
<form id="form1" method="post">
<input type="submit" id="activity1" name="activity1" class="btn btn-info span3 mhm" value="<?php echo htmlspecialchars($type1);?>">
</form>
<form id="form2" action="registerresults.php" method="post">
<input type="submit" id="activity2" name="activity2" class="btn btn-info span3 mhm" value="<?php echo htmlspecialchars($type2);?>">
</form>
<form id="form3" action="registerresults.php" method="post">
<input type="submit" id="activity3" name="activity3" class="btn btn-info span3 mhm" value="<?php echo htmlspecialchars($type3);?>">
</form>
// registerresults.php - 插入数据库
$id = $_SESSION['id'];
$competitionId = $_GET['competitionId'];
$organisationId = $_SESSION['organisationId'];
if (isset($_POST['activity1']) && !empty($_POST['activity1']))
{
//insert new points into database
$today = date("Y-m-d h:i:s");
$insertCall = mysql_query("INSERT INTO `entries` (`userid`, `competitionId`, `activity_type`, `activity_id`, `points`, `date`) VALUES ('$id', '$competitionId', '$type1', '1', '$weighting1', '$today');");
}
if (isset($_POST['activity2']) && !empty($_POST['activity2']))
{
$today = date("Y-m-d h:i:s");
$insertCall = mysql_query("INSERT INTO `entries` (`userid`, `competitionId`, `activity_type`, `activity_id`, `points`, `date`) VALUES ('$id', '$competitionId', '$type2', '2', '$weighting2', '$today');");
}
if (isset($_POST['activity3']) && !empty($_POST['activity3']))
{
$today = date("Y-m-d h:i:s");
$insertCall = mysql_query("INSERT INTO `entries` (`userid`, `competitionId`, `activity_type`, `activity_id`, `points`, `date`) VALUES ('$id', '$competitionId', '$type3', '3', '$weighting3', '$today');");
}
?>