So I have 3 forms on a page, and depending on which form is submitted - something is entered into the DB. Now, this code works perfectly, if I include it all on the same page, but of course, it requires a refresh, so I am looking to implement AJAX. Now, I've moved the insert php code onto a new page, and am looking for some help with building an AJAX call to pass the data to the php page, which will proceed to insert the right data.
Essentially:
- There are three forms, one for each activity type.
- Depending on which form is submitted, some different data is entered into the DB
- Need help with building an AJAX call to figure out which form was submitted, pass the data to the next page succesfully so the PHP code can insert into DB.
I will rewrite the SQL using PDO, so please ignore that in the current code.
<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 - Inserts into DB
$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');"); } ?>